1972 Pong “python”

That feeling when, your program compiles and runs like it should…
So last week, was a pong game challenge, need I say more.
Here goes nothing.

#http://www.codeskulptor.org/#user38_5WtyGnsPgb_0.py
# Implementation of classic arcade game Pong
try:
    import simplegui
except:
    import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
    # To run simplegui in idle python, install SimpleGUICS2Pygame module
    # download module : https://pypi.python.org/pypi/SimpleGUICS2Pygame

import random

# initialize globals - pos and vel encode vertical info for paddles
WIDTH = 600
HEIGHT = 400       
BALL_RADIUS = 20
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
LEFT = False
RIGHT = True
paddle1_vel, paddle2_vel = 0,0

paddle1_pos = [WIDTH - HALF_PAD_WIDTH,  HEIGHT / 2]#,[WIDTH - PAD_WIDTH, HEIGHT]#[WIDTH / 2, HEIGHT / 2]
paddle2_pos = [HALF_PAD_WIDTH, HEIGHT / 2]

ball_pos = [WIDTH / 2, HEIGHT / 2]
# initialize ball_pos and ball_vel for new bal in middle of table
# if direction is RIGHT, the ball's velocity is upper right, else upper left
def spawn_ball(direction):
    global ball_pos, ball_vel # these are vectors stored as lists


# define event handlers
def new_game():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are numbers
    global score1, score2  # these are ints
   


 
def draw(canvas):
    global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, paddle1_vel, paddle2_vel
        
    # draw mid line and gutters
    canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
    canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
    canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")

    # update ball
            
    # draw ball
    canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "Red", "White")

    """Limit keeps the paddle on the screen"""
    if paddle1_pos[1] < HALF_PAD_HEIGHT:
        paddle1_pos[1] = HALF_PAD_HEIGHT
        paddle1_vel = 0
    elif paddle1_pos[1] > HEIGHT - HALF_PAD_HEIGHT:
        paddle1_pos[1] = HEIGHT - HALF_PAD_HEIGHT
        paddle1_vel = 0
    if paddle2_pos[1] < HALF_PAD_HEIGHT:
        paddle2_pos[1] = HALF_PAD_HEIGHT
        paddle2_vel = 0
    elif paddle2_pos[1] > HEIGHT - HALF_PAD_HEIGHT:
        paddle2_pos[1] = HEIGHT - HALF_PAD_HEIGHT
        paddle2_vel = 0
        
    """Drawing Paddles"""
    paddle1_pos[1] += paddle1_vel
    paddle2_pos[1] += paddle2_vel
    
    paddle1_top =  [paddle1_pos[0], paddle1_pos[1] - HALF_PAD_HEIGHT]
    paddle1_bot =  [paddle1_pos[0], paddle1_pos[1] + HALF_PAD_HEIGHT]
    canvas.draw_line(paddle1_top, paddle1_bot, PAD_WIDTH, "White")

    paddle2_top =  [paddle2_pos[0], paddle2_pos[1] - HALF_PAD_HEIGHT]
    paddle2_bot =  [paddle2_pos[0], paddle2_pos[1] + HALF_PAD_HEIGHT]
    canvas.draw_line(paddle2_top ,paddle2_bot, PAD_WIDTH, "White")

    # draw scores
        
def keydown(key):
    global paddle1_vel, paddle2_vel
    acc = 5
    if key == simplegui.KEY_MAP["up"]:
        paddle1_vel -= acc
    if key == simplegui.KEY_MAP["down"]:
        paddle1_vel += acc
    if key == simplegui.KEY_MAP["w"]:
        paddle2_vel -= acc 
    if key == simplegui.KEY_MAP["s"]:
        paddle2_vel += acc
   
def keyup(key):
    global paddle1_vel, paddle2_vel
    acc = 0
    if key == simplegui.KEY_MAP["up"]:
        paddle1_vel = acc
    if key == simplegui.KEY_MAP["down"]:
        paddle1_vel = acc
    if key == simplegui.KEY_MAP["w"]:
        paddle2_vel = acc 
    if key == simplegui.KEY_MAP["s"]:
        paddle2_vel = acc

def exit_button():
    frame.stop()

# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.add_button('Exit', exit_button, 50)

# start frame
new_game()
frame.start()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s