Guest User

Untitled

a guest
May 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. # Implementation of classic arcade game Pong
  2.  
  3. import simplegui
  4. import random
  5.  
  6. # initialize globals - pos and vel encode vertical info for paddles
  7. WIDTH = 600
  8. HEIGHT = 400
  9. BALL_RADIUS = 20
  10. PAD_WIDTH = 8
  11. PAD_HEIGHT = 80
  12. HALF_PAD_WIDTH = PAD_WIDTH / 2
  13. HALF_PAD_HEIGHT = PAD_HEIGHT / 2
  14. PADDLE_VEL = 5
  15. LEFT = False
  16. RIGHT = True
  17.  
  18. paddle1_pos = HEIGHT/2
  19. paddle2_pos = HEIGHT/2
  20. paddle1_vel = 0
  21. paddle2_vel = 0
  22. score1 = 0
  23. score2 = 0
  24.  
  25. # initialize ball_pos and ball_vel for new bal in middle of table
  26. ball_pos = [WIDTH/2, HEIGHT/2]
  27. ball_vel = [1, -1]
  28.  
  29. # if direction is RIGHT, the ball's velocity is upper right, else upper left
  30. def spawn_ball(direction):
  31. global ball_pos, ball_vel # these are vectors stored as lists
  32. ball_pos = [WIDTH/2, HEIGHT/2]
  33.  
  34. ball_vel[0] = -random.randrange(12,24) / 100
  35. if direction == True:
  36. ball_vel[0] = - ball_vel[0]
  37. ball_vel[1] = -random.randrange(60, 180) / 100
  38.  
  39. # define event handlers
  40. def new_game():
  41. global paddle1_pos_top, paddle2_pos_bot, paddle1_vel, paddle2_vel
  42. global ball_pos, ball_vel # these are numbers
  43. global score1, score2 # these are ints
  44. ball_pos = [WIDTH/2, HEIGHT/2]
  45. paddle1_pos = [0, HEIGHT/2]
  46. paddle2_pos = [WIDTH, HEIGHT/2]
  47. score1 = 0
  48. score2 = 0
  49. spawn_ball(random.choice([LEFT, RIGHT]))
  50.  
  51. def draw(canvas):
  52. global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, direction
  53.  
  54.  
  55. # draw mid line and gutters
  56. canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 4, "Red")
  57. canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 4, "Red")
  58. canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 4, "Red")
  59.  
  60. # draw ball
  61. canvas.draw_circle(ball_pos, 15, 12, "Blue", "Blue")
  62. # update paddle's vertical position, keep paddle on the screen
  63. #left player paddle
  64. if ((paddle1_pos <= HEIGHT - PAD_HEIGHT) and (paddle1_vel > 0)) or ((paddle1_pos >= 0) and (paddle1_vel < 0)):
  65. paddle1_pos += paddle1_vel
  66. #right player paddle
  67. elif ((paddle2_pos <= HEIGHT - PAD_HEIGHT) and (paddle2_vel > 0)) or ((paddle2_pos >= 0) and (paddle2_vel < 0)):
  68. paddle2_pos += paddle2_vel
  69.  
  70. # draw paddles
  71. canvas.draw_line([0, paddle1_pos], [0, paddle1_pos + PAD_HEIGHT], PAD_WIDTH, "White")
  72. canvas.draw_line([WIDTH, paddle2_pos], [WIDTH, paddle2_pos + PAD_HEIGHT], PAD_WIDTH, "White")
  73.  
  74. # determine whether paddle and ball collide
  75. # update ball
  76. ball_pos[0] += ball_vel[0]
  77. ball_pos[1] += ball_vel[1]
  78.  
  79. #upper and lower wall collisions
  80. if ball_pos[1] <= 20:
  81. ball_vel[1] = - ball_vel[1]
  82. elif ball_pos[1] >= HEIGHT - 20:
  83. ball_vel[1] = - ball_vel[1]
  84.  
  85. #Checking for collision of ball with paddles
  86. #checking left side for collision
  87. if ball_pos[0] <= HALF_PAD_WIDTH + BALL_RADIUS:
  88. if((ball_pos[1] > paddle1_pos + PAD_HEIGHT) and (ball_pos[1] < HEIGHT)):
  89. score2 += 1
  90. spawn_ball(RIGHT)
  91. elif ((ball_pos[1] < paddle1_pos) and (ball_pos[1] > 0)):
  92. score2 += 1
  93. spawn_ball(RIGHT)
  94. elif ((ball_pos[1] >= paddle1_pos) and (ball_pos[1] <= paddle1_pos + PAD_HEIGHT + 2)):
  95. ball_vel[0] = - ball_vel[0]
  96. ball_vel[0] += 0.1 * ball_vel[0]
  97.  
  98. #checking right side for collision
  99. if ball_pos[0] >= WIDTH - HALF_PAD_WIDTH - BALL_RADIUS:
  100. if((ball_pos[1] > paddle2_pos + PAD_HEIGHT) and (ball_pos[1] < HEIGHT)):
  101. score1 += 1
  102. spawn_ball(LEFT)
  103. elif ((ball_pos[1] < paddle2_pos - PAD_HEIGHT) and (ball_pos[1] > 0)):
  104. score1 += 1
  105. spawn_ball(LEFT)
  106. elif ((ball_pos[1] >= paddle2_pos) and (ball_pos[1] <= paddle2_pos + PAD_HEIGHT + 2)):
  107. ball_vel[0] = - ball_vel[0]
  108. ball_vel[0] += 0.1 * ball_vel[0]
  109.  
  110. # draw scores
  111. canvas.draw_text(str(score1), (440, 40), 40, "White")
  112. canvas.draw_text(str(score2), (140, 40), 40, "White")
  113.  
  114. def keydown(key):
  115. global paddle1_vel, paddle2_vel
  116.  
  117. #left player paddle
  118. if key == simplegui.KEY_MAP["w"]:
  119. paddle1_vel = -PADDLE_VEL
  120. elif key == simplegui.KEY_MAP["s"]:
  121. paddle1_vel = PADDLE_VEL
  122.  
  123. #right player paddle
  124. if key == simplegui.KEY_MAP["up"]:
  125. paddle2_vel = -PADDLE_VEL
  126. elif key == simplegui.KEY_MAP["down"]:
  127. paddle2_vel = PADDLE_VEL
  128.  
  129. def keyup(key):
  130. global paddle1_vel, paddle2_vel, paddle_vel
  131. #player1
  132. if key == simplegui.KEY_MAP["w"]:
  133. paddle1_vel = 0
  134. elif key == simplegui.KEY_MAP["s"]:
  135. paddle1_vel = 0
  136.  
  137. #player2
  138. if key == simplegui.KEY_MAP["down"]:
  139. paddle2_vel = 0
  140. elif key == simplegui.KEY_MAP["up"]:
  141. paddle2_vel = 0
  142.  
  143. # create frame
  144. frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
  145. frame.add_button("Restart", new_game, 100)
  146. frame.set_draw_handler(draw)
  147. frame.set_keydown_handler(keydown)
  148. frame.set_keyup_handler(keyup)
  149.  
  150.  
  151. # start frame
  152. new_game()
  153. frame.start()
Add Comment
Please, Sign In to add comment