rendered paste bodyimport pygame, sys
from pygame.locals import *
# game/window constants
SCREENWIDTH = 640
SCREENHEIGHT = 480
BALLSPEED = 4
# game specific variables
pscore = 0
cscore = 0
paused = 1
# init the pygame modules
pygame.init()
screen = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT))
pygame.display.set_caption("Pong clone")
clock = pygame.time.Clock()
# load all images
img_ball = pygame.image.load("ball.gif").convert()
img_player = pygame.image.load("player.gif").convert()
class Player(object):
def __init__(self,X,Y):
self.x = X
self.y = Y
def input(self,event):
if event.type == KEYDOWN:
if event.key == K_p:
if paused:
paused=0
else:
paused=1
def draw(self):
screen.blit(img_player, (self.x,self.y))
class Ball(object):
def __init__(self,X,Y):
self.x = X
self.y = Y
self.xvel = BALLSPEED
self.yvel = -BALLSPEED
def reset(self):
this.x = SCREENWIDTH/2
this.y = SCREENHEIGHT/2
paused = 1
def draw(self):
# bounce the ball if it collides with the upper/lower walls
if self.y+img_ball.get_height() > SCREENHEIGHT or self.y < 0:
self.yvel-=self.yvel*2
# if someone scores the ball
if self.x+img_ball.get_width() > SCREENWIDTH:
reset()
elif self.x < 0:
reset()
if not paused:
self.x+=self.xvel
self.y+=self.yvel
screen.blit(img_ball, (self.x,self.y))
# initialize ball object
ball = Ball(SCREENWIDTH/2, SCREENHEIGHT/2)
player = Player(20, SCREENHEIGHT/2)
while 1:
clock.tick(60)
# loop trough all events
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
player.input(event)
# fill background to black color
screen.fill((0,0,0))
# draw ball on the screen
ball.draw()
# update screen buffer
pygame.display.flip()