Trying to run this code: #Pong
#Myles Broomes#version of the classic game pong
#player controls a paddle and competes against an AI opponent
#the aim of the game is to use the paddle to deflect the ball and get it passed
the opponent, resulting in a point
#first to 10 points winsimport pygame,random,sys
from pygame import *pygame.init()WINWIDTH=640 #width of the window in pixels
WINHEIGHT=420 #height of the window in pixelsFPS=60 #frames per second#sound
object for the countdown before the game begins
timerSound=pygame.mixer.Sound('ding.wav')# r g b
BLACK=( 0, 0, 0)
WHITE=(255,255,255)
GREY= (131,139,134)def get_text(color,text,size,pos,underline=False):
"""Takes 4 parameters - colour, fontsize, an XY position and text - and
returns a text and surface object. """
fontObj=pygame.font.Font('freesansbold.ttf',size)
fontObj.set_underline(underline) surfObj=fontObj.render(text,True,color)
textObj=surfObj.get_rect()
textObj.center=(pos) return surfObj,textObjdef start_screen():
"""Wait for the player to be ready before starting the game. """
ready=False #player is not yet ready
fontColor=GREY while not ready: #stay in the loop until the player is
ready
DISPLAYSURF.fill(BLACK)
pongSurf,pongText=get_text(fontColor,'Pong!',45,(WINWIDTH/2,WINHEIGHT/2))
DISPLAYSURF.blit(pongSurf,pongText)
startSurf,startText=get_text(fontColor,'Click here to
start!',10,(WINWIDTH/2,WINHEIGHT/2+30))
DISPLAYSURF.blit(startSurf,startText)
devSurf,devText=get_text(WHITE,'By Myles Broomes',10,(WINWIDTH-50,WINHEIGHT-10))
DISPLAYSURF.blit(devSurf,devText)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONUP:
if pongText.collidepoint(event.pos):
ready=True if event.type==MOUSEMOTION:
if pongText.collidepoint(event.pos):
fontColor=WHITE
else:
fontColor=GREY pygame.display.update()
FPSCLOCK.tick(FPS) return #start the game when the player is ready
def run_game():
"""Runs through the game loop. """
#default parameters of the players paddle
padXStart=30
padXEnd=30 padYStart=WINHEIGHT/2-30
padYEnd=WINHEIGHT/2+30 padWidth=5 #default parameters for the
opponents paddle
AIpadXStart=WINWIDTH-30
AIpadXEnd=WINWIDTH-30 AIpadYStart=WINHEIGHT/2-30
AIpadYEnd=WINHEIGHT/2+30 #default parameters of the ball
ballSize=5
ballX=WINWIDTH/2
ballY=WINHEIGHT/2 #decides which way to move the paddle - up or down -
depending which key the player presses, None by default (paddle is stationary)
direction=None #randomise the direction that ball moves in
possDir=[-5,5] #possible directions for the ball to move down, up, left or
right
ballMoveX=random.choice(possDir)
ballMoveY=random.choice(possDir) #keeps track of the players score, 0 by
default
score=0 #keeps track of the opponents score, 0 by default
AIscore=0 #countdown from 3 until the beginning of the game
countdown=3
timer=0 #check if the ball travels into the area outside of the
opponents line of sight
blindspotA=None
blindspotB=None timerSound.play() #plays the sound at the beginning of
the countdown while countdown>0: #loop until the coundown is at 0
DISPLAYSURF.fill(BLACK) for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
timeSurf,timeText=get_text(WHITE,'%s'%countdown,40,(WINWIDTH/2,WINHEIGHT/2))
DISPLAYSURF.blit(timeSurf,timeText) if timer<1:
timer+=0.017
else:
countdown-=1
if countdown>0: #only plays the sound if the number is 1,2 or 3
timerSound.play() #plays the sound each time the countdown
decreases
timer=0 pygame.display.update()
FPSCLOCK.tick(FPS) while True:
DISPLAYSURF.fill(BLACK) #clear everything off the screen so it can be
redrawn for line in range(0,WINHEIGHT-20): #draw a stuttered line down
the middle of the screen
start=line*30
end=start+20
halfwayLine=pygame.draw.line(DISPLAYSURF,WHITE,(WINWIDTH/2,start),(WINWIDTH/2,end),2)
#display the players score in the top left corner of the screen
scoreSurf,scoreText=get_text(WHITE,'%s'%score,25,(WINWIDTH/2-40,20))
DISPLAYSURF.blit(scoreSurf,scoreText) #display the opponents
score in the top right corner of the screen
AIscoreSurf,AIscoreText=get_text(WHITE,'%s'%AIscore,25,(WINWIDTH/2+40,20))
DISPLAYSURF.blit(AIscoreSurf,AIscoreText)
#balls rect object
ballRect=(ballX, ballY, ballSize, ballSize)
#draw the players paddle
paddle=pygame.draw.line(DISPLAYSURF, WHITE, (padXStart,padYStart),
(padXEnd,padYEnd), padWidth) #draw the opponents paddle
AIpaddle=pygame.draw.line(DISPLAYSURF, WHITE,
(AIpadXStart,AIpadYStart), (AIpadXEnd,AIpadYEnd), padWidth) for event in
pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==KEYDOWN: #move the paddle up if the paddle presses
the up key and down for the down key
if event.key==K_UP:
direction='up'
elif event.key==K_DOWN:
direction='down' padLength=[] #reset the list on
each iteration of the game loop
AIpadLength=[] for i in range(int(padYStart),int(padYEnd),1):
#sequence containing the every X and Y value from the top of the paddle to the
bottom
addToList=i
padLength.append(addToList) for j in
range(int(AIpadYStart),int(AIpadYEnd),1):
addToList=j
AIpadLength.append(addToList) #draws the opponents line of
sight
sightLineA=pygame.Rect(AIpadXStart-210,AIpadYStart-150,210,188)
sightLineB=pygame.Rect(AIpadXStart-210,AIpadYStart+42,210,190)
#pygame.draw.rect(DISPLAYSURF,WHITE,sightLineA)
#pygame.draw.rect(DISPLAYSURF,WHITE,sightLineB) #draw the ball
ball=pygame.draw.rect(DISPLAYSURF,WHITE,ballRect)
if direction=='up': #move the paddle up
padYStart-=10
padYEnd-=10
elif direction=='down': #move the paddle down
padYStart+=10
padYEnd+=10 #test to see if the ball is in the opponents
line of sight
if sightLineA.colliderect(ballRect): #move the paddle up if the ball is
in the top half of the opponents line of sight
AIpadYStart-=10
AIpadYEnd-=10
elif sightLineB.colliderect(ballRect): #move the paddle down if the
ball is in the bottom half of the opponents line of sight
AIpadYStart+=10
AIpadYEnd+=10 try:
if blindspotA.colliderect(ballRect): #check if the ball is in the
opponents blindspot, if so move the paddle towards the blindspot
AIpadYStart-=10
AIpadYEnd-=10
elif blindspotB.colliderect(ballRect):
AIpadYStart+=10
AIpadYEnd+=10
except AttributeError:
None if AIpadYEnd>=WINHEIGHT-3: #draw a rect covering the
opponents blindspot at the top of the screen
blindspotA=pygame.Rect(AIpadXStart-210,2,210,130)
#pygame.draw.rect(DISPLAYSURF,WHITE,blindspotA)
elif AIpadYStart<=3: #draw a rect covering the opponents blindspot at
the bottom of the screen
blindspotB=pygame.Rect(AIpadXStart-210,WINHEIGHT-132,210,130)
#pygame.draw.rect(DISPLAYSURF,WHITE,blindspotB) if
padYStart<=0: #stop the paddle from going off the screen
padYStart=0
padYEnd=padYStart+60
elif padYEnd>=WINHEIGHT:
padYEnd=WINHEIGHT
padYStart=padYEnd-60 if AIpadYStart<=0: #stop the opponents
paddle from going off the screen
AIpadYStart=0
AIpadYEnd=AIpadYStart+60
elif AIpadYEnd>=WINHEIGHT:
AIpadYEnd=WINHEIGHT
AIpadYStart=AIpadYEnd-60 #move the ball back to the center
of the screen if it goes off the screen
if ballX>WINWIDTH:
ballMoveX=random.choice(possDir) #reset the direction that the ball
is moving in
ballMoveY=random.choice(possDir)
ballX=WINWIDTH/2
score+=1
elif ballX<0:
ballMoveX=random.choice(possDir)
ballMoveY=random.choice(possDir)
ballX=WINWIDTH/2
AIscore+=1 if ballX==padXStart and ballY in padLength:
#redirect the ball if it makes contact with the front of the players paddle
ballMoveX=-ballMoveX
elif ballX==AIpadXStart-5 and ballY in AIpadLength: #redirect the ball
if it makes contact with the front of the opponents paddle
ballMoveX=-ballMoveX if ballY<=5: #redirect the ball if it
touches the top edge
ballMoveY=-ballMoveY
elif ballY>=WINHEIGHT-5: #redirect the ball if it touches the bottom
edge
ballMoveY=-ballMoveY #move the ball
ballX+=ballMoveX
ballY+=ballMoveY if score==10 or AIscore==10:
end_game(score,AIscore)
return #go back to the start screen
pygame.display.update()
FPSCLOCK.tick(FPS)def end_game(player_score,opp_score):
"""Inform the player if they have won or lost. """
ready=False while not ready:
DISPLAYSURF.fill(BLACK)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONUP:
return
finalScoreSurf,finalScoreText=get_text(WHITE,'Final-Score:',40,(WINWIDTH/2,50),True)
DISPLAYSURF.blit(finalScoreSurf,finalScoreText)
youSurf,youText=get_text(WHITE,'%s'%player_score,100,(WINWIDTH/4,WINHEIGHT/2))
DISPLAYSURF.blit(youSurf,youText)
hyphenSurf,hyphenText=get_text(WHITE,'-',30,(WINWIDTH/2,WINHEIGHT/2))
DISPLAYSURF.blit(hyphenSurf,hyphenText)
oppSurf,oppText=get_text(WHITE,'%s'%opp_score,100,((WINWIDTH/2+WINWIDTH/4),WINHEIGHT/2))
DISPLAYSURF.blit(oppSurf,oppText)
continueSurf,continueText=get_text(WHITE,'Click to
restart!',10,(WINWIDTH/2,WINHEIGHT-40))
DISPLAYSURF.blit(continueSurf,continueText)
pygame.display.update()
FPSCLOCK.tick(FPS)def main():
"""Main game loop. """
global DISPLAYSURF,FPSCLOCK
DISPLAYSURF=pygame.display.set_mode((WINWIDTH,WINHEIGHT))
pygame.display.set_caption('Pong') FPSCLOCK=pygame.time.Clock()
while True:
start_screen()
run_game()#Play the game!
main()
Can anyone tell me why I keep getting this error: 'AttributeError: module
object has no attribute 'init'. I'm running Python 3.3.0 and Pygame 1.9.1 on
Windows.