Game Design: Walls
Game Design: Walls
In this chapter we are going to add some walls that BB8, troopers and bullets cannot go through.
We will be using this wall block for our wall sprite. The image is named wall.png and can be found in your Images folder that you downloaded from the original repository. It is 128 x 128 pixels but we are going to add a wall_scale constant at the beginning of our program to cut this in half and make it 64 x 64 pixels.
We are not actually going to create a Wall class because these are static sprites and no real need to constantly update them.
#159: Create a self.walls SpriteList() to hold walls that collide with things.
Before we start creating walls, we are going to do a little code clean up to be able to draw and update all of our sprites at the same time.
#160: Create an all_sprites SpriteList().
Now we need to go to all of the places in our code where we created a sprite and instead of just appending it to that particular list we also append it to the all_sprites list.
You might say, "How is that cleaning up code? We just added 6 extra lines?" The reason is because now we can go to the on_draw and on_update sections and instead of drawing and updating every single list, we can replace all of those lines with a single line of code.
If you remember, in the setup() section, we randomly drew the troopers in the left and right 1/3 of the window so they wouldn't spawn on our BB8 which is in the middle of the screen. We are going to draw some walls on our screen and we don't want the troopers to appear under the walls either so we will update our trooper code to spawn them in the left and right 1/4 of the screen.
#173: Half of the troopers spawn in the left 1/4 of the screen.
#176: Half of the troopers spawn in the right 1/4 of the screen.
In the setup() section we are going to learn how to draw walls with two different methods. First we are going to use a loop.
#182: Use a for loop in range(5). This will make 5 wall boxes.
#183: Create a wall sprite.
#184: Place the wall.center_x at 1/3 of the screen width.
#185: Place the wall.center_y at 1/4 of the screen height but then add on y*wall.width. This will add 64 px on to the center_y each time.
#186-187: Append each wall block to the walls and the all_sprites lists.
Go ahead and test your program. You should see a stack of 5 blocks on the center right but they won't collide with anything.
We are going to make a second wall with another method. First we will create a 2D-array to hold all of the coordinates of all our wall blocks.
#190-193: Create a variable called coordinate_list and add as many coordinates of blocks as you want.
#196: Use a for loop to loop through each coordinate.
#197: Create the wall block.
#198-199: Set wall.center_x to coordinate[0] and set wall.center_y to coordinate[1].
#200-201: Append each wall block to the walls and the all_sprites lists.
Run your program again to look at this new wall.
In the on_update section where game_running == True we need to see if BB8 runs into a wall.
#251: Create a bb8_hit_wall variable that collects wall blocks that BB8 runs into.
#252: Check to see if a wall block has been hit by seeing if the bb8_hit_wall list is greater than 0.
#253: If it is, make the speed of BB8 negative. This will make him bounce backwards when he hits a wall.
#254: We don't want to kill the wall block, we just want to remove it from the collision list. We can do this by calling clear() on the list.
In the on_update section where we are looping through each trooper to shoot a random bullet we also want to detect if the trooper is hitting a wall.
#268: Create a trooper_hit_wall variable that collects wall blocks that a trooper runs into.
#269: Check to see if a wall block has been hit by seeing if the trooper_hit_wall list is greater than 0.
#270-271: If it is, check to see if the center_x of the trooper is to the left or right of the wall block that it just hit. If it is change the trooper.dx to negative to make the trooper bounce back.
#272-273: Next, check to see if the center_y of the trooper is above or below the wall block that it just hit. If it is change the trooper.dy to negative to make the trooper bounce back.
#275: Again, we don't want to kill the wall block, we just want to remove it from the collision list. We can do this by calling clear() on the list.
In the on_update section we can add a section to check if an enemy bullet hits a wall block.
#286: Loop through the self.ebullets list.
#289: Create a ebullet_hit_wall variable that collects wall blocks that an enemy bullet runs into.
#290: Check to see if a wall block has been hit by seeing if the ebullet_hit_wall list is greater than 0.
#291: If it is, kill the ebullet.
In the on_update section we already have a section where we are looping through the bullets. In that loop we need to check if a bullet hits a wall block.
#297: Create a bullet_hit_wall variable that collects wall blocks that a bullet runs into.
#298: Check to see if a wall block has been hit by seeing if the bullet_hit_wall list is greater than 0.
#299: If it is, kill the bullet.
Finally, we will run the main function. We can change the game title to "BB8 Walls".
When you get this game to work, demonstrate it to your instructor. Feel free to implement any new knowledge into your game project.