Advertisement
ansakoy

Pong

Oct 12th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 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. LEFT = False
  15. RIGHT = True
  16.  
  17. # initialize ball_pos and ball_vel for new bal in middle of table
  18. # if direction is RIGHT, the ball's velocity is upper right, else upper left
  19. def spawn_ball(direction):
  20.     global ball_pos, ball_vel # these are vectors stored as lists
  21.     ball_pos = [WIDTH/2, HEIGHT/2]
  22.     if direction:
  23.         ball_vel = [- (random.randrange(120, 240)) / 60.0, - (random.randrange(60, 180)) / 60.0]
  24.     else:
  25.         ball_vel = [random.randrange(120, 240) / 60.0, - (random.randrange(60, 180)) / 60.0]
  26.  
  27. # define event handlers
  28. def new_game():
  29.     global pad1_pos, pad2_pos, pad1_vel, pad2_vel, speed  # these are numbers
  30.     global score1, score2  # these are ints
  31.     pad1_pos = HEIGHT / 2 - HALF_PAD_HEIGHT
  32.     pad2_pos = HEIGHT / 2 - HALF_PAD_HEIGHT
  33.     pad1_vel = 0
  34.     pad2_vel = 0
  35.     score1 = 0
  36.     score2 = 0
  37.     spawn_ball(random.randrange(2)) # Use random to choose initial direction
  38.  
  39. def draw(canvas):
  40.     global score1, score2, pad1_pos, pad2_pos, ball_pos, ball_vel
  41.    
  42.     # draw scores
  43.     canvas.draw_text(str(score1), [WIDTH - WIDTH / 4, HEIGHT/3], 40, 'Yellow')
  44.     canvas.draw_text(str(score2), [WIDTH / 4, HEIGHT/3], 40, 'Yellow')
  45.  
  46.     # draw mid line and gutters
  47.     canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "Orange")
  48.     canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "Orange")
  49.     canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "Orange")
  50.  
  51.     # update ball
  52.     if ball_pos[0] <= PAD_WIDTH + BALL_RADIUS:
  53.         if (pad1_pos <= ball_pos[1]) and (pad1_pos + PAD_HEIGHT >= ball_pos[1]):
  54.             ball_vel[0] = - ball_vel[0]
  55.             ball_vel[0] =  ball_vel[0] + ball_vel[0]/ 10.0
  56.             ball_vel[1] = ball_vel[1] +  ball_vel[1]/ 10.0
  57.         else:
  58.             spawn_ball(0)
  59.             score1 += 1
  60.     elif ball_pos[0] >= WIDTH - PAD_WIDTH - BALL_RADIUS:
  61.         if (pad2_pos <= ball_pos[1]) and (pad2_pos + PAD_HEIGHT >= ball_pos[1]):
  62.             ball_vel[0] = - ball_vel[0]
  63.             ball_vel[0] = ball_vel[0] +  ball_vel[0]/ 10.0
  64.             ball_vel[1] = ball_vel[1] +  ball_vel[1]/ 10.0
  65.         else:
  66.             spawn_ball(1)
  67.             score2 += 1
  68.     elif (ball_pos[1] <= 0 + BALL_RADIUS) or (ball_pos[1] >= HEIGHT - BALL_RADIUS):
  69.         ball_vel[1] = - ball_vel[1]
  70.     ball_pos[0] += ball_vel[0]
  71.     ball_pos[1] += ball_vel[1]
  72.  
  73.     # draw ball
  74.     canvas.draw_circle(ball_pos, BALL_RADIUS, 1, 'Navy', 'Navy')
  75.  
  76.     # update paddle's vertical position, keep paddle on the screen
  77.     if 0 <= pad1_pos + pad1_vel <= HEIGHT - PAD_HEIGHT:
  78.         pad1_pos += pad1_vel
  79.     if 0 <= pad2_pos + pad2_vel <= HEIGHT - PAD_HEIGHT:
  80.         pad2_pos += pad2_vel
  81.  
  82.     # draw paddles
  83.     canvas.draw_line([HALF_PAD_WIDTH, pad1_pos], [HALF_PAD_WIDTH, pad1_pos + PAD_HEIGHT], PAD_WIDTH, 'Orange')
  84.     canvas.draw_line([WIDTH - HALF_PAD_WIDTH, pad2_pos], [WIDTH - HALF_PAD_WIDTH, pad2_pos + PAD_HEIGHT], PAD_WIDTH, 'Orange')
  85.  
  86. # define key input    
  87. def keydown(key):
  88.     global pad1_vel, pad2_vel
  89.     speed = 10
  90.     if key == simplegui.KEY_MAP['w']:
  91.         pad1_vel -= speed
  92.     elif key == simplegui.KEY_MAP['s']:
  93.         pad1_vel += speed
  94.     elif key == simplegui.KEY_MAP['up']:
  95.         pad2_vel -= speed
  96.     elif key == simplegui.KEY_MAP['down']:
  97.         pad2_vel += speed
  98.  
  99. def keyup(key):
  100.     global pad1_vel, pad2_vel
  101.     speed = 10
  102.     if key == simplegui.KEY_MAP['w']:
  103.         pad1_vel += speed
  104.     elif key == simplegui.KEY_MAP['s']:
  105.         pad1_vel -= speed
  106.     elif key == simplegui.KEY_MAP['up']:
  107.         pad2_vel += speed
  108.     elif key == simplegui.KEY_MAP['down']:
  109.         pad2_vel -= speed
  110.  
  111. # create frame, register handlers
  112. frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
  113. frame.set_canvas_background('Olive')
  114. frame.set_draw_handler(draw)
  115. frame.set_keydown_handler(keydown)
  116. frame.set_keyup_handler(keyup)
  117. frame.add_button('Reset', new_game)
  118.  
  119. # start frame
  120. new_game()
  121. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement