Creo que no necesita más descripción que el título. Las dos palas se controlan con s y w, y las flechas de dirección arriba y abajo. ¡A pasar el rato!
Una vez más, toca ejecutarlo en CodeSkulptor por la librería gráfica propia de la plataforma.
Pong: Pulsa para ver/ocultar el código
# 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
LEFT = False
RIGHT = True
#position[x,y] and velocity[x,y] of ball
ball_pos=[0,0]
ball_vel=[0,0]
#position[x,y] and velocity[x,y] of paddles
paddle_pos=[HEIGHT/2,HEIGHT/2]
paddle_vel=[0,0]
#score[left,rigth]
score=[0,0]
# 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
ball_pos = [WIDTH/2, HEIGHT/2]
ball_vel[0]=random.randrange(2, 4);
ball_vel[1]=-1*random.randrange(1, 3);
if direction==LEFT:
ball_vel[0] =-1*ball_vel[0]
# define event handlers
def new_game():
global score, paddle_pos
score=[0,0]
paddle_pos=[HEIGHT/2,HEIGHT/2]
spawn_ball(LEFT)
#draw
def draw(c):
# 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")
# update ball
new_position_ball()
# draw ball
c.draw_circle(ball_pos,BALL_RADIUS,2,"White","White")
# update paddle's vertical position, keep paddle on the screen
modify_paddle(0)
modify_paddle(1)
# draw paddles
c.draw_polygon(rectangle_paddle(0,paddle_pos[0]), 1, "White", "White")
c.draw_polygon(rectangle_paddle(1,paddle_pos[1]), 1, "White", "White")
#check the colision
colision()
# draw scores
c.draw_text(str(score[0]),[WIDTH*(1/3.0),120],50, "Green")
c.draw_text(str(score[1]),[WIDTH*(2/3.0),120],50, "Green")
#increase and decrease the velocity of paddles
def keydown(key):
if key==simplegui.KEY_MAP["s"]:
paddle_vel[0]+=5
elif key==simplegui.KEY_MAP["w"]:
paddle_vel[0]-=5
elif key==simplegui.KEY_MAP["down"]:
paddle_vel[1]+=5
elif key==simplegui.KEY_MAP["up"]:
paddle_vel[1]-=5
def keyup(key):
if key==simplegui.KEY_MAP["s"]:
paddle_vel[0]-=5
elif key==simplegui.KEY_MAP["w"]:
paddle_vel[0]+=5
elif key==simplegui.KEY_MAP["down"]:
paddle_vel[1]-=5
elif key==simplegui.KEY_MAP["up"]:
paddle_vel[1]+=5
#check if ball have colision with the paddle or the edge
def colision():
#get the limits of paddle
rigth_limit=WIDTH-PAD_WIDTH-BALL_RADIUS
left_limit=PAD_WIDTH+BALL_RADIUS
#check if ball colide with the edge
if ball_pos[0]>=rigth_limit:
#if yes, check the position of the paddle to increase velocity or score
if colision_with_ball(paddle_pos[1])==1:
ball_vel[0]+=ball_vel[0]*0.10
ball_vel[1]+=ball_vel[1]*0.10
else:
score[0]+=1
spawn_ball(LEFT)
#check if ball colide with the edge
elif ball_pos[0]<= left_limit:
#if yes, check the position of the paddle to increase velocity or score
if colision_with_ball(paddle_pos[0])==1:
ball_vel[0]+=ball_vel[0]*0.10
ball_vel[1]+=ball_vel[1]*0.10
else:
score[1]+=1
spawn_ball(RIGHT)
#check if ball colides with the paddle
#return 1 if colides, 0 otherwise
def colision_with_ball(y):
if ball_pos[1]<=y+HALF_PAD_HEIGHT and ball_pos[1]>=y-HALF_PAD_HEIGHT:
return 1
else:
return 0
#move the paddle
def modify_paddle(paddle):
upperPosition=paddle_pos[paddle]+paddle_vel[paddle]-HALF_PAD_HEIGHT
downPosition=paddle_pos[paddle]+paddle_vel[paddle]+HALF_PAD_HEIGHT
if not(upperPosition<0 or downPosition>HEIGHT):
paddle_pos[paddle]+=paddle_vel[paddle]
#get the rectangle of paddle
def rectangle_paddle(position,y):
point = [[0,0],[0,0],[0,0],[0,0]]
side=0
if position>0:
side=WIDTH-PAD_WIDTH
point[0] = [side, y-HALF_PAD_HEIGHT]
point[1] = [side+PAD_WIDTH, y-HALF_PAD_HEIGHT]
point[2] = [side+PAD_WIDTH, y+HALF_PAD_HEIGHT]
point[3] = [side, y+HALF_PAD_HEIGHT]
return point
#update position ball
def new_position_ball():
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
under_limit=HEIGHT-BALL_RADIUS
upper_limit=BALL_RADIUS
rigth_limit=WIDTH-PAD_WIDTH-BALL_RADIUS
left_limit=PAD_WIDTH+BALL_RADIUS
if ball_pos[0]>=rigth_limit:
ball_vel[0]*=-1
ball_pos[0]=rigth_limit
elif ball_pos[0]<= left_limit:
ball_vel[0]*=-1
ball_pos[0]=left_limit
if ball_pos[1]>=under_limit:
ball_vel[1]*=-1
ball_pos[1]=under_limit
elif ball_pos[1]<=upper_limit:
ball_pos[1]=upper_limit
ball_vel[1]*=-1
# 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("Reset", new_game)
# start frame
new_game()
frame.start()

