First ever lines of code in Py. For testing go to cloud www.codesculptor.org and paste the code in canvas, then press run.Implementation of classic arcade game Pong - week 4 of 8, assignment #4

# Implementation of classic arcade game Pong
 
import simplegui
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
 
# helper function that spawns a ball by updating the 
# ball's position vector and velocity vector
# if right is True, the ball's velocity is upper right, else upper left
def ball_init(right):
    global ball_pos, ball_vel # these are vectors stored as lists
    pass
 
 
# define event handlers
 
def new_game():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are floats
    global score1, score2  # these are ints
    pass
 
def draw(c):
    global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel
 
    # update paddle's vertical position, keep paddle on the screen
 
    # draw mid line and gutters
    c.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
    c.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
    c.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")
 
    # draw paddles
 
    # update ball
 
    # draw ball and scores
 
def keydown(key):
    global paddle1_vel, paddle2_vel
    #current_key=chr(key)
    if key == simplegui.KEY_MAP["down"] and paddle1_vel<(HEIGHT - HALF_PAD_HEIGHT):
        #move right down
        paddle1_vel += 5
    if key == simplegui.KEY_MAP["up"] and (paddle1_vel - HALF_PAD_HEIGHT)>0:
        #move right up
        paddle1_vel -= 5
    if key == simplegui.KEY_MAP["s"] and paddle2_vel<(HEIGHT - HALF_PAD_HEIGHT):
        #move left down
        paddle2_vel += 5
    if key == simplegui.KEY_MAP["w"] and (paddle2_vel - HALF_PAD_HEIGHT)>0:
        #move left up
        paddle2_vel -= 5
 
 
def keyup(key):
    global paddle1_vel, paddle2_vel
    #current_key=''
 
 
# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
 
 
# start frame
frame.start()

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.