sexta-feira, maio 17, 2013

Advanced Python Class: "Pong Game" (no AI Engine)




Project Description/Specification

  1. Add code to the program template that draws a ball moving across the Pong table. We recommend that you add the positional update for the ball to the draw handler as shown in the second part of the "Motion" video.
  2. Add code to the function ball_init that spawns a ball in the middle of the table and assigns the ball a fixed velocity (for now). Ignore the parameter right for now.
  3. Add a call to ball_init in the function new_game which starts a game of Pong. Then add a call to new_game in the main body of your program. 
  4. Modify your code such that the ball collides with and bounces off of the top and bottom walls. Experiment with different hard-coded initial velocities to test your code.
  5. Add randomization to the velocity in ball_init(right) The velocity of the ball should be upwards and towards the right if right == True and upwards and towards the left if right == False. The exact values for the horizontal and vertical components of this velocity should be generated using random.randrange(). 
  6. For the horizontal velocity, we suggest a speed of around random.randrange(120, 240) pixels per second. For the vertical velocity, we suggest a speed of around random.randrange(60, 180) pixels per second. (You will need to set the signs of velocities appropriately.)
  7. Add code to the draw handler that tests whether the ball touches/collides with the left and right gutters. (Remember that the gutters are offset from the left and right edges of the canvas by the width of the paddle as described in the "Pong" video.) When the ball touches a gutter, use ball_init(right) to respawn the ball in the center of the table headed towards the opposite gutter.
  8. Next, add code that draws the left and right paddles in their respective gutters. The vertical positions of these two paddles should depend on two global variables.(In the template, the variables were paddle1_pos and paddle2_pos.)
  9. Add code that modifies the values of these vertical positions via an update in the draw handler.  The update should reference two global variables that contain the vertical velocities of the paddles. (In the template, the variables were paddle1_vel and paddle2_vel.)
  10. Update the values of these two vertical velocities using key handlers. The "w" and "s" keys should control the vertical velocity of the left paddle while the "Up arrow" and "Down arrow" key should control the velocity of the right paddle. 
  11. In our version of Pong, the left paddle moves up at a constant velocity if the "w" key is pressed and moves down at a constant velocity if the "s" is pressed and is motionless if neither is pressed. (The motion if both are pressed is up to you.) To achieve this effect, you will need to use both a keydown and a keyup handler to increase/decrease the vertical velocity in an appropriate manner.
  12. Restrict your paddles to stay entirely on the canvas by adding a check before you update the paddles' vertical positions in the draw handler. In particular, test whether the current update for a paddle's position will move part of the paddle off of the screen. If it does, don't allow the update.
  13. Modify your collision code for the left and right gutters in step 6 to check whether the ball is actually striking a paddle when it touches a gutter. If so, reflect the ball back into play. This collision model eliminates the possibility of the ball striking the edge of the paddle and greatly simplifies your collision/reflection code.
  14. To moderately increase the difficulty of your game, increase the velocity of the ball by 10% each time it strikes a paddle.
  15. Add scoring to the game as shown in the Pong video lecture. Each time the ball strikes the left or right gutter (but not a paddle), the opposite player receives a point and ball is respawned appropriately.
  16. Finally, add code to new_game which resets the score before calling ball_init. 
  17. Add a "Restart" button that calls new_game to reset the score and relaunch the ball.


import simplegui
import random

# initialize globals - pos and vel encode vertical info for paddles
# GLOBAL FRAME
WIDTH = 600 
HEIGHT = 400 
# GLOBAL BALL
BALL_RADIUS = 20
# helper function that spawns a ball, returns a position vector and a velocity vector
ball_pos = [WIDTH / 2, HEIGHT / 2]
ball_vel = [2, 1] 
# GLOBAL PADDLE
PAD_WIDTH = 8
PAD_HEIGHT = 80
PAD1_POS = HEIGHT / 2
PAD2_POS = HEIGHT / 2
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
paddle_vel = 5
paddle1_vel = 0
paddle2_vel = 0
# GLOBAL KEYS
keyup1 = False
keydown1 = False
keyup2 = False
keydown2 = False
# GLOBAL SCORE
score1 = 0
score2 = 0
      
#DEFINE EVENT HANDLERS:
#resets the game
def init(): 
    global PAD1_POS, PAD2_POS,HEIGHT, score1, score2  
    #reset the score:
    score1 = 0
    score2 = 0
    #reset the paddle positions:
    PAD1_POS = HEIGHT / 2
    PAD2_POS = HEIGHT / 2
    #reset the ball:
    ball_init(ball_vel[0] > 0)
    
# if right is True, move to the right, otherwise move to the left
def ball_init(right):
    # List-like vectors
    global ball_pos, ball_vel 
    global WIDTH
    global HEIGHT

    # puts ball back to center
    ball_pos = [WIDTH / 2, HEIGHT / 2]
    
    #new ball velocity
    ball_vel[0] = -random.randrange(120,240) / 100 

    if right == True :
        ball_vel[0] *= -1
    ball_vel[1] = -random.randrange(60, 180) / 100 
    
#draws things on the canvas
def draw(c): 
    global score1, score2, PAD1_POS, PAD2_POS,paddle1_vel,paddle2_vel,ball_pos, ball_vel, PAD1_POS, PAD2_POS
    
    # insert helper text on the screen -> keys to play with
    c.draw_text("Left Player keys: up = 'w', down = 's'", (80, 390), 12, "White", "serif")
    c.draw_text("Right Player keys: up = 'arrow up', down = 'arrow down'", (310, 390), 12, "White", "serif")
    
    # drawing - line and gutters
    c.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "Yellow")
    c.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "Yellow")
    c.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "Yellow")
    
    # drawing - paddles
    c.draw_polygon([[0, PAD1_POS], [PAD_WIDTH, PAD1_POS],[PAD_WIDTH, 
                                                          (PAD1_POS) + PAD_HEIGHT ],[0, (PAD1_POS) 
                                                          + PAD_HEIGHT]],1, "Yellow", "Yellow") 
    c.draw_polygon([[WIDTH, PAD2_POS], [WIDTH - PAD_WIDTH, PAD2_POS],
                    [WIDTH - PAD_WIDTH, PAD2_POS + PAD_HEIGHT], 
                    [WIDTH, PAD2_POS + PAD_HEIGHT]],1, "Yellow", "Yellow")
    
    # updating paddle's y's position as well as keeping paddle on the screen
    if (PAD1_POS <= HEIGHT - PAD_HEIGHT and paddle1_vel > 0) or (PAD1_POS >= 0 and paddle1_vel < 0) :
        PAD1_POS += paddle1_vel    
    elif (PAD2_POS <= HEIGHT - PAD_HEIGHT and paddle2_vel > 0) or (PAD2_POS >= 0 and paddle2_vel < 0) :
        PAD2_POS += paddle2_vel   
        
    # drawing - ball
    c.draw_circle(ball_pos, BALL_RADIUS, 2, "White", "Black")
    
    # update - ball
    ball_pos[0] += ball_vel[0]
    ball_pos[1] += ball_vel[1]
    
    if ball_pos[0] >= WIDTH - BALL_RADIUS - PAD_WIDTH or ball_pos[0] <= BALL_RADIUS + PAD_WIDTH :
        #ball bouncing off Paddle1 (PAD1)
        if PAD1_POS < ball_pos[1] < PAD1_POS + PAD_HEIGHT and ball_vel[0] < 0 : 
            ball_vel[0] *= -1.1
            ball_vel[1] *= 1.1
            
        #ball bounce off Paddle2 (PAD2)
        elif PAD2_POS < ball_pos[1] < PAD2_POS + PAD_HEIGHT and ball_vel[0] > 0 : 
            ball_vel[0] *= -1.1
            ball_vel[1] *= 1.1

        else :
            if ball_vel[0] > 0 :
                score1 += 1
            
            else :
                #if ball enters left gutter, score 1-point for side 2
                score2 += 1
            ball_init(ball_vel[0] < 0)
            
    # ball bounce off bottom of screen
    if ball_pos[1] >= HEIGHT - BALL_RADIUS :   
        ball_vel[1] *= -1

    #ball bounces off top of screen
    elif ball_pos[1] <= BALL_RADIUS : 
        ball_vel[1] *= -1 
    
    #drawing scores on the canvas
    c.draw_text(str(score1), [40, 50], 40, "Green")    
    c.draw_text(str(score2), [WIDTH - 80, 50], 40, "Green")  
    
#Decision Tree - what to do to take when a key is pressed
def keydown(key): 
    global paddle1_vel, paddle2_vel, PAD2_POS, PAD1_POS, keydown1, keydown2, keyup1, keyup2
   
    if key == simplegui.KEY_MAP["down"]:
        keydown2 = True
        paddle2_vel = paddle_vel  
        
    elif key == simplegui.KEY_MAP["up"]:
        keyup2 = True
        paddle2_vel = -paddle_vel  
        
    if key == simplegui.KEY_MAP["w"]:
        keyup1 = True
        paddle1_vel = -paddle_vel  
        
    elif key == simplegui.KEY_MAP["s"]:
        keydown1 = True
        paddle1_vel = paddle_vel      
        
#decision-tree - what to do when a key is released after being pressed
def keyup(key): 
    global paddle1_vel, paddle2_vel, keydown1, keydown2, keyup2, keyup1
    
    if key == simplegui.KEY_MAP["down"]:
        if keydown2 == True: 
            keydown2 = False
            paddle2_vel = 0
            
              
    elif key == simplegui.KEY_MAP["up"]:
        if keyup2 == True:
            keyup2 = False
            paddle2_vel = 0
            
    elif key == simplegui.KEY_MAP["w"]:
        if keyup1 == True:
            keyup1 = False
            paddle1_vel = 0
            
    elif key == simplegui.KEY_MAP["s"]:
        if keydown1 == True: 
           keyup1 = False
           paddle1_vel = 0

# create frame
frame = simplegui.create_frame("Pong-like Game @Manuel Augusto Antao - 17.05.2013", WIDTH, HEIGHT)
frame.set_canvas_background("Maroon")
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.add_button("Restart", init, 100)

# start frame
init()
frame.start()

NB: I can still remember the time when I was standing in front of a christmas tree and my parents said to look underneath to see whether I'd gotten something. After holding the present in my hand, my eyes would start to be shiny and happiness would go through my whole body... 

The feeling of having successfuly completed this class was something akin to that. 

Sem comentários: