Jump to content

Recommended Posts

Posted

Hi,

I have been trying to recreate the PiBuster game described in 'Raspberry Pi for Dummies' and have copied the code exactly out of the book but I keep getting this error:

"IndexError: list index out of range" when I run it. If anyone can throw some light on this (or has the complete working code) I would be grateful.

 

This is the line that it refers to (highlighted in bold) which is very near to the end of the code:

 

ballx+=ballxdir # This code changes the direction

if map[bally-1][ballx-1]==0: # of the ball depending

bally+=ballydir # on what it hits.

 

And this is the complete code:

 

# PiBuster Code

# PiBuster from Raspberry Pi for Dummies

 

 

import pygame, sys, time, random

from pygame.locals import *

 

 

pygame.init() # initialize pygame

 

 

# This section deals with setting up the game window

gameSurface=pygame.display.set_mode((440,480)) # playing area size (pixels - width,height)

pygame.display.set_caption('MrG-PiBuster') # Caption on window title bar

pygame.mouse.set_visible(0) # You won't see the mouse pointer in the game window

 

 

# This section deals with the colours used in the game (note: we have to use the American spelling of 'color' (boo! hiss!))

SHADOW = (192, 192, 192) # The numbers in the brackets are the RED, GREEN and BLUE (RGB) values for the colours

WHITE = (255, 255, 255) # Example: 255, 255, 255 is white, 0, 0, 0 is black, 200, 0, 0 is RED

LIGHTGREEN = (0, 255, 0) # In this game we will use the colours we have created

GREEN = (0, 200, 0) # here as it is easier than typing in the numbers

BLUE = (0, 0, 128)

LIGHTBLUE = (0, 0, 255)

RED = (200, 0, 0)

LIGHTRED = (255, 100, 100)

PURPLE = (102, 0, 102)

LIGHTPURPLE = (153, 0, 153)

 

 

gameSurface.fill(WHITE) # Turns the game surface white

pygame.display.update()

 

 

# This section deals with positioning the ball

def drawball(x,y):

pygame.draw.circle (gameSurface,BLUE,(realx(x)+10,realy(y)+10),10,0)

pygame.draw.circle (gameSurface,LIGHTBLUE,(realx(x)+6,realy(y)+6),2,0)

 

pygame.display.update()

 

 

# drawbat - Routine to draw the bat

def drawbat(x):

pygame.draw.rect (gameSurface,LIGHTPURPLE,(realx(x),realy(BATY),40,4))

pygame.draw.rect (gameSurface,PURPLE,(realx(x),realy(BATY)+4,40,6))

pygame.draw.rect (gameSurface,SHADOW,(realx(x),realy(BATY)+10,40,2))

pygame.draw.rect (gameSurface,SHADOW,(realx(x)+38,realy(BATY),2,12))

 

 

# This section deals with drawing shapes on the game surface

 

 

#pygame.draw.rect(gameSurface,PURPLE,(16,16,406,2))

#pygame.draw.rect(gameSurface,PURPLE,(16,16,2,440))

#pygame.draw.rect(gameSurface,PURPLE,(422,16,2,440))

 

 

# This section deals with drawing the bricks

def realx(x):

x=x*20

return x

 

 

def realy(y):

y=y*20

return y

 

def drawbrick(xcoord,ycoord,col):

 

 

if col==1:

boxcolor=GREEN

highlightcolor=LIGHTGREEN

else:

boxcolor=RED

highlightcolor=LIGHTRED

pygame.draw.rect (gameSurface,boxcolor,(realx(xcoord),realy(ycoord),20,20))

pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(ycoord),2,20))

pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(ycoord),20,2))

pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord)+18,realy(ycoord),2,20))

pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord),realy(ycoord)+18,20,2))

 

# This section deals with displaying 'End of game' type messages

def showtext(text):

fontObj=pygame.font.Font('freesansbold.ttf',64) # The font name and size.

textsurface=fontObj.render(text, True, PURPLE, WHITE) # PURPLE is the text colour, WHITE is the background color.

textRectObj=textsurface.get_rect() # Creates a rectangle to display the text.

textRectObj.center=(220,200) # Puts the rectangle at position X=220 and Y=200 (note the American spelling of 'center')

gameSurface.blit(textsurface,textRectObj) # Puts your text in the rectangle.

pygame.display.update()

 

# Now we can create some functions to tell the program what text we would like in the box.

def gameover():

showtext('GAME OVER')

time.sleep(8) # Show the message for 8 seconds.

engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.

 

def gamewon():

showtext('GAME WON!')

time.sleep(8) # Show the message for 8 seconds.

engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.

 

def endgame(): # The 'endgame' funtion.

pygame.quit()

sys.exit()

 

# This funtion checks the program to see if the player has won.

def havetheywon():

if score==brickcount: # Every time the player knocks out a brick the program checks

return True # the remaining bricks against the original number of bricks and adds one

else: # to the score and keeps going (return False) until they are all gone (return True).

return False

 

 

 

 

 

 

 

# This section deals with creating the game map

map=[

#--------0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9-- # There is a space between each number on this line

[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],

[0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0],

[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0],

[0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0],

[0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],

[0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

]

 

 

# This section checks the game map to see if there should be a brick there.

brickcount=0

for x in range(1,21):

for y in range(1,20):

if map[y-1][x-1]!=0:

drawbrick(x,y,map[y-1][x-1])

brickcount+=1

pygame.display.update()

 

 

 

 

# This section deals with positioning the bat and setting the players score to zero at the start of game.

#initialize bat and score

batx=random.randint(1,19) # Puts the bat in a random place at bottom of screen

BATY=20

drawbat(batx) # Draws the bat by calling the routine we created earlier called 'drawbat'

score=0 # Sets the score to zero

 

pygame.display.update()

 

 

def blank(x,y):

pygame.draw.rect (gameSurface,WHITE,(realx(x),realy(y),20,20)) # This draws a white square where the bat used to be

# before you moved it. Its used to make the ball move

# too and make bricks disappear.

 

def clearbat(x): # This routine clears the bat. Because the bat is 2 squares long we have to run it twice.

blank(x,BATY)

blank(x+1,BATY)

 

pygame.display.update()

 

 

# This section deals with randomly putting the ball in an empty area at the top of the screen.

ballx=0

bally=0

while map[bally-1][ballx-1]!=0 or ballx==0:

ballx=random.randint(1,20)

bally=random.randint(1,7)

ballxdir=1

ballydir=-1

drawball(ballx,bally)

 

 

# This section defines how fast the ball will travel.

FPS=20 # Frames per second = 20

fpsClock=pygame.time.Clock()

pygame.key.set_repeat(1,1) # This bit enables the key to be held down to move the bat without having to keep pressing it.

 

 

# This bit updates the screen and creates a slight delay

#(2.5 seconds in this case) before the game starts.

pygame.display.update()

time.sleep(2.5)

 

 

# This section is the main game loop, it checks for key presses,

# moves the bat if required, moves the ball, knocks out bricks,

# updates the screen, checks if a player has won.

# MAIN GAME LOOP

while True:

for event in pygame.event.get():

oldbat=batx

if event.type == QUIT:

endgame()

if event.type==KEYDOWN:

if event.key==K_RIGHT: # If the Right arrow key is pressed

batx=batx+1 # move the bat 1 space to the right.

elif event.key==K_LEFT: # If the Left arrow key is pressed

batx=batx-1 # move the bat 1 space to the left.

if batx==0 or batx==20: # This bit stops

batx=oldbat # the bat from

clearbat(oldbat) # going off

drawbat(batx) # the screen.

 

# This section deals with making the ball move.

oldballx=ballx

oldbally=bally

if ballx==1:

ballxdir=10

if ballxdir==20:

ballxdir=-1

if bally==1:

ballydir=1

# uncomment the two lines below for cheat mode.

# if bally==BATY-1:

# ballydir=-1

 

 

pygame.display.update()

if havetheywon()==True: # If the conditions to win the game have been met

gamewon() # then go to 'gamewon' function.

fpsClock.tick(FPS)

 

 

# This section deals with the ball hitting the bat.

if bally==BATY-1: # This is the

batleft=batx # code for bouncing

batright=batx+1 # the ball off the bat.

if ballx>=batleft and ballx<=batright:

ballydir=-1

if ballx+ballxdir>=batleft and ballx+ballxdir<=batright: # This code finds out

ballydir=-1 # if the ball has reached

if bally==BATY: # the same level as the bat,

gameover() # if so, the game is over.

 

ballx+=ballxdir # This code changes the direction

if map[bally-1][ballx-1]==0: # of the ball depending

bally+=ballydir # on what it hits.

 

# This section deals with the scoring.

if map[bally-1][ballx-1]!=0:

blank(ballx,bally)

map[bally-1][ballx-1]=0

bally=oldbally

ballx=oldballx

ballydir=-ballydir

if random.randint(1,10)>5 and map[bally+ballydir-1][ballx-ballxdir-1]==0:

ballxdir=-ballxdir

score+=1

 

blank(oldballx,oldbally)

drawball(ballx,bally)

Posted

As the error suggests, that line of code is trying to access an element in the array that is out of range i.e. it's trying to access an index higher/lower than the bounds of the array.

 

Try adding in a print statement just before the line that causes an error that will print the index it's trying to access, this may give you a clue as to where the error occurs (probably when the ball is at the edge of the screen)

 

If you repost the code, but wrap it in 'code' tags, you might have more luck getting help as the copy/paste above won't run at all due to lack of indenting

 

Wrap all your code in code tags (without the spaces between tags): [ code]Your code here [ /code]

Posted

Thanks, I will try the print statement. Meanwhile here is my code hopefully formatted properly:

 

My code in code tags :

 

[ code]# PiBuster Code# PiBuster from Raspberry Pi for Dummies

 

 

import pygame, sys, time, random

from pygame.locals import *

 

 

pygame.init() # initialize pygame

 

 

# This section deals with setting up the game window

gameSurface=pygame.display.set_mode((440,480)) # playing area size (pixels - width,height)

pygame.display.set_caption('MrG-PiBuster') # Caption on window title bar

pygame.mouse.set_visible(0) # You won't see the mouse pointer in the game window

 

 

# This section deals with the colours used in the game (note: we have to use the American spelling of 'color' (boo! hiss!))

SHADOW = (192, 192, 192) # The numbers in the brackets are the RED, GREEN and BLUE (RGB) values for the colours

WHITE = (255, 255, 255) # Example: 255, 255, 255 is white, 0, 0, 0 is black, 200, 0, 0 is RED

LIGHTGREEN = (0, 255, 0) # In this game we will use the colours we have created

GREEN = (0, 200, 0) # here as it is easier than typing in the numbers

BLUE = (0, 0, 128)

LIGHTBLUE = (0, 0, 255)

RED = (200, 0, 0)

LIGHTRED = (255, 100, 100)

PURPLE = (102, 0, 102)

LIGHTPURPLE = (153, 0, 153)

 

 

gameSurface.fill(WHITE) # Turns the game surface white

pygame.display.update()

 

 

# This section deals with positioning the ball

def drawball(x,y):

pygame.draw.circle (gameSurface,BLUE,(realx(x)+10,realy(y)+10),10,0)

pygame.draw.circle (gameSurface,LIGHTBLUE,(realx(x)+6,realy(y)+6),2,0)

 

pygame.display.update()

 

 

# drawbat - Routine to draw the bat

def drawbat(x):

pygame.draw.rect (gameSurface,LIGHTPURPLE,(realx(x),realy(BATY),40,4))

pygame.draw.rect (gameSurface,PURPLE,(realx(x),realy(BATY)+4,40,6))

pygame.draw.rect (gameSurface,SHADOW,(realx(x),realy(BATY)+10,40,2))

pygame.draw.rect (gameSurface,SHADOW,(realx(x)+38,realy(BATY),2,12))

 

 

# This section deals with drawing shapes on the game surface

 

 

#pygame.draw.rect(gameSurface,PURPLE,(16,16,406,2))

#pygame.draw.rect(gameSurface,PURPLE,(16,16,2,440))

#pygame.draw.rect(gameSurface,PURPLE,(422,16,2,440))

 

 

# This section deals with drawing the bricks

def realx(x):

x=x*20

return x

 

 

def realy(y):

y=y*20

return y

 

def drawbrick(xcoord,ycoord,col):

 

 

if col==1:

boxcolor=GREEN

highlightcolor=LIGHTGREEN

else:

boxcolor=RED

highlightcolor=LIGHTRED

pygame.draw.rect (gameSurface,boxcolor,(realx(xcoord),realy(ycoord),20,20))

pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(ycoord),2,20))

pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(ycoord),20,2))

pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord)+18,realy(ycoord),2,20))

pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord),realy(ycoord)+18,20,2))

 

# This section deals with displaying 'End of game' type messages

def showtext(text):

fontObj=pygame.font.Font('freesansbold.ttf',64) # The font name and size.

textsurface=fontObj.render(text, True, PURPLE, WHITE) # PURPLE is the text colour, WHITE is the background color.

textRectObj=textsurface.get_rect() # Creates a rectangle to display the text.

textRectObj.center=(220,200) # Puts the rectangle at position X=220 and Y=200 (note the American spelling of 'center')

gameSurface.blit(textsurface,textRectObj) # Puts your text in the rectangle.

pygame.display.update()

 

# Now we can create some functions to tell the program what text we would like in the box.

def gameover():

showtext('GAME OVER')

time.sleep(8) # Show the message for 8 seconds.

engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.

 

def gamewon():

showtext('GAME WON!')

time.sleep(8) # Show the message for 8 seconds.

engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.

 

def endgame(): # The 'endgame' funtion.

pygame.quit()

sys.exit()

 

# This funtion checks the program to see if the player has won.

def havetheywon():

if score==brickcount: # Every time the player knocks out a brick the program checks

return True # the remaining bricks against the original number of bricks and adds one

else: # to the score and keeps going (return False) until they are all gone (return True).

return False

 

 

 

 

 

 

 

# This section deals with creating the game map

map=[

#--------0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9-- # There is a space between each number on this line

[0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],

[0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0],

[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0],

[0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0],

[0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],

[0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],

[0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

]

 

 

# This section checks the game map to see if there should be a brick there.

brickcount=0

for x in range(1,21):

for y in range(1,20):

if map[y-1][x-1]!=0:

drawbrick(x,y,map[y-1][x-1])

brickcount+=1

pygame.display.update()

 

 

 

 

# This section deals with positioning the bat and setting the players score to zero at the start of game.

#initialize bat and score

batx=random.randint(1,19) # Puts the bat in a random place at bottom of screen

BATY=20

drawbat(batx) # Draws the bat by calling the routine we created earlier called 'drawbat'

score=0 # Sets the score to zero

 

pygame.display.update()

 

 

def blank(x,y):

pygame.draw.rect (gameSurface,WHITE,(realx(x),realy(y),20,20)) # This draws a white square where the bat used to be

# before you moved it. Its used to make the ball move

# too and make bricks disappear.

 

def clearbat(x): # This routine clears the bat. Because the bat is 2 squares long we have to run it twice.

blank(x,BATY)

blank(x+1,BATY)

 

pygame.display.update()

 

 

# This section deals with randomly putting the ball in an empty area at the top of the screen.

ballx=0

bally=0

while map[bally-1][ballx-1]!=0 or ballx==0:

ballx=random.randint(1,20)

bally=random.randint(1,7)

ballxdir=1

ballydir=-1

drawball(ballx,bally)

 

 

# This section defines how fast the ball will travel.

FPS=20 # Frames per second = 20

fpsClock=pygame.time.Clock()

pygame.key.set_repeat(1,1) # This bit enables the key to be held down to move the bat without having to keep pressing it.

 

 

# This bit updates the screen and creates a slight delay

#(2.5 seconds in this case) before the game starts.

pygame.display.update()

time.sleep(2.5)

 

 

# This section is the main game loop, it checks for key presses,

# moves the bat if required, moves the ball, knocks out bricks,

# updates the screen, checks if a player has won.

# MAIN GAME LOOP

while True:

for event in pygame.event.get():

oldbat=batx

if event.type == QUIT:

endgame()

if event.type==KEYDOWN:

if event.key==K_RIGHT: # If the Right arrow key is pressed

batx=batx+1 # move the bat 1 space to the right.

elif event.key==K_LEFT: # If the Left arrow key is pressed

batx=batx-1 # move the bat 1 space to the left.

if batx==0 or batx==20: # This bit stops

batx=oldbat # the bat from

clearbat(oldbat) # going off

drawbat(batx) # the screen.

 

# This section deals with making the ball move.

oldballx=ballx

oldbally=bally

if ballx==1:

ballxdir=10

if ballxdir==20:

ballxdir=-1

if bally==1:

ballydir=1

# uncomment the two lines below for cheat mode.

# if bally==BATY-1:

# ballydir=-1

 

 

pygame.display.update()

if havetheywon()==True: # If the conditions to win the game have been met

gamewon() # then go to 'gamewon' function.

fpsClock.tick(FPS)

 

 

# This section deals with the ball hitting the bat.

if bally==BATY-1: # This is the

batleft=batx # code for bouncing

batright=batx+1 # the ball off the bat.

if ballx>=batleft and ballx<=batright:

ballydir=-1

if ballx+ballxdir>=batleft and ballx+ballxdir<=batright: # This code finds out

ballydir=-1 # if the ball has reached

if bally==BATY: # the same level as the bat,

gameover() # if so, the game is over.

 

ballx+=ballxdir # This code changes the direction

if map[bally-1][ballx-1]==0: # of the ball depending

bally+=ballydir # on what it hits.

 

# This section deals with the scoring.

if map[bally-1][ballx-1]!=0:

blank(ballx,bally)

map[bally-1][ballx-1]=0

bally=oldbally

ballx=oldballx

ballydir=-ballydir

if random.randint(1,10)>5 and map[bally+ballydir-1][ballx-ballxdir-1]==0:

ballxdir=-ballxdir

score+=1

 

blank(oldballx,oldbally)

drawball(ballx,bally)

 

 

 

 

[ /code]

Posted
Oh, it came out the same...:eek:

 

Remove the space after the opening bracket in each code tag (I had to put them so that they wouldn't get parsed as code blocks by the forum)

Posted

Sorry, misread it....

 

[/color][color=#333333][font=Verdana]# PiBuster Code[/font][/color][color=#333333]# PiBuster from Raspberry Pi for Dummies[/color]


[color=#333333]import pygame, sys, time, random[/color]
[color=#333333]from pygame.locals import *[/color]


[color=#333333]pygame.init() # initialize pygame[/color]


[color=#333333]# This section deals with setting up the game window[/color]
[color=#333333]gameSurface=pygame.display.set_mode((440,480)) # playing area size (pixels - width,height)[/color]
[color=#333333]pygame.display.set_caption('MrG-PiBuster') # Caption on window title bar[/color]
[color=#333333]pygame.mouse.set_visible(0) # You won't see the mouse pointer in the game window[/color]


[color=#333333]# This section deals with the colours used in the game (note: we have to use the American spelling of 'color' (boo! hiss!))[/color]
[color=#333333]SHADOW = (192, 192, 192) # The numbers in the brackets are the RED, GREEN and BLUE (RGB) values for the colours[/color]
[color=#333333]WHITE = (255, 255, 255) # Example: 255, 255, 255 is white, 0, 0, 0 is black, 200, 0, 0 is RED[/color]
[color=#333333]LIGHTGREEN = (0, 255, 0) # In this game we will use the colours we have created [/color]
[color=#333333]GREEN = (0, 200, 0) # here as it is easier than typing in the numbers[/color]
[color=#333333]BLUE = (0, 0, 128)[/color]
[color=#333333]LIGHTBLUE = (0, 0, 255)[/color]
[color=#333333]RED = (200, 0, 0)[/color]
[color=#333333]LIGHTRED = (255, 100, 100)[/color]
[color=#333333]PURPLE = (102, 0, 102)[/color]
[color=#333333]LIGHTPURPLE = (153, 0, 153)[/color]


[color=#333333]gameSurface.fill(WHITE) # Turns the game surface white[/color]
[color=#333333]pygame.display.update()[/color]


[color=#333333]# This section deals with positioning the ball[/color]
[color=#333333]def drawball(x,y):[/color]
[color=#333333]pygame.draw.circle (gameSurface,BLUE,(realx(x)+10,realy(y)+10),10,0)[/color]
[color=#333333]pygame.draw.circle (gameSurface,LIGHTBLUE,(realx(x)+6,realy(y)+6),2,0 )[/color]

[color=#333333]pygame.display.update()[/color]


[color=#333333]# drawbat - Routine to draw the bat[/color]
[color=#333333]def drawbat(x):[/color]
[color=#333333]pygame.draw.rect (gameSurface,LIGHTPURPLE,(realx(x),realy(BATY),40, 4))[/color]
[color=#333333]pygame.draw.rect (gameSurface,PURPLE,(realx(x),realy(BATY)+4,40,6))[/color]
[color=#333333]pygame.draw.rect (gameSurface,SHADOW,(realx(x),realy(BATY)+10,40,2) )[/color]
[color=#333333]pygame.draw.rect (gameSurface,SHADOW,(realx(x)+38,realy(BATY),2,12) )[/color]


[color=#333333]# This section deals with drawing shapes on the game surface[/color]


[color=#333333]#pygame.draw.rect(gameSurface,PURPLE,(16,16,406,2) )[/color]
[color=#333333]#pygame.draw.rect(gameSurface,PURPLE,(16,16,2,440) )[/color]
[color=#333333]#pygame.draw.rect(gameSurface,PURPLE,(422,16,2,440 ))[/color]


[color=#333333]# This section deals with drawing the bricks[/color]
[color=#333333]def realx(x):[/color]
[color=#333333]x=x*20[/color]
[color=#333333]return x[/color]


[color=#333333]def realy(y):[/color]
[color=#333333]y=y*20[/color]
[color=#333333]return y[/color]

[color=#333333]def drawbrick(xcoord,ycoord,col):[/color]


[color=#333333]if col==1:[/color]
[color=#333333]boxcolor=GREEN[/color]
[color=#333333]highlightcolor=LIGHTGREEN[/color]
[color=#333333]else:[/color]
[color=#333333]boxcolor=RED[/color]
[color=#333333]highlightcolor=LIGHTRED[/color]
[color=#333333]pygame.draw.rect (gameSurface,boxcolor,(realx(xcoord),realy(ycoord) ,20,20))[/color]
[color=#333333]pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(y coord),2,20))[/color]
[color=#333333]pygame.draw.rect (gameSurface,highlightcolor,(realx(xcoord),realy(y coord),20,2))[/color]
[color=#333333]pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord)+18,realy(ycoord ),2,20))[/color]
[color=#333333]pygame.draw.rect (gameSurface,SHADOW,(realx(xcoord),realy(ycoord)+1 8,20,2))[/color]

[color=#333333]# This section deals with displaying 'End of game' type messages[/color]
[color=#333333]def showtext(text):[/color]
[color=#333333]fontObj=pygame.font.Font('freesansbold.ttf',64) # The font name and size.[/color]
[color=#333333]textsurface=fontObj.render(text, True, PURPLE, WHITE) # PURPLE is the text colour, WHITE is the background color.[/color]
[color=#333333]textRectObj=textsurface.get_rect() # Creates a rectangle to display the text.[/color]
[color=#333333]textRectObj.center=(220,200) # Puts the rectangle at position X=220 and Y=200 (note the American spelling of 'center')[/color]
[color=#333333]gameSurface.blit(textsurface,textRectObj) # Puts your text in the rectangle.[/color]
[color=#333333]pygame.display.update()[/color]

[color=#333333]# Now we can create some functions to tell the program what text we would like in the box.[/color]
[color=#333333]def gameover():[/color]
[color=#333333]showtext('GAME OVER')[/color]
[color=#333333]time.sleep(8) # Show the message for 8 seconds.[/color]
[color=#333333]engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.[/color]

[color=#333333]def gamewon():[/color]
[color=#333333]showtext('GAME WON!')[/color]
[color=#333333]time.sleep(8) # Show the message for 8 seconds.[/color]
[color=#333333]engame() # This bit runs the 'endgame' function & quits Pygame and closes the PiBuster window.[/color]

[color=#333333]def endgame(): # The 'endgame' funtion.[/color]
[color=#333333]pygame.quit()[/color]
[color=#333333]sys.exit()[/color]

[color=#333333]# This funtion checks the program to see if the player has won.[/color]
[color=#333333]def havetheywon():[/color]
[color=#333333]if score==brickcount: # Every time the player knocks out a brick the program checks[/color]
[color=#333333]return True # the remaining bricks against the original number of bricks and adds one[/color]
[color=#333333]else: # to the score and keeps going (return False) until they are all gone (return True).[/color]
[color=#333333]return False[/color]







[color=#333333]# This section deals with creating the game map[/color]
[color=#333333]map=[[/color]
[color=#333333]#--------0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9-- # There is a space between each number on this line[/color]
[color=#333333][0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0],[/color]
[color=#333333][0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0],[/color]
[color=#333333][0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],[/color]
[color=#333333][0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333][0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[/color]
[color=#333333]][/color]


[color=#333333]# This section checks the game map to see if there should be a brick there.[/color]
[color=#333333]brickcount=0[/color]
[color=#333333]for x in range(1,21):[/color]
[color=#333333]for y in range(1,20):[/color]
[color=#333333]if map[y-1][x-1]!=0:[/color]
[color=#333333]drawbrick(x,y,map[y-1][x-1])[/color]
[color=#333333]brickcount+=1[/color]
[color=#333333]pygame.display.update()[/color]




[color=#333333]# This section deals with positioning the bat and setting the players score to zero at the start of game.[/color]
[color=#333333]#initialize bat and score[/color]
[color=#333333]batx=random.randint(1,19) # Puts the bat in a random place at bottom of screen[/color]
[color=#333333]BATY=20[/color]
[color=#333333]drawbat(batx) # Draws the bat by calling the routine we created earlier called 'drawbat'[/color]
[color=#333333]score=0 # Sets the score to zero[/color]

[color=#333333]pygame.display.update()[/color]


[color=#333333]def blank(x,y):[/color]
[color=#333333]pygame.draw.rect (gameSurface,WHITE,(realx(x),realy(y),20,20)) # This draws a white square where the bat used to be[/color]
[color=#333333]# before you moved it. Its used to make the ball move[/color]
[color=#333333]# too and make bricks disappear.[/color]

[color=#333333]def clearbat(x): # This routine clears the bat. Because the bat is 2 squares long we have to run it twice.[/color]
[color=#333333]blank(x,BATY)[/color]
[color=#333333]blank(x+1,BATY)[/color]

[color=#333333]pygame.display.update()[/color]


[color=#333333]# This section deals with randomly putting the ball in an empty area at the top of the screen.[/color]
[color=#333333]ballx=0[/color]
[color=#333333]bally=0[/color]
[color=#333333]while map[bally-1][ballx-1]!=0 or ballx==0:[/color]
[color=#333333]ballx=random.randint(1,20)[/color]
[color=#333333]bally=random.randint(1,7)[/color]
[color=#333333]ballxdir=1[/color]
[color=#333333]ballydir=-1[/color]
[color=#333333]drawball(ballx,bally)[/color]


[color=#333333]# This section defines how fast the ball will travel.[/color]
[color=#333333]FPS=20 # Frames per second = 20[/color]
[color=#333333]fpsClock=pygame.time.Clock()[/color]
[color=#333333]pygame.key.set_repeat(1,1) # This bit enables the key to be held down to move the bat without having to keep pressing it.[/color]


[color=#333333]# This bit updates the screen and creates a slight delay [/color]
[color=#333333]#(2.5 seconds in this case) before the game starts.[/color]
[color=#333333]pygame.display.update()[/color]
[color=#333333]time.sleep(2.5)[/color]


[color=#333333]# This section is the main game loop, it checks for key presses,[/color]
[color=#333333]# moves the bat if required, moves the ball, knocks out bricks,[/color]
[color=#333333]# updates the screen, checks if a player has won.[/color]
[color=#333333]# MAIN GAME LOOP[/color]
[color=#333333]while True:[/color]
[color=#333333]for event in pygame.event.get():[/color]
[color=#333333]oldbat=batx[/color]
[color=#333333]if event.type == QUIT:[/color]
[color=#333333]endgame()[/color]
[color=#333333]if event.type==KEYDOWN:[/color]
[color=#333333]if event.key==K_RIGHT: # If the Right arrow key is pressed[/color]
[color=#333333]batx=batx+1 # move the bat 1 space to the right.[/color]
[color=#333333]elif event.key==K_LEFT: # If the Left arrow key is pressed[/color]
[color=#333333]batx=batx-1 # move the bat 1 space to the left.[/color]
[color=#333333]if batx==0 or batx==20: # This bit stops[/color]
[color=#333333]batx=oldbat # the bat from[/color]
[color=#333333]clearbat(oldbat) # going off[/color]
[color=#333333]drawbat(batx) # the screen.[/color]

[color=#333333]# This section deals with making the ball move.[/color]
[color=#333333]oldballx=ballx[/color]
[color=#333333]oldbally=bally[/color]
[color=#333333]if ballx==1:[/color]
[color=#333333]ballxdir=10[/color]
[color=#333333]if ballxdir==20:[/color]
[color=#333333]ballxdir=-1[/color]
[color=#333333]if bally==1:[/color]
[color=#333333]ballydir=1[/color]
[color=#333333]# uncomment the two lines below for cheat mode.[/color]
[color=#333333]# if bally==BATY-1:[/color]
[color=#333333]# ballydir=-1[/color]


[color=#333333]pygame.display.update()[/color]
[color=#333333]if havetheywon()==True: # If the conditions to win the game have been met[/color]
[color=#333333]gamewon() # then go to 'gamewon' function.[/color]
[color=#333333]fpsClock.tick(FPS)[/color]


[color=#333333]# This section deals with the ball hitting the bat.[/color]
[color=#333333]if bally==BATY-1: # This is the [/color]
[color=#333333]batleft=batx # code for bouncing[/color]
[color=#333333]batright=batx+1 # the ball off the bat.[/color]
[color=#333333]if ballx>=batleft and ballx<=batright:[/color]
[color=#333333]ballydir=-1[/color]
[color=#333333]if ballx+ballxdir>=batleft and ballx+ballxdir<=batright: # This code finds out[/color]
[color=#333333]ballydir=-1 # if the ball has reached[/color]
[color=#333333]if bally==BATY: # the same level as the bat,[/color]
[color=#333333]gameover() # if so, the game is over.[/color]

[color=#333333]ballx+=ballxdir # This code changes the direction[/color]
[color=#333333]if map[bally-1][ballx-1]==0: # of the ball depending[/color]
[color=#333333]bally+=ballydir # on what it hits.[/color]

[color=#333333]# This section deals with the scoring.[/color]
[color=#333333]if map[bally-1][ballx-1]!=0:[/color]
[color=#333333]blank(ballx,bally)[/color]
[color=#333333]map[bally-1][ballx-1]=0[/color]
[color=#333333]bally=oldbally[/color]
[color=#333333]ballx=oldballx[/color]
[color=#333333]ballydir=-ballydir[/color]
[color=#333333]if random.randint(1,10)>5 and map[bally+ballydir-1][ballx-ballxdir-1]==0:[/color]
[color=#333333]ballxdir=-ballxdir[/color]
[color=#333333]score+=1[/color]

[color=#333333]blank(oldballx,oldbally)[/color]
[color=#333333]drawball(ballx,bally)[/color]




[color=#333333]
Posted
Mmmm...no, its properly formatted in Idle. I have copied and pasted this from my PC from Notepad++ where it is also properly formatted. Not sure what is happening here.:confused:
Posted
I think it must be the WYSIWYG editor on these forums, it doesn't know what to do with the tabs that get pasted into it. I've had similar problems with Wordpress's TinyMCE.
  • 2 years later...
Posted
I haven't tried making the pibuster game yet, but it looks very hard to get the final code without making a mistake. If anybody could send me the final code I would be very grateful.
Posted
I haven't tried making the pibuster game yet, but it looks very hard to get the final code without making a mistake. If anybody could send me the final code I would be very grateful.��

 

Hi,

the whole of the game is there in #6 above.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...