taffners

pong

Apr 19th, 2013
2,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.11 KB | None | 0 0
  1. #works in codeskulptor.org
  2.  
  3. import simplegui
  4. import random
  5.  
  6. # initialize globals - pos and vel encode vertical info for paddles
  7. WIDTH = 1000
  8. HEIGHT = 500      
  9. BALL_RADIUS = 20
  10. PAD_WIDTH = 20
  11. PAD_HEIGHT = 80
  12. HALF_PAD_WIDTH = PAD_WIDTH / 2
  13. HALF_PAD_HEIGHT = PAD_HEIGHT / 2
  14. ball_pos = [WIDTH / 2, HEIGHT / 2]
  15. ball_vel = [0, 0]
  16. face_off_circle_radius = 110
  17. face_off_dot_radius = 15
  18. paddle1_pos = [0, (HEIGHT/2) - 30]
  19. paddle2_pos = [0, (HEIGHT/2) - 30]
  20. paddle1_vel = [0,0]
  21. paddle2_vel = [0,0]
  22. player_right_score = 0
  23. player_left_score = 0
  24.  
  25. # helper function that spawns a ball, returns a position vector and a velocity vector
  26. def helper():
  27.     global start_movement
  28.     start_movement = random.randrange(1,3)
  29.    
  30.     ball_init(start_movement)
  31.    
  32. # if right is True, spawn to the right, else spawn to the left
  33. def ball_init(start_movement):
  34.     global ball_pos, ball_vel # these are vectors stored as lists
  35.        
  36.     # initally ball moves Right
  37.     if start_movement == 1:
  38.         x_ball_vel = random.randrange(1, 5)
  39.         y_ball_vel = random.randrange(1, 5)
  40.         ball_vel = [x_ball_vel, y_ball_vel]
  41.        
  42.        
  43.     # initally ball moves left
  44.     if start_movement == 2:
  45.         x_ball_vel = random.randrange(-5, -1)
  46.         y_ball_vel = random.randrange(-5, -1)
  47.         ball_vel = [x_ball_vel, y_ball_vel]
  48.        
  49.        
  50. # define event handlers
  51. def init():
  52.     global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are floats
  53.     global score1, score2  # these are ints
  54.     pass
  55.  
  56. def draw(c):
  57.     global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, paddle1_vel
  58.     global player_right_score, player_left_score
  59.     # update paddle's vertical position, keep paddle on the screen
  60.     paddle1_pos[0] += paddle1_vel[0]
  61.     paddle1_pos[1] += paddle1_vel[1]
  62.     paddle2_pos[0] += paddle2_vel[0]
  63.     paddle2_pos[1] += paddle2_vel[1]
  64.    
  65.     if paddle1_pos[1] > HEIGHT - PAD_HEIGHT:
  66.         paddle1_vel[1] = 0
  67.     if paddle1_pos[1] < 0:
  68.         paddle1_vel[1] = 0
  69.     if paddle2_pos[1] > HEIGHT - PAD_HEIGHT:
  70.         paddle2_vel[1] = 0
  71.     if paddle2_pos[1] < 0:
  72.         paddle2_vel[1] = 0  
  73.     # draw mid line, gutters, blue lines, face off circles
  74.     c.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 2, "red")
  75.     c.draw_line([WIDTH / 2 + 125, 0],[WIDTH / 2 + 125, HEIGHT], 2, "blue") #blue line right
  76.     c.draw_line([WIDTH / 2 - 125, 0],[WIDTH / 2 - 125, HEIGHT], 2, "blue") #blue line left
  77.     c.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "red") # gutter left
  78.     c.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "red") #gutter right
  79.     c.draw_circle([WIDTH/2, HEIGHT/2], face_off_circle_radius, 1, 'red') #middle circle
  80.     c.draw_circle([WIDTH/2, HEIGHT/2], face_off_dot_radius, 1, 'red', 'red') #middle circle dot
  81.     c.draw_circle([WIDTH/4 -100, HEIGHT/4], face_off_circle_radius, 1, 'red') #faceoff circle left top
  82.     c.draw_circle([WIDTH/4 -100, HEIGHT/4], face_off_dot_radius, 1, 'red', 'red') #faceoff dot left top
  83.     c.draw_circle([WIDTH/4 -100, (HEIGHT/4)*3], face_off_circle_radius, 1, 'red') #faceoff circle left bottom
  84.     c.draw_circle([WIDTH/4 -100, (HEIGHT/4)*3], face_off_dot_radius, 1, 'red', 'red') #faceoff dot left bottom
  85.     c.draw_circle([(WIDTH/4)*3 +100, HEIGHT/4], face_off_circle_radius, 1, 'red') #faceoff circle right top
  86.     c.draw_circle([(WIDTH/4)*3 +100, HEIGHT/4], face_off_dot_radius, 1, 'red', 'red') #faceoff dot right top
  87.     c.draw_circle([(WIDTH/4)*3 +100, (HEIGHT/4)*3], face_off_circle_radius, 1, 'red') #faceoff circle right bottom
  88.     c.draw_circle([(WIDTH/4)*3 +100, (HEIGHT/4)*3], face_off_dot_radius, 1, 'red', 'red') #faceoff dot right bottom
  89.     c.draw_text((str(player_left_score)), [(WIDTH/3), HEIGHT/6], 40, 'green')
  90.     c.draw_text((str(player_right_score)), [(WIDTH/3)*2, HEIGHT/6], 40, 'green')
  91.     c.draw_text('Pong was built by Samantha Taffer March 10th 2013', [50, HEIGHT - 10], 15, 'black')
  92.    
  93.     # draw paddles
  94.     c.draw_polygon([[0, paddle1_pos[1]], [0, paddle1_pos[1] + PAD_HEIGHT],[PAD_WIDTH, paddle1_pos[1] + PAD_HEIGHT], [PAD_WIDTH, paddle1_pos[1]]], 2, 'black', 'black') #left paddle
  95.     c.draw_polygon([[WIDTH - PAD_WIDTH, paddle2_pos[1] ], [WIDTH - PAD_WIDTH, paddle2_pos[1] + PAD_HEIGHT],[WIDTH, paddle2_pos[1] + PAD_HEIGHT], [WIDTH, paddle2_pos[1]]], 2, 'black', 'black') #Right paddle
  96.    
  97.     # Update ball position
  98.     ball_pos[0] += ball_vel[0]
  99.     ball_pos[1] += ball_vel[1]
  100.    
  101.     # collide off of right gutter
  102.     if ball_pos[0] >= WIDTH - PAD_WIDTH:
  103.         # hitting paddle 2 (right paddle)
  104.         if ball_pos[1] >= paddle2_pos[1] and ball_pos[1] <= (paddle2_pos[1] + PAD_HEIGHT):
  105.             ball_vel[0] = - ball_vel[0] - 1
  106.         else:
  107.             ball_vel = [0, 0]
  108.             ball_pos = [WIDTH / 2, HEIGHT / 2]
  109.             player_left_score = player_left_score + 1
  110.             print 'player_left_score: ', player_left_score
  111.             helper()
  112.        
  113.     # collide off of left gutter
  114.    
  115.     if ball_pos[0] <= PAD_WIDTH:
  116.         # hitting paddle 1 (left paddle)
  117.         if ball_pos[1] >= paddle1_pos[1] and ball_pos[1] <= (paddle1_pos[1] + PAD_HEIGHT):
  118.             ball_vel[0] = - ball_vel[0]    + 1
  119.            
  120.         else:
  121.             ball_vel = [0, 0]
  122.             ball_pos = [WIDTH / 2, HEIGHT / 2]
  123.             player_right_score = player_right_score + 1
  124.            
  125.             helper()
  126.            
  127.      
  128.     # collide and reflect off of top of canvas
  129.     if ball_pos[1] <= BALL_RADIUS:
  130.         ball_vel[1] = - ball_vel[1]
  131.        
  132.     # collide and reflect off of bottom of canvas
  133.     if ball_pos[1] >= HEIGHT - BALL_RADIUS:
  134.         ball_vel[1] = - ball_vel[1]
  135.     # update ball
  136.            
  137.     # draw ball and scores
  138.     c.draw_circle(ball_pos, BALL_RADIUS, 2, 'black', 'black')
  139.        
  140. def keydown(key):
  141.  
  142.     global paddle1_vel, paddle2_vel
  143.     current_key = chr(key)
  144.    
  145.     if key == simplegui.KEY_MAP['down']: # right paddle up
  146.         paddle2_vel[1] = 0
  147.         paddle2_vel[1] = paddle2_vel[1] + 3
  148.     elif key == simplegui.KEY_MAP['up']: # right paddle down
  149.         paddle2_vel[1] = 0
  150.         paddle2_vel[1] = paddle2_vel[1] - 3
  151.    
  152.     elif key == simplegui.KEY_MAP['W']: #left paddle up
  153.         paddle1_vel[1] = 0
  154.         paddle1_vel[1] = paddle1_vel[1] - 3
  155.     elif key == simplegui.KEY_MAP['S']: # left paddle down
  156.         paddle1_vel[1] = 0
  157.         paddle1_vel[1] = paddle1_vel[1] + 3
  158.  
  159. def keyup(key):
  160.     global paddle1_vel, paddle2_vel
  161.     current_key = chr(key)
  162.     if key == simplegui.KEY_MAP['down']: # right paddle up
  163.         paddle2_vel[1] = 0
  164.     elif key == simplegui.KEY_MAP['up']: # right paddle down
  165.         paddle2_vel[1] = 0
  166.    
  167.     elif key == simplegui.KEY_MAP['W']: #left paddle up
  168.         paddle1_vel[1] = 0
  169.     elif key == simplegui.KEY_MAP['S']: # left paddle down
  170.         paddle1_vel[1] = 0
  171.  
  172.  
  173.  
  174. # create frame
  175. frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
  176. frame.set_draw_handler(draw)
  177. frame.set_keydown_handler(keydown)
  178. frame.set_keyup_handler(keyup)
  179. frame.add_button("Restart", init, 100)
  180. frame.add_button('start', helper, 100)
  181. frame.set_canvas_background('silver')
  182.  
  183. # start frame
  184.  
  185.  
  186. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment