Guest User

Untitled

a guest
May 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3. WIDTH = 1000
  4. HEIGHT = 500
  5.  
  6. R_W = 10
  7. R_H = 100
  8.  
  9. BALL_RADIUS = 35
  10.  
  11. root = Tk()
  12.  
  13. c = Canvas(root, width=WIDTH, height=HEIGHT, background="#006400")
  14. c.pack()
  15. c.create_line(R_W, 0, R_W, HEIGHT, fill="#006400")
  16. c.create_line(WIDTH-R_W, 0, WIDTH-R_W, HEIGHT, fill="#006400")
  17. c.create_line(WIDTH/2, 0, WIDTH/2, HEIGHT, fill="white")
  18.  
  19. BALL = c.create_oval(WIDTH/2+BALL_RADIUS/2,
  20. HEIGHT/2+BALL_RADIUS/2,
  21. WIDTH/2-BALL_RADIUS/2,
  22. HEIGHT/2-BALL_RADIUS/2,
  23. fill="white")
  24.  
  25.  
  26.  
  27. LEFT_PAD = c.create_line(R_W, 0, R_W, R_H+1, width=R_W, fill="red")
  28.  
  29. RIGHT_PAD = c.create_line(WIDTH-R_W/2, 0, WIDTH-R_W/2,
  30. R_H+10, width=R_W, fill="blue")
  31.  
  32. BALL_X_CHANGE = 10
  33. BALL_Y_CHANGE = 0
  34.  
  35. def move_ball():
  36. c.move(BALL, BALL_X_CHANGE, BALL_Y_CHANGE)
  37.  
  38. def main():
  39. move_ball()
  40. root.after(30, main)
  41.  
  42. main()
  43. PAD_SPEED = 20
  44. LEFT_PAD_SPEED = 0
  45. RIGHT_PAD_SPEED = 0
  46.  
  47. def move_pads():
  48. PADS = {LEFT_PAD: LEFT_PAD_SPEED,
  49. RIGHT_PAD: RIGHT_PAD_SPEED}
  50. for pad in PADS:
  51.  
  52. c.move(pad, 0, PADS[pad])
  53.  
  54. if c.coords(pad)[1] < 0:
  55. c.move(pad, 0, -c.coords(pad)[1])
  56. elif c.coords(pad)[3] > HEIGHT:
  57. c.move(pad, 0, HEIGHT - c.coords(pad)[3])
  58.  
  59. def main():
  60. move_ball()
  61. move_pads()
  62. root.after(30, main)
  63.  
  64. c.focus_set()
  65.  
  66. def movement_handler(event):
  67. global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
  68. if event.keysym == "w":
  69. LEFT_PAD_SPEED = -PAD_SPEED
  70. elif event.keysym == "s":
  71. LEFT_PAD_SPEED = PAD_SPEED
  72. elif event.keysym == "Up":
  73. RIGHT_PAD_SPEED = -PAD_SPEED
  74. elif event.keysym == "Down":
  75. RIGHT_PAD_SPEED = PAD_SPEED
  76.  
  77.  
  78. c.bind("<KeyPress>", movement_handler)
  79.  
  80. def stop_pad(event):
  81. global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
  82. if event.keysym in "ws":
  83. LEFT_PAD_SPEED = 0
  84. elif event.keysym in ("Up", "Down"):
  85. RIGHT_PAD_SPEED = 0
  86.  
  87.  
  88. c.bind("<KeyRelease>", stop_pad)
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. BALL_SPEED_UP = 1.04
  96. BALL_MAX_SPEED = 20
  97. BALL_X_SPEED = 10
  98. BALL_Y_SPEED = 10
  99. right_line_distance = WIDTH - R_W
  100.  
  101. INITIAL_SPEED = 10
  102. def bounce(action):
  103. global BALL_X_SPEED, BALL_Y_SPEED
  104. if action == "strike":
  105. BALL_Y_SPEED = random.randrange(-10, 10)
  106. if abs(BALL_X_SPEED) < BALL_MAX_SPEED:
  107. BALL_X_SPEED *= -BALL_SPEED_UP
  108. else:
  109. BALL_X_SPEED = -BALL_X_SPEED
  110. else:
  111. BALL_Y_SPEED = -BALL_Y_SPEED
  112.  
  113.  
  114.  
  115. def run_ball():
  116. global BALL_X_SPEED
  117. c.coords(BALL, WIDTH/2-BALL_RADIUS/2,
  118. HEIGHT/2-BALL_RADIUS/2,
  119. WIDTH/2+BALL_RADIUS/2,
  120. HEIGHT/2+BALL_RADIUS/2)
  121. BALL_X_SPEED = -(BALL_X_SPEED * -INITIAL_SPEED) / abs(BALL_X_SPEED)
  122.  
  123.  
  124.  
  125.  
  126. def move_ball():
  127. ball_left, ball_top, ball_right, ball_bot = c.coords(BALL)
  128. ball_center = (ball_top + ball_bot) / 2
  129. if ball_right + BALL_X_SPEED < right_line_distance and \
  130. ball_left + BALL_X_SPEED > R_W:
  131. c.move(BALL, BALL_X_SPEED, BALL_Y_SPEED)
  132.  
  133. elif ball_right == right_line_distance or ball_left == R_W:
  134. if ball_right > WIDTH / 2:
  135. if c.coords(RIGHT_PAD)[1] < ball_center < c.coords(RIGHT_PAD)[3]:
  136. bounce("strike")
  137. else:
  138. run_ball()
  139.  
  140. else:
  141. if c.coords(LEFT_PAD)[1] < ball_center < c.coords(LEFT_PAD)[3]:
  142. bounce("strike")
  143. else:
  144. run_ball()
  145. else:
  146. if ball_right > WIDTH / 2:
  147. c.move(BALL, right_line_distance-ball_right, BALL_Y_SPEED)
  148. else:
  149. c.move(BALL, -ball_left+R_W, BALL_Y_SPEED)
  150. if ball_top + BALL_Y_SPEED < 0 or ball_bot + BALL_Y_SPEED > HEIGHT:
  151. bounce("ricochet")
  152.  
  153. root.mainloop()
Add Comment
Please, Sign In to add comment