fork download
  1. from tkinter import *
  2. import time
  3. import random
  4.  
  5. tk = Tk()
  6. tk.title('Game')
  7. tk.resizable(0, 0)
  8. tk.wm_attributes('-topmost', 1)
  9. canvas = Canvas(tk, width=500, height=400, highlightthickness=0)
  10. canvas.pack()
  11. my_image = None
  12. PhotoImage(file='d:\\earth.gif')
  13. canvas.create_image(0, 0, anchor=NW, image=my_image)
  14. tk.update()
  15.  
  16.  
  17. class Ball:
  18. def __init__(self, canvas, paddle, score, color):
  19. self.canvas = canvas
  20. self.paddle = paddle
  21. self.score = score
  22. self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
  23. self.canvas.move(self.id, 245, 100)
  24. starts = [-2, -1, 1, 2]
  25. random.shuffle(starts)
  26. self.x = starts[0]
  27. self.y = -2
  28. self.canvas_height = self.canvas.winfo_height()
  29. self.canvas_width = self.canvas.winfo_width()
  30. self.hit_bottom = False
  31.  
  32. def hit_paddle(self, pos):
  33. paddle_pos = self.canvas.coords(self.paddle.id)
  34. if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  35. if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  36. self.score.hit()
  37. return True
  38. return False
  39.  
  40. def draw(self):
  41. self.canvas.move(self.id, self.x, self.y)
  42. pos = self.canvas.coords(self.id)
  43. if pos[1] <= 0:
  44. self.y = 2
  45. if pos[3] >= self.canvas_height:
  46. self.hit_bottom = True
  47. canvas.create_text(250, 120, text='Вы проиграли', font=('Courier', 30), fill='red')
  48. if self.hit_paddle(pos) == True:
  49. self.y = -2
  50. if pos[0] <= 0:
  51. self.x = 2
  52. if pos[2] >= self.canvas_width:
  53. self.x = -2
  54.  
  55.  
  56. class Paddle:
  57. def __init__(self, canvas, color):
  58. self.canvas = canvas
  59. self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
  60. start_1 = [40, 60, 90, 120, 150, 180, 200]
  61. random.shuffle(start_1)
  62. self.starting_point_x = start_1[0]
  63. self.canvas.move(self.id, self.starting_point_x, 300)
  64. self.x = 0
  65. self.canvas_width = self.canvas.winfo_width()
  66. self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
  67. self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
  68. self.started = False
  69. self.canvas.bind_all('<KeyPress-Return>', self.start_game)
  70.  
  71. def turn_right(self, event):
  72. self.x = 2
  73.  
  74. def turn_left(self, event):
  75. self.x = -2
  76.  
  77. def start_game(self, event):
  78. self.started = True
  79.  
  80. def draw(self):
  81. self.canvas.move(self.id, self.x, 0)
  82. pos = self.canvas.coords(self.id)
  83. if pos[0] <= 0:
  84. self.x = 0
  85. elif pos[2] >= self.canvas_width:
  86. self.x = 0
  87.  
  88.  
  89. class Score:
  90. def __init__(self, canvas, color):
  91. self.score = 0
  92. self.canvas = canvas
  93. self.id = canvas.create_text(450, 10, text=self.score, font=('Courier', 15), fill=color)
  94.  
  95. def hit(self):
  96. self.score += 1
  97. self.canvas.itemconfig(self.id, text=self.score)
  98.  
  99.  
  100. score = Score(canvas, 'green')
  101. paddle = Paddle(canvas, 'White')
  102. ball = Ball(canvas, paddle, score, 'red')
  103.  
  104.  
  105. while 1:
  106. if ball.hit_bottom == False and paddle.started == True:
  107. ball.draw()
  108. paddle.draw()
  109. tk.update_idletasks()
  110. tk.update()
  111. time.sleep(0.1)
Runtime error #stdin #stdout #stderr 0.11s 23564KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'