/ex13.py

https://bitbucket.org/korovamilk/python_for_kids
Python | 121 lines | 95 code | 15 blank | 11 comment | 15 complexity | 8a9ed4ab8f6819f26bb3979ea4e3513d MD5 | raw file
  1. #!/usr/bin/env python3
  2. from tkinter import *
  3. import random
  4. import time
  5. # Ball class
  6. class Ball:
  7. def __init__(self, canvas, paddle, score, color):
  8. self.canvas = canvas
  9. self.paddle = paddle
  10. self.score = score
  11. self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
  12. self.canvas.move(self.id, 245, 100)
  13. starts = [-3, -2, -1, 1, 2, 3]
  14. random.shuffle(starts)
  15. self.x = starts[0]
  16. self.y = -3
  17. self.canvas_height = self.canvas.winfo_height()
  18. self.canvas_width = self.canvas.winfo_width()
  19. self.hit_bottom = False
  20. def hit_paddle(self, pos):
  21. paddle_pos = self.canvas.coords(self.paddle.id) # paddle coords
  22. if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  23. if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  24. self.x += self.paddle.x
  25. self.score.hit()
  26. return True
  27. return False
  28. def draw(self):
  29. self.canvas.move(self.id, self.x, self.y)
  30. pos = self.canvas.coords(self.id)
  31. print('Ball: %s' %self.canvas.coords(self.id))
  32. # [x1, y1, x2, y2 ]
  33. # ex: [255.0, 203.0, 270.0, 218.0]
  34. # [pos[0], pos[1], pos[2], pos[3]]
  35. if pos[1] <= 0:
  36. self.y = 3
  37. if pos[3] >= self.canvas_height:
  38. self.hit_bottom = True
  39. if self.hit_paddle(pos) == True:
  40. self.y = -3
  41. if pos[0] <= 0:
  42. self.x = 3
  43. if pos[2] >= self.canvas_width:
  44. self.x = -3
  45. class Paddle:
  46. def __init__(self, canvas, color):
  47. self.canvas = canvas
  48. self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
  49. self.canvas.move(self.id, 200, 300)
  50. self.x = 0
  51. self.canvas_width = self.canvas.winfo_width()
  52. self.started = False
  53. self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
  54. self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
  55. self.canvas.bind_all('<Button-1>', self.start_game)
  56. def draw(self):
  57. self.canvas.move(self.id, self.x, 0) # no vertical moves allowed
  58. pos = self.canvas.coords(self.id)
  59. print('Paddle: %s' % self.canvas.coords(self.id))
  60. if pos[0] <= 0:
  61. self.x = 0
  62. elif pos[2] >= self.canvas_width:
  63. self.x = 0
  64. def turn_left(self, evt):
  65. self.x = -2
  66. def turn_right(self, evt):
  67. self.x = 2
  68. def start_game(self, evt):
  69. self.started = True
  70. class Score:
  71. def __init__(self, canvas, color):
  72. self.score = 0
  73. self.canvas = canvas
  74. self.id = canvas.create_text(450, 10, text=self.score, fill=color)
  75. def hit(self):
  76. self.score += 10
  77. self.canvas.itemconfig(self.id, text=self.score)
  78. tk = Tk()
  79. tk.title("Game")
  80. tk.resizable(0, 0) # windows is not resizeable
  81. tk.wm_attributes("-topmost", 1) # window always on top
  82. w=500
  83. h=400
  84. canvas = Canvas(tk, width=w, height=h, bd=0, highlightthickness = 0)
  85. canvas.pack()
  86. tk.update()
  87. score = Score(canvas, 'red')
  88. paddle = Paddle(canvas, 'blue')
  89. ball = Ball(canvas, paddle, score, 'yellow')
  90. #game_start_text = canvas.create_text(250, 200, text='GAME START', state='hidden')
  91. game_over_text = canvas.create_text(250, 200, text='GAME OVER', state='hidden')
  92. #game_score_text = canvas.create_text(0,400, text=ball.score, state='hidden')
  93. # main loop: tells tk to redraw screen
  94. while 1:
  95. ## if paddle.started == True:
  96. ## time.sleep(1)
  97. ## canvas.itemconfig(game_start_text, state='normal')
  98. if ball.hit_bottom == False and paddle.started == True:
  99. ball.draw()
  100. paddle.draw()
  101. if ball.hit_bottom == True:
  102. time.sleep(1)
  103. canvas.itemconfig(game_over_text, state='normal')
  104. tk.update_idletasks()
  105. tk.update()
  106. time.sleep(0.01) # two hundredth of a second