You can see the pink walls here? They are actually multiple smaller rectangles drawn close together. How do we decide what is the pattern for the walls for adding multiple levels in the game?
Introducing the abstract data type (ADT) list. What are ADT’s? They are data types like “int” and “char” but are bigger bags that can hold many of them and can hold them of various types (the same bag can have “int”, “char”, objects etc) in python.
For now we will have a list of characters, and if the character is “W” it will draw the rectangle and if it is ” “, the place would be empty.
As we go through each item in the list we increase the “x” value and when we go to the next item, we increase the “y” value.
We start the wall at (0,200), that is why, in the below picture you can see the wall starting mid screen. Try to visualize the data structure levels – the first level of walls with the picture.
Read this for understanding nested lists.
Now to the code:
Create a nested list for 2 levels for example:
#Two levels - a list of lists levels = [ [ "WWWWWWWWWWWWWWWWWWWW", " ", " ", " ", " ", " ", " ", " ", " ", "W WWWWWWW", " ", " ", " ", " ", " ", "WWWWWWWWWWWWWWWWWWWW", ], [ "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", "W WWWWWWWWWWWWWWWWWWWWWWWWWWWWW W", "W W", "W W", "W WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W", "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", ] ]
Create a “Wall” class that stores the position of the wall rectangle at position (x,y) of size (30,30) and add it to the a list of such rectangles (walls).
walls =  RECTWIDTH = 30 RECTLENGTH = 30 class Wall(pygame.sprite.Sprite): def __init__(self, x, y): self.rect = pygame.Rect(x,y, RECTWIDTH,RECTLENGTH) walls.append(self)
Choose the first level. Coding and shifting to the next level is very much similar to changing to the game over screen as in Part 16. Take it up as a challenge.
level = levels
Create the wall objects(added automatically into the walls list since it is in the “__init__” of wall) as per the location specification given in the levels data structure.
WALLSTARTXPOS = 0 WALLSTARTYPOS = 200 x = WALLSTARTXPOS y = WALLSTARTYPOS for row in level: for col in row: if(col == "W"): wall = Wall(x,y) x+=10 y+=10 x=0
Draw the walls and rocket in the while loop. Changes in BOLD. The rectangle is each wall in the walls list is drawn. Only the walls (“W”) in the level are stored in the walls list.
SCREEN.fill(BLACK) #Drawing rectangles now #wall_group.draw(SCREEN) COLOR = (255,100, 150) #draws the rectangle in the levels for wall in walls: wallrect = pygame.draw.rect(SCREEN, COLOR, wall.rect) rocket.update(X,Y) rocket.draw_updatescreen()
Check for collision and update the screen. Draw a BLACK rectangle at the colliding rectangle position, so that it looks eaten up and remove that rectangle from the walls list.
for wall in walls: if(rocket.rect.colliderect(wall.rect)): print("collision : " + str(id(wall))) #Blacken the collided rectangle wallrect = pygame.draw.rect(SCREEN, BLACK, wall.rect) #remove it from the list walls.remove(wall) pygame.display.update()
This completes the tutorial. Yuppie!
You have reached here? Then you have learned all aspects of pygame and many python concepts.
Now you can try coding a full fledged game for further practice.
Thanks for following!