Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3. import time
  4.  
  5. class Ball:
  6. def __init__(self, canvas, paddle, color, score):
  7. self.canvas = canvas
  8. self.paddle = paddle
  9. self.score = score
  10. self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
  11. self.canvas.move(self.id, 245, 100)
  12. starts = [-3, -2, -1, 1, 2, 3]
  13. random.shuffle(starts)
  14. self.x = starts[0]
  15. self.y = -1
  16. self.canvas_width = self.canvas.winfo_width()
  17. self.canvas_height = self.canvas.winfo_height()
  18. self.hit_bottom = False
  19. def draw(self):
  20. self.canvas.move(self.id, self.x, self.y)
  21. pos = self.canvas.coords(self.id)
  22. if pos[0] <= 0:
  23. self.x = 3.5
  24. if pos[3] >= self.canvas_height:
  25. self.canvas.create_text(250, 200, font="Times 50", text="GAME OVER", fill="lawn green")
  26. self.canvas.create_text(250, 330, font="Times 20", text="You did good, but not good enough.", fill="lawn green")
  27. self.canvas.create_text(160, 250, font="Times 20", text="Your score is,", fill="lawn green")
  28. self.hit_bottom = True
  29. if pos[2] >= self.canvas_width:
  30. self.x = -3.5
  31. if pos[1] <= 0:
  32. self.y = 3.5
  33. if pos[3] >= self.canvas_height:
  34. self.y = -3.5
  35. if self.hit_paddle(pos) == True:
  36. self.y = -3.5
  37. self.canvas.create_text(250, 250, font="Times 20", text=self.score, fill="coral4")
  38. self.score += 1
  39. self.canvas.create_text(250, 250, font="Times 20", text=self.score, fill="lawn green")
  40. def hit_paddle(self, pos):
  41. paddle_pos = self.canvas.coords(self.paddle.id)
  42. if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  43. if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  44. return True
  45. return False
  46.  
  47. class Paddle:
  48. def __init__(self, canvas, color):
  49. self.canvas = canvas
  50. self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
  51. self.canvas.move(self.id, 200, 300)
  52. self.x = 0
  53. self.canvas_width = self.canvas.winfo_width()
  54. self.canvas.bind_all("<KeyPress-Left>", self.turn_left)
  55. self.canvas.bind_all("<KeyPress-Right>", self.turn_right)
  56. def turn_left(self, evt):
  57. self.x = -5
  58. def turn_right(self, evt):
  59. self.x = 5
  60. def draw(self):
  61. self.canvas.move(self.id, self.x, 0)
  62. pos = self.canvas.coords(self.id)
  63. if pos[0] <= 0:
  64. self.x = 0
  65. elif pos[2] >= self.canvas_width:
  66. self.x = 0
  67. tk = Tk()
  68. tk.title("Game")
  69. tk.resizable(0,0)
  70. tk.wm_attributes("-topmost", 1)
  71. canvas = Canvas(tk, width=500, height=500, bd=0, bg="coral4", highlightthickness=0)
  72. canvas.pack()
  73. tk.update()
  74. score = 0
  75. paddle = Paddle(canvas, "blue")
  76. ball = Ball(canvas, paddle, "yellow", score)
  77.  
  78. while 1:
  79. if ball.hit_bottom == False:
  80. ball.draw()
  81. paddle.draw()
  82. tk.update_idletasks()
  83. tk.update()
  84. time.sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement