0% found this document useful (0 votes)
60 views106 pages

Ball On Fight Pixel

Uploaded by

pereirabrayan510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
60 views106 pages

Ball On Fight Pixel

Uploaded by

pereirabrayan510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 106
SETTING UP Welcome to the balloon fight course! We're going to create a balloon game where we pop balloons and traverse dungeons! SAME GUIDE | CHAPTER ONE First, we will need to create a new project in pixelPAD. Once you've logged in you want to: Click on the "MY PAD" button on the left, if you don’t see it click on the hamburger icon (three lines) next to pixelPAD. Then click on the blue button “Create App". Awindow should pop-up, type in your App Name. Feel free to name it any way you like. Here, we named it “Balloon Fight". Make sure the App Engine is "pixelpad2D". Click "Create" when you're done. Pinsload Once you created your project, you should see the PixelPAD Development Environment page. 1" THE PLAYER Let’s get started! Look under the "Class" section and you should only see one class there, the "Game" class. 2 First, Let's add the most important asset of all, our player! Create a new Class Enter the script name Press OK It should show up under Classes Classes Clore : Pay attention to the pink and purple Click on the Game Class, tabs! These tell you where to type in then click on the Start Tab your code. of your editor. Ne Add the following bolded code self.pixelhead = Player() Here we are simply creating an instance of the class "Player" and naming it"pixelhead”. ‘The self attributes the created object to the class this code is written in, in this case, the Game class. In plain english, this line would look like this: from the class Player. ixelhead' is my object ea: «2 Save and Play your game to see the changes ] 13 | CHAPTER TWO Hmm... That's weird, why is it just a blue box that says "empty image" and not a balloon fighter? Well that’s because this object doesn't have an image or “sprite” attached to it. Let’s attach one! First, we have to add the sprite to our project. Now let's add a sprite + Find and select a sprite for your Player & Pixelhead Float + Enter the sprite name It should show up under Sprites Notice how it automatically adds -png to the end of our space sprite. The .png is the file type. Note: If you are using a different sprite, it may be .jpg or something else. Make sure to pay attention to the type. Ss ° elereucee la) Now that we have the sprite added to our project, let’s assign it to our then click on the Start Tab ( Click on the Player Class, ) of your editor. is Notice that we type in "pixehead. png’, this is to call the sprite we just TaD aad created that has the same name. self.sprite = sprite(“pixelhead. png") Here we assign the sprite "pixelhead png” to our Player class. es: as | You should now notice that your player has a sprite on it now! 18 GAME GUIDE | CHAPTER TWO. of your editor. THE CARTESIAN COORDINATE SYSTEM Right now, our player is stuck in the middle of the screen. If we want to change where it goes, all we need to do is change it’s x and y values, When making 2D games, everything is on a grid, like a sheet of graph paper. X represents how many squares it goes to the left or right, and Y represents how many squares it goes up or down For an in- depth explanation, head to the Cartesian Coordinate System section of Glossary! For now, let’s experiment with placing our player around our scene. (Click on the Game Class, then click on the Start Talo The black bolded code is what you need to add while the is what you have already typed in before. This is to let you know where you need self.pixelhead = Player() self.pixelhead.x = 200 Here, we directly manipulate the x position of our player object and set itto 200. SAVE JB PLAY Save and Play your game to see the changes | Your player should have moved over to the right. I we changed that number from 200 to 400 it would move even further to the right! Try it out. See if you can get it to move right up to the edge of the screen. Great work! Can you guess how we'll make it move back over to the left? Here's a hint: we'll need numbers smaller than zero. That's right, negative numbers! Here you only have to change the umber for self pixelhead.x MGame BSc lad pam pr self.pixelhead = Player() self.pixelhead.x = -208 Here we set our player's x position to -200 instead of positive. es: as Save and Play your game to see the changes | Now your player should move to the left. Again, see if you can make your player touch the left edge of the screen without going too far. Now for moving up and down itis almost the exact same idea. The only thing different is instead of writing self.pixelhead.x we need to write self. pixelhead.y! self.pixelhead = Player() self.pixelhead.x = -200 self.pixelhead.y = 100 Here we set our player's y position to 100. es: as Save and Play your game to see the changes | See how it moves up? Try changing the number until you reach the top edge of the screen. And finally, ust like before, change it to a negative number and try to touch the bottom edge of the screen. Now if you change both self pixelhead.x and self.pixelhead.y, you can move your player to any part of the screen! Keep in mind that if you don't set the x ory positions of an object, it will be created on the position 0. ” GAME GUIDE | CHAPTER TWO SCALING ‘You might be thinking that our player looks too small (or too big) for our game. We can make this object smaller or bigger by changing the scale of this object. Every object has default scaleX and scaleY values set to 1. If we change the scaleX value, we will be changing the width of this object. I we change the scaleY, we will be changing the height of this object. Let's try it out! The X and ¥ are upper case for scaleX and scaleY. Make sure to pay attention to Upper and Lower cases. The code may not work if they are not typed self.pixelhead = Player() self.pixelhead.x = -200 self.pixelhead.y = 100 self.pixelhead.scaleXx 2.5 self.pixelhead.scaleY = @.5 Here we're simply changing our scaleX and scaleY values to 0.5. as: as Save and Play your game to see the changes | Notice how our player got smaller. Reducing scaleX and scaleY values in half makes our object reduce its size in half as well. Let's make our object twice as big as the original size. We just need to set scaleX and scaleY to 2. self.pixelhead = Player() self.pixelhead.x = -200 self.pixelhead.y = 10 self.pixelhead. scalex self.pixelhead.scaleY =2 =2 Here we're simply changing our scaleX and scaleY values to 2 a: - as Save and Play your game to see the changes | Now our player is twice as big since we doubled its scaleX and scaleY values. Go ahead and choose how big you want your player to be! For our example, we are going to leave the object at its original size by removing the scaling lines Here we're removing the lines that scale our player, so it can stay on its original size. + Save and Play your game to see the changes | ” SAME GUIDE | CHAPTER THREE 20 CHAPTER ADDING ENEMIES Our player on its own is pretty cool, but it isn’t much of a game if there is nothing else in the world. Let's add some new objects to decorate the place! Let's start again by creating a new class and selecting a sprite for our enemies! a Create a new Class Enter the script name It should show up under Classes Classes Cad Notice how it automatically adds png to the end of our space sprite, The .png is the file type. Note: If you are using a different sprite, it may be .jpg or something else. Make sure to pay attention to the type. 8 Now let's add a sprite Find and select a sprite for your Enemy Aa Virushead Float + Enter the sprite name Press OK It should show up under Sprites 2 GAME GUIDE | CHAPTER THREE self. pixelhead self. pixelhead self. pixelhead Player() -200 108 self.virushead = Enemy() Here we simply create a new enemy object in our game scene. a: - a Save and Play your game to see the changes | Just like before it's only a blue block that says "empty image". Remember how we fixed that last time? We added a sprite onto the class! (Click on the Enemy Class, then click on the Start Tab (of your editor. self.sprite = sprite("virushead.png”) Here we assign the "virushead. png sprite to the Enemy class. swe Save and Play your game to see the changes | Now the player should only move when you press down on the right arrow key. Try it out! Note on line 2, it's indented to the right! You can either use the "tab" key to tab over, or use 2 spaces Now that your enemy has a sprite, we can move it around and scale it up/ down, just like we did with the player. eed self. pixelhe: self. pixelhe: self. pixelhe: 108 self. virushe: self.virushead.x = 208 Here we are simply assigning an x position to the enemy. As we haven't assigned ay position, it stays in the y = 0 es: a Save and Play your game to see the changes Now that you have an enemy object with a sprite, change the numbers around to whatever you want to put it somewhere in the game. 23 1 CHAPTER THREE 24 ADDING BUBBLES Next, let's make some bubbles and put them all around our game! These bubbles will act as pickups for our player. 1 Create anew Class Enter the script name J Press OK It should show up under Classes Classes dowd Notice how it automatically adds -Png to the end of our space sprite, The .png is the file type. Note: If you are using a different sprite, it may be .jpg or something else, Make sure to pay attention to the type. 6 Now let's add a Ea] + Find and select a sprite for your Pickup Bubble + Enter the sprite name Press OK 0K It should show up under Sprites A A Ese) TT 25 Now, let's create a bubble in our Game class. self.pixelhead = Player() self.pixelhead.x = -200 self.pixelhead.y = 100 self.virushead = Enemy() self.virushead.x = 20 self-bubble = Pickup() self.bubble.x = -400 self.bubble.y = -200 Just like our player and our enemy, we create anew bubble object. Next, we move the bubble around by changing its x and y positions. EN + GY Save and Play your game to see the changes | You'll se the empty image sprite again, let's assign a sprite to the bubble object. (Tor clad self.sprite = sprite("bubble.png") Here we're simply adding the "bubble.png” sprite to the Pickup class. swe Save and Play your game to see the changes | = You should now be able to see your bubble in your scene. Feel free to add 5 more bubbles to your game. 3 = z 3 2 27 GAME GUIDE | CHAPTER THREE BACKGROUND We are now going to start working on our game scenario. First, we are going to add a background image to our game. Create a new Class can + name Background Press OK It should show up under Classes Classes SE) Notice how it automatically adds Png to the end of our space sprite. The .png is the file type. Note: If you ore using a different sprite, it may be .jpg or something else. Make sure to pay attention to the type. 6 Now let's add a sprite Ea] + Find and select a sprite for your Background Mountains Background + Enter the sprite name It should show up under Sprites a Oy ere 7 Se self.sprite = sprite("background. png") Here we're simply assigning the "background.png' to our background + Save and Play your game to see the changes | You'll see the empty image sprite again, let's assign a sprite to the bubble object, self.pixelhead = Player() self.pixelhead.x = -26 self.pixelhead.y = 108 self.virushead = Eneny() self.virushead.x = 268 self.bubble = Pickup() self.bubble.x = -40@ self.bubble.y = -200 self.mountains = Background() Here we're creating an object of our Background class inside the game and calling it "mountains" es: Save and Play your game to see the changes | GAME GUIDE | CHAPTER THREE Notice that all our game is gone and now we can only see our background That's because our background is being created on top of the rest of our game. Imagine a stack of paper sheets. The paper sheet at the bottom, under all other sheets, was added first than the others, while the one at the top was added last. In the same way, if we want our background to be displayed behind everything else in our game, we have to create its object first than anything else. Let's remove our background creation line from the bottom of the code and add it at the top. elf mountains = Background() self.pixelhead = Player() self.pixelhead.x = -200 self.pixelhead.y = 100 self.virushead = Enemy() self.virushead.x = 200 self.bubble = Pickup() self.bubble.x = -408 self.bubble.y = -200 seifmounteins—Beckground(} Here we're moving the background creation line from the bottom to the top of our code. re + Save and Play your game to see the changes | 04. CHAPTER PLAYER MOVEMENT We created a bunch of objects and placed them around the scene, but it's not much of a game if we can't interact with itat all. Next, we are going to add movement to our player! All the coding we have done up to this point has been in the “Start” Tab. Whatever code is typed there will happen only 1 time, when the game Starts. Today we'll be using the "Loop" Tab. Whatever code is in the Loop Tab will happen constantly, doing it over and over and over again For an in-depth explanation, head to The Game Loop of the Glossary. 4 Tr oop self.x = self.x +1 Here we are setting the x position of the player to its current position + 1 This will move your player to the right every frame. a: - a2 Save and Play your game to see the changes | You should see the player move right until they go all the way off the screen, Try changing the number! What happens when it's a higher or lower number? What if it’s negative? What if you replace x with y? Test these out, you should be able to get the player to move in any direction DETECTING KEYS ‘As {un as it is to watch our player run off in every direction, we want to have control over when he moves and which way. To de that, we are going to need an If statement. c ~\ Notice how "self.x = selfx +5" is indented? This is intentional. If you do not have an indent, you can press the Tab key on your keyboard to add it _) The crossed out code means you can remove it because ey we don’t need it anymore. Af key_is_pressed("D"): S self.x = self.x +5 2 Ee Here we have an if statement that checks if the D key is pressed on the g current frame of the game, ifitis, we use the movement code we had é from earlier. 8 We also increased our player’ speed by changing the number 1 for 5. 2 ea + GN ° Save and Play your game to see the changes | 2 When you press the D key your player should move right! You can change the speed of movement by increasing the value that we add to self.x by. On new lines, let's type our similar statements for each of the other directions. if key_is_pressed("D"): self.x = self.x +5 if key_is_pressed("A"): self.x = self.x - 5 if key_is_pressed("w") self.y = self.y + 5 if key_is_pressed("s"): self.y = self.y - 5 Here we have additional if statements that make the player move left, up and down. Notice that each if statement manipulates the position of our player in different ways, for example the "A" key will subtract from x coordinate, whereas the "W" key will add to y coordinate es: Save and Play your game to see the changes | Your player character should now be able to move in every direction using the WASD keys! You can change the numbers from 1 to something else if you want them to go faster or slower. We still have an issue with the direction our player is facing. It looks weird if they are moving right but still facing left. Luckily it’s very simple to mirror any sprite in PixelPAD. As you know, every object has scaleX and scaleY values. If we invert the scaleV value (positive to negative, or negative to positive), we flip the sprite vertically, and if we invert the scaleX value, we flip it horizontally. 33 GAME GUIDE | CHAPTER FOUR As we haven't changed our scaleX, we can flip the player sprite to the right, by setting its scaleX value to -1. If we set it back to 7, then it turns to the left again if key_is_pressed("D"): self.x = self.x +5 self.scalex = 1 if key_is_pressed(" self.x = self.x - 5 1 self.scalex = if key_is_pressed("W") self.y = self.y +5 if key_is_pressed(” self.y = self.y - 5 Here we are flipping the sprite to each of the directions the player can face ‘To do this we simply manipulated the scaleX value so it flips the sprite. Ee + GN Save and Play your game to see the changes | You should now notice that your player faces each direction properly when you move left or right. Ua CHAPTER COLLISIONS Next, we're going to implement collisions between the player and the enemy. First of all, what is a collision? Think of collision as checking whenever 2 objects touch. When you clap your hands together, they are colliding. For an in-depth explanation, head to the collision section of the Glossary. We're going to check if our player collides with the enemy object we added earlier, and if it does, then destroy our player. To check if two objects are colliding, we need to use the get_collision() function. This function will either return to us the object we've collided with, or False if we haven't collided with anything. 35 Tg ry if key_is_pressed("D") self.x = self.x +5 self.scalex if key_is_pressed("A") self.x = self.x - 5 self.scalex = -1 if key_is_pressed("W") self.y = self.y +5 if key_is_pressed("s") self.y = self.y - 5 if get_collision(self, "Enemy"): destroy(self) Here we have an if statement checking ifthe player is colliding with the enemy. If any object from the class Enemy has collided with the player, we destroy the player. Ee + Gs Save and Play your game to see the changes | Your player will now be destroyed whenever you collide with an enemy. Now what about our bubbles? Instead of destroying the player, we'll want to destroy the bubble we collide with GAME GUIDE | CHAPTER FIVE if key_is_pressed("D"): self.x = self.x +5 self.scalex = 1 if key_is_pressed("A"): self.x = self.x - 5 self.scalex = -1 if key_is_pressed("W"): self.y = self.y +5 if key_is_pressed("s"): self.y = self.y - 5 if get_collision(self, “Eneny"): destroy(self) bubbleTouch = get_collision(self, "Pickup") if bubbleTouch: destroy (bubbleTouch) Here we're checking for a collision between the player and a bubble just like our enemy, however we assign a variable named bubbleTouch to the same get collision code What this does is store the collided object in a variable called bubble, that way we can refer to it when we destroy it. Ifno bubble collides with the player, the bubbleTouch variable will be false, and nothing will be destroyed. es - a Save and Play your game to see the changes | You should now destroy bubbles whenever you collide with them! 7 i CHAPTER WEAK SPOTS Now, we'll be adding a weak point to our player and enemies, so we can fight against them, The first thing we'll need to do is add a new object for our enemy's weak point. In Balloon Fight, you defeat enemies by popping the balloons over their head. GAME GUIDE | CHAPTER SIX Now let’s add a sprite + Find and select a sprite for your Pickup Balloon Red + Enter the sprite name It should show up under Sprites Be See) FC kd self.sprite = sprite("balloon.pn; Here we have an if statement checking if the player is colliding with the enemy. If any object from the class Enemy has collided with the player, we destroy the player. SAVE FR PLAY ] 39 Luckily, other than the self sprite = sprite() line, we don’t need to write any code on this object. Everything it does will be done from the enemy and player objects since each of them will have their own balloon Now instead of creating a new instance of these balloons again and again in the room, we can just tell our player and enemies to always create their own balloon when they spawn. Toa meeoad self.sprite = sprite("pixelhead.png") se1f.myBalloon = Balloon() Here we create a Balloon object when our player is created, es: as | You'll notice that no matter where our player is, the balloon is created in the middle of the screen. To fix this, we just need to change their x and y coordinates to the current position of the player. Pe ry if key_is_pressed(" self.x = self.x +5 self. scalex if key_is_pressed("A") self.x = self.x - 5 self.scalex = -1 if key_is_pressed(" self.y = self if key_is_pressed(” self.y = self.y - 5 Af get_collision(self, “Eneny") destroy(self) . bubbleTouch = get_collision(self, “Pickup") 3 if bubbleTouch f destroy(bubbleTouch) 3 self.myBalloon.x = self.x . self.myBalloon.y = self.y 3 Here we're assigning the position of the balloon on every frame to the 2 current position of the player. & Be + Gy 40 | You may notice that your balloon is just covering up your player. We need to make it so that your balloon stays just above their heads. Luckily we can edit our player's loop tab. a Toi if key_is_pressed("D"): self.x = self.x +5 self.scalex if key_is pressed("A"): self.x = self.x - 5 self.scalex = -1 if key_is_pressed("W"): self.y = self.y +5 if key_is_pressed("s") self.y = self.y - 5 if get_collision(self, "Eneny") destroy(self) bubbleTouch = get_collision(self, “Pickup") if bubbleTouch destroy(bubbleTouch) self.myBalloon.x = self.x self.myBalloon.y = self.y + 88 Here we're offsetting the y position of the balloon every frame so that the balloon tracks slightly above the player. save | x & You should notice that the player's balloon is properly placed above their 3 head. Remember that these two lines of code should always stay at the . end of your code. Otherwise you might see a delay in repositioning your 8 balloon, 3 = $ a Now we need to add the functionality to the balloon so that when our ‘enemy hits it, it gets destroyed and so does the player. To do that, we are going to modify our get_collision code we had before. Instead of our player dying when it collides with an enemy, it will die when its balloon, collides with the enemy. TE if key_is_pressed("D"): self.x = self.x +5 self.scalex = 2 if key_is_pressed("A"): self.x = self.x - 5 self.scalex = -1 if key_is_pressed("w"): self.y = self.y +5 if key_is_pressed("s"): self.y = self.y - 5 if get_collision(sel#.myBalloon, Enemy"): destroy(self.my8alloon) destroy(self) bubbleTouch = get_collision(self, "Pickup") if bubbleTouch: destroy (bubbleTouch) self.myBalloon.x = self.x self.myBalloon.y = self.y + 80 Here we modify our get_collision code to check collisions between the player's balloon and an enemy. Then, before destroying the player we need to destroy the balloon, otherwise the balloon will stay in the game when our player gets destroyed. es - a Jj 43 Now whenever an enemy touches the player's balloon, the balloon and the player get destroyed! Now that our player already has its weak spot, we can use the same logic to add it to our enemy as well! ae self.sprite = sprite("virushead. png") ‘self.myBalloon = Balloon() Here we create a Balloon object when our enemy is created. Ee + Gg J GAME GUIDE | CHAPTER SIX Een ai if get_collision(self.myBalloon, "Player"): destroy (self.myBalloon) destroy(self) self.myBalloon.x = self.x self.myBalloon.y = self.y + 80 Here, we first reposition the balloon to above the enemy Later, we check collisions between the enemy's balloon and the player. If that happens, we destroy the enemy's balloon and then destroy the enemy. es: ay J You should now be able to destroy your enemy by popping its balloon. 45 UE CHAPTER BUBBLE MOVEMENT Next we'll be adding movement to our enemies and our bubble objects. Let's start with bubbles because they are simpler. We just want them to move upwards. We can do this just like we did for our player in the beginning of this course. or rd self.y = self.y +1 Here we move the Pickup up every frame. Like before, you can change the number to change the speed S SAVE JEG PLAY . You'll notice that your bubbles now move upwards constantly. 3 2 3 3 46 ENEMY MOVEMENT Our enemy will be a litle trickier since we don't want it to just move in 1 direction forever. We're going to make it patrol up and down like its flying, To do this we'll need a couple of new variables. od self.sprite = sprite(virushead. png") self.myBalloon = Balloon() self.timer = @ self.floating = “up* Here we're creating a timer to count how much time has elapsed. Next, we create a variable that determines what direction the enemy will float in. + | The timer variable will let us do something after a certain amount of time has passed. In this case, it will be to change directions. The self floating variable will control whether our enemy goes up or down, and will change with the timer. To make our timer variable work, we need it to count up by 1 every single frame. We can do this in the enemy's loop tab. aa if get_collision(self.myBalloon, “Player"): destroy (self.myBalloon) destroy (self) self.timer = self.timer +1 self.x self.y + 80 self.myBalloon.x self.myBalloon.y Here we are adding one to the timer value every frame. Remember that PixelPAD runs at 60 frames a second. Meaning when our timer is 60, that means one second has passed For an in-depth explanation of framerate, head to The Game Loop section of the Glossary. Ei + GM 4a We can now use the timer to change direction after 1 second has passed. if get_collision(self.myBalloon, "Player") destroy(self.ayBalloon) destroy(self) self.timer = self.timer + 1 if self.timer >= 60: self.timer = @ if self.floating == "up": self.floating = “down elif self.floating == “dow self. floating = self myBalloon.x = self.x self.nyBalloon.y = self.y + 80 Here we are checking if the timer is equal to, or is greater than 60.|f tis, we first reset the timer variable back to 0. Then, we check if our floating direction is up or down. Depending on what direction the enemy is currently floating in, we change the direction of the enemy to the opposite direction, and then reset the timer to 0. Notice the elif for checking if our direction is down. This is short for else if This means the if statement for checking if setf floating is down only runs ifthe first if statement is false. This is useful because without the elif ifthe floating direction was up, we would setitto down. Immediately afer would be another if statement checking iffioating direction is down, setting the direction back to up. The elf makes sure we only run one of these conditions ata given frame. es + ey | You will notice that your enemy still does nothing even with changing directions. That's because we still need to add the code to actually move the enemy. GAME GUIDE | CHAPTER SEVEN ci a if get_collision(self.nyBalloon, “Player"): destroy(self.myBalloon) destroy(self) self.timer = self.timer +1 if self.timer >= 60: self.timer = @ if self.floating == "up": self. floating = elif self.floating self. floating = down’ ‘down: if self floating == self.y = self.y +1 elif self.floating == "down": self.y = self.y - 1 self.my8alloon.x self.my8alloon.y self.x self.y + 80 Here we simply check if our floating direction is up or down, and then, move our enemy up or down accordingly. es: «sp J Your enemy should now patrol up and down your scene, however it's still not moving sideways. Let’s go back to the start tab and create another variable to control whether the enemy should move left or right. GEnemy Start self.sprite = sprite("virushead. png") self.myBalloon = Balloon() self.timer = self. floating self.direction = "left" Here we're creating a variable to control which direction the enemy is moving in a - a | 49 Now, we will move the enemy depending on the direction variable. Note: The "..." is to indicate there is more code below/above, you do NOT need to add the dots. \ ra self.timer = self.timer + 1 if self.tiner >= 60: self.timer = @ if self. floating Pp" self. floating = “down” elif self.floating == "down": self. floating = "up" if self.floating == “up" self.y = self.y +1 elif self.floating == “down” self.y = self.y - 1 Af self.direction == "right" self.x = self.x +1 elif self.direction == “left self.x = self.x - 1 self.myBalloon.x self.myBalloon.y self.x self.y + 80 Here we simply check if our direction is right or lef, and then move our enemy right or left accordingly es + ay | GAME GUIDE | CHAPTER SEVEN x Notice that your enemy moves to the left but keeps facing right. Let's fix it by manipulating its scaleX variable depending on which direction the ‘enemy is moving in self.timer = self.tiner +1 if self.timer >= 60: self.timer = @ if self. floating = self. floating = elif self. floating self. floating = "down" if self floating == “up” self.y = self.y +1 elif self.floating == “down”: self.y = self.y - 1 if self.direction == “right” self.x = self.x +1 self.scalex = 1 elif self.direction self.x = self.x - 1 “left self.scalex = - self.myBalloon.x = self.x self.myBalloon.y = self.y + 80 Here we are flipping the sprite to each of the directions the enemy can face. To do this we simply manipulated the scaleX value so it flips the sprite es: as | 08. Ha WALLS AND FLOORS At this point we need to add some terrain to interact with so we aren't just floating in the endless sky. 0 Create a new Class 2. Create a new Class ‘| ‘| Enter the script name Enter the script name . It should show up under It should show up under Classes Classes 82 Find and select the sprite for your Floor Grass Ground + Enter the sprite name floor + Find and select a sprite for your Ceiling Rock Ceiling + Enter the sprite name It should show up under Sprites It should show up under Sprites ° Sprites GAME GUIDE | CHAPTER EIGHT Now let's add a w Find and select the sprite for your Wall Wall Left + Enter the sprite name wallLeft Find and select a sprite for your Wall Wall Right + Enter the sprite name wall It should show up under Sprites wall ag self.sprite = sprite("wallleft.png") | (Jaded self. sprite = sprite("floor.png") J Now that our classes are set up we can start adding them to our game! See self.mountains = Background() self.pixelhead = Player() self.pixelhead.x = -200 self.virushead = Enemy() self.virushead.x = 200 self.bubble = Pickup() self.bubble.x = -420 self.bubble.y = -200 self.ceiling = Floor() self.ceiling.y = 260 self.wallleft = Wal1() self.wallleft.x = -550 self.wallRight = Wall() self.wallRight.x = 558 self.floor = Floor() self.floor.y = -380 Here we're creating two objects from the Wall class, and posit ‘them to the left and right side of our game screen. Next, we also create two objects from the Floor class and position them. ‘on the top and bottom of our game screen. exe + ay | & = 2 Notice that although we've got the 4 objects in our game, the right wall and 3 the ceiling don't look right. We need to change the ceiling and right wall’s sprites for the ceiling.png 56 and wallRight.png sprites we added self.mountains = Background() self.pixelhead = Player() self.pixelhead.x = -200 self.virushead = Enemy() self.virushead.x = 288 self.bubble = Pickup() self.bubble.x = -402 self.bubble.y = -208 self.ceiling = Floor() self.ceiling.y = 268 self.ceiling.sprite = sprite("ceiling. png") self.wallleft = Wall() self.wallleft.x = -558 self.wallRight = Wall() self.wallRight.x = 550 self.wallRight sprite = sprite("wallRight.png") self.floor = Floor() self.floor.y = -380 Here we're creating two objects from the Wall class, and positioning them to the left and right side of our game screen. Next, we also create two objects from the Floor class and position them on the top and bottom of our game screen. es - as | 37 ENEMY BOUNCE You should now have all walls and floors looking nice in your game! Now that we have both walls set up, we can keep our enemy inside our game's boundaries by changing its direction when touching a wall ery self.timer = self.timer + 1 if self.timer >= 60: self.timer = @ if self floating == “up” self. floating = “down” elif self.floating == "down": self. floating = "up" up if self.floating == “up" self.y = self.y +1 elif self.floating == "down" self.y = self.y - 1 Af self.direction self.x self.scaleX = 1 Af get_collision(self, “Wall”): self.direction = “left” elif self.direction eft": self.x = self.x - 1 self. scalex = -1 if get_collision(self, “Wall”): self.direction = “right” self.myBalloon.x = self.x self.myBalloon.y = self.y + 80 Here we're changing our enemy's direction when it collides with a wall E IFitis moving left and touches a wall, it changes it’s direction to right. Ifit 3 is moving right and touches a wall, it changes it's direction to lett. & SAVE JE PLAY J 3 = 2 & se Your enemy should now be bouncing on walls! ay SPAWNERS Next we will be adding spawner objects to create new challenges and wards for our player. We will also use random numbers to add some unpredictability to where and when they will spawn, The first thing we'll need to do is add a new class to serve as our spawner, Create a new Class Enter the script name It should show up under Classes foro Ee self.wallRight = Wall() self.wallRight.x = 550 self.wallRight. sprite = sprite("wallRight.png") self. floor = Floor() self.floor.y = -38@ self.bubbleCreator = Spawner() Here we are creating our Spawner object in our game. es: | You should now be able to see your bubbleCreator object in the middle of the screen. We actually don’t even need a sprite this time since this object will be invisible. We do, however, need to set it as invisible, and create a timer variable to add a delay between spawning objects. 6 GAME GUIDE | CHAPTER NINE a med self.visible = False self.timer = 0 Here we are creating our Spawner object in our game. EN + GY | i self.timer = self.timer + 1 if self.timer >= 120: bubble = Pickup() bubble.x = @ bubble.y = -400 self.timer = @ ‘On every frame, we're going to add one to the timer variable. After that we have an if condition checking if our timer has exceeded 120 frames. 120 / 60 = 2. This means that we'll spawn anew bubble object every 2 seconds! Then, we simply set that newly created object's position to the position (0-400), which will make it spawn at the center bottom of the screen es - a | Notice that your spawner creates bubbles every 2 seconds, however they always spawn in the same place. Let’s change that by generating random numbers. RANDOMNESS To create random numbers, we first need to tell the computer that this object needs access to the random functions. To do this we need to import something called the random library. For an in-depth explanation of what a library is, head to the Python basics section of the Glossary. ee import random self.timer = self.timer +1 if self.timer >= 128 bubble = Pickup() bubble.x = @ bubble.y = -400 self.timer = @ Here we've imported the random library at the top of our code Its important to import all libraries at the top of code because if we Use an instruction that belongs to the library before we import it, the computer won't know where to look for that instruction! es : es | Next, we have to use our randomness to affect our bubbles’ positions. 63 GAME GUIDE | CHAPTER NINE aed import random self.timer = self.tiner + 1 if self.timer >= 120: bubble = Pickup() bubble.x = random. randint(-600, 600) bubble-y = -400 self.tiner = 2 Here we're using the random.randint() function to generate a random number between -600 and 600, and assigning that to our bubble's x position That will make our bubble to be spawned anywhere in the screen, from the leftto the right border. es: | You should now notice that your bubbles spawn all along the bottom of your screen! Right now, there isn’t much point collecting them, but later we can make them give the player score, increase their jump height, and more! As always you can edit the numbers in order to change how far the bubbles can be offset and how often they spawn CHAPTER HORIZONTAL ACCELERATION AND MOMENTUM We're now going to improve our player's mechanics. We are going to let our player accelerate and decelerate using math! First of all, it's important to understand the difference between velocity and acceleration. Velocity is the rate that we change position (movement), but acceleration is the rate that we change velocity. Essentially, it means that we can have a smooth transition from being stopped to being in motion Don't worry if that doesn’t make much sense right now, you can just follow along the step-by-step and get the same result, but it's good to start thinking about First, we need to store a velocity variable to our playe! 65 TP aod self.sprite = sprite("pixelhead. png") self.myBalloon = Balloon() self.velocityX = @ Here we simply create a velocityX variable. ea - as Jj Now instead of increasing or decreasing self.x when we press left or right, we need to increase or decrease its self velocityX, if key_is_pressed(" self.velocityx = self.velocityx + 1 selfix self S self.scalex = if key_is_pressed( self.velocityx - 1 self.velocityx sebfix—seltx 5 self.scaleX = if key_is_pressed("W"): self.y = self.y +5 if key_is_pressed("S"): self.y = self.y - 5 1 if get_collision(self.myBalloon, “Enemy") destroy(self.nyBalloon) destroy(self) bubbleTouch = get_collision(self, “Pickup") if bubbleTouch destroy(bubbleTouch) self.myBalloon.x = self.x z self.myBalloon.y = self.y + 80 E Here we remove the old lines of code that change our position directly, z and instead change our self velocityX variable. 8 SAVE Jean PLAY | 3 = z & 6 You'll notice that you aren't able to move. That's because we need to con- stantly apply the current velocityX to our x position at all times if key_is_pressed("D self.velocityx = self.velocityx + 1 self.scalex = 1 if key_is_pressed("A"): self.velocityx = self.velocityx - 1 self.scalex = -2 if key_is_pressed("w"): self.y = self.y +5 if key_is_pressed("s"): self.y = self.y - 5 if get_collision(self.myBalloon, "Eneny’ destroy (self.nyBal loon) destroy(sel#) bubbleTouch = get_collision(self, "Pickup") if bubbleTouch: destroy (bubbleTouch) self.x = self.x + self.velocityx self.myBalloon.x self.myBalloon.y self.x self.y + 80 Here we change the x position of the player depending on how much velocityX we have. This also means we have momentum and even if we stop holding down the right arrow key, our character will keep going onward. es - es | You'll notice your movement is more fluid, but it still has some issues. If you hold down one direction for long enough, you'll still go insanely fast There is no limit this way, the player could end up going so fast it breaks the game. To fix this, welll need to put a maximum velocity in place. GAME GUIDE | CHAPTER TEN TP aod self.sprite = sprite("pixelhead.png") self.myBalloon = Balloon() self.velocityx = @ self.maxVelocity = 8 Here we create a maxVelocity variable and set it to a value that will limit our speed es: a | Tp aD if get_collision(self.myBalloon, “Enemy") destroy(self.ny8alloon) destroy(self) bubbleTouch = get_collision(self, "Pickup") if bubbleTouch destroy(bubbleTouch) self.x = self.x + self.velocityx Af self.velocityx > self.maxvelocity: self.velocityx = self.maxVelocity elif self.velocityx < -self.maxvelocity: self.velocityx = -self.maxVelocity self.x self.y + 80 self.myBalloon.x self.myBalloon-y Here we are checking if the velocityX is greater than the maxVelocity, If your velocityX is greater, then we set velocityX to the maxVelocity. Alternatively, we check if the velocityX variable is lower than the negative value of maxVelocity, if tis, then we set velocityX to the negative value of maxVelocity. Ge: ay _I You'll notice you can now maintain momentum left or right using physics! VERTICAL ACCELERATION AND MOMENTUM Now we're going to store our up and down velocity as well @Player Start self.sprite = sprite(""pixelhead. png") self.nyBalloon = 8alloon() self.velocityx = @ self.maxvelocity = 8 self.velocityY = @ Like before, here we're storing velocity, except in the y direction this time (up/down). ES + GY J Now that we have our variable, we need to use it to change the position of ‘our player like we did for our horizontal movement. 69 GAME GUIDE | CHAPTER TEN Saar if key_is_pressed("D") self.velocityX = self.velocityX +1 self.x = self.x +5 self.scalex = 1 if key_is_pressed("A") self.velocityx = self.velocityx - 1 self.scalex = -1 if key_was_pressed("W"): self.velocityY = self.velocityY + 3 if get_collision(self.myBalloon, “Enemy”) destroy(self.nyBalloon) destroy(self) bubbleTouch = get_collision(self, “Pickup") if bubbleTouch destroy(bubbleTouch) self.x = self.x + self.velocityx if self.velocityX > self.maxVelocity: self.velocityx = self.maxVelocity elif self.velocityx < -self.maxVelocity self.velocityX = -self.maxVelocity self.y = self.y + self.velocityY Af self.velocityY > self.maxvelocity: self.velocityY = self.maxVelocity Af self.velocityY < -self.maxVelocit} self.velocityY = -self.maxVelocity self.myBalloon.x = self.x self.myBalloon.y = self.y + 80 Here, we are using key_was_pressed instead of key_is_pressed. This way, we only check if the key was pressed down the current frame. IFitis, then we add 3 to the velocityY of the player. In terms of gameplay, this means that every time we press the up key, we jolt upwards and then will slowly decelerate when we add gravity later. Inthe middle we assign the y position of the player using selfy + our current velocityY. At the bottom, we have two if statements to keep the player's velocityY within the maxVelocity. es: | You should now notice that you have y acceleration. However, we keep go- ing up! We need to add some gravity so that our player's balloon momen- tum slowly moves downward. GRAVITY Let's start by adding a variable that will store the gravityForce on our player. eames self.sprite = sprite("pixelhead. png") self.myBalloon = Balloon() self.velocityx = @ self.maxVelocity = 8 self.velocityY = @ self.gravity = 0.1 Here we created a variable that holds the value that gravity will take away from velocityY every frame. es: «2 | Atthe moment, we don’t make use of this gravity. Let’s add some code to Loop so that our velocityY decays over time. n Saar self.x = self.x + self.velocityx if self.velocityx > self.maxVelocity: self.velocityx = self.maxVelocity elif self.velocityx < -self.maxVelocity: self.velocityx = -self.maxVelocity self.y = self.y + self.velocityY self.velocityY = self.velocityY - self.gravity if self.velocityY > self.maxVelocity: self.velocityY = self.maxVelocity if self.velocityY < -self.maxVelocity: self.velocityY = -self.maxVelocity self myBalloon.x self.my8alloon.y self.x self.y + 80 ‘On every frame, this line will decrease the value of velocity’. Since we also have the other line we added earlier that assigns the y position of our player, this means that we will constantly have a force pushing our player downwards. However, by jumping we add to the velocityY value, making it possible to counteract gravity and jump! ea - as Jj You should now be affected by gravity. When we bounce on our bubble objects or on enemies’ balloons we should get a big boost in our velocity’. It should be pretty simple to do, we'll just need to add to our collision checks! GAME GUIDE | CHAPTER TEN Per Af get_collision(self.nyBalloon, “Eneny" ‘destroy(self.myBalloon) destroy(self) bubbleTouch = get_collision(self, "Pickup") if bubbleTouch: destroy (bubbleTouch) self.velocityY = 6 self.x = self.x + self.velocityx By simply assigning the velocity’ to the 6, every time we hita bubble we can bounce off! +a You should now be able to bounce off of bubbles, Let's add this mechanic to when we pop our enemies balloons as well! Ay if-_geticoltision{seif:myBattoon;—"Piayer4}> playerHit = get_collision(self.myBalloon, “Player") if playerhit playerHit.velocityy = 6 destroy(self.nyBalloon) destroy(self) self.timer = self.timer + 1 if self.timer >= 60: self.timer = 0 if self.floating == "up": self. floating = "down" elif self.floating == "down self. floating = "up" up Here we first change our if statement so we can get the player object who has collided with us. Then, ifthe collision happens, we change the velocityY inside our player to 6. es: | You should now be able to bounce off of bubbles as well as enemy balloons! Atthis point you should also be able to jump yet be affected by gravity! GAME GUIDE | CHAPTER TEN FRICTION Friction is basically just applying a small force in the opposite direct are currently moving in until we reach 0. For example, if our velocityX is at 5, we want friction to subtract from it every frame until it goes back down to 0. First let's create a new variable to hold onto the number we'll use for friction. 5 TP aod self.sprite = sprite("pixelhead. png") self.myBalloon = Balloon() self.velocityx = @ self.maxVelocity = 8 self.velocityy = @ self.gravity = @.1 self.friction = 0.1 Here we've created a simple variable to store the friction force. ea: «sp | Next, let's use this variable in Loop aaa if self.velocityY > self.maxVelocity: self. velocity’ = self.maxVelocity if self.velocityY < -self.maxVelocity: self.velocityY = -self.maxVelocity Af self.velocityx > 0: self.velocityx = self.velocityx - self.friction Af self.velocityx < 0: self.velocityx = self.velocityx + self.friction self.x self.y + 80 self.myBalloon.x self.myBalloon.y Here we are simply adding or subtracting frictionForce to our velocity depending on velocityX's current value Friction will always want to slow our player to a stop, when our velocity iso. SAVE JB PLAY | GAME GUIDE | CHAPTER ELEVEN You may notice that there’s a couple of small bugs here. If our velocity ever gets between 0 and 0.1, friction will sometimes still be applied even though we should be stopped. We can fix this by nullifying any velocity that is between -0.1 and 0.1 if self.velocityY > self.maxvelocity: self.velocityY = self.maxvelocity if self.velocityY < -self.maxVelocity self.velocityY = -self.maxVelocity if self.velocityx > @ self.velocityx = self.velocityx - self.friction if self.velocityx < @ self.velocityX = self.velocityX + self.friction if self.velocityx < self.friction and self.velocityx > “self. friction: self.velocityx = @ self.myBalloon.x self.myBalloon.y self.x self.y + 80 Here we are simply adding or subtracting frictionForce to our velocityX depending on velocityX’s current value Friction will always want to slow our player to a stop, when our velocityX is0. save JRE PLAY | Now your player should be correctly affected by friction in the left or right directions. ” BOUNCING OFF WALLS AND FLOORS Next, we're going to handle collisions between the player and the terrain. What we basically want to be doing is bouncing off of walls/floors ‘whenever we bump into them, We also want to maintain our current velocity, just push it in the opposite direction. For example, if we slam into a wall going right at top speed, we should bounce to the lett while still going at top speed. If we hit it very softly, only ata velocity of 1, we should go left with a velocity of 1 Player Loop bubbleTouch = get_collision(self, "Pickup") if bubbleTouch destroy(bubbleTouch) self.velocityy = 6 self.x = self.x + self.velocityx if get_collision(self,"Wwall") self.x = self.x - self.velocityx self.velocityx = -self.velocityx if self.velocityX > self.maxVelocity: self.velocityX = self.maxVelocity elif self.velocityx < -self.maxVelocity self.velocityX = -self.maxVelocity Here, right after we move the player, we are checking fora collision between the player and a wall IF a collision occurs, we move the player back and make its current velocityX its negated value. as - a J You should now be able to bounce off of walls! GAME GUIDE | CHAPTER ELEVEN Next, we are going to do the same for Floors, but changing the velocityY now. Soar self.y = self.y + self.velocityY if get_collision(self,"Floor"): self.y = self.y - self.velocityY self.velocityY = -self.velocityY self.velocityY = self.velocityY - self.gravity if self.velocityY > self.maxvelocity: self.velocityY = self.maxVelocity if self.velocityY < -self.maxVelocity self.velocityY = -self.maxVelocity Here, right after moving the player, we are checking ifthe play colliding with the floor or ceiling, If a collision occurs, we move the player back and make its current velocityY its negated value. eo as | Phew! That was a lot of new stuff to add but our player movement should now be 100% perfect. Ifyou want you can change some of the variables like friction, maxVelocity, and the rate that you add to your x and y velocities when you press the WASD keys. Don't make them too crazy or you might start going so fast you phase through the walls! 79 80 ROOMS All the way through the course we've just been adding new objects directly into the game script. This works just fine until we want to have multiple different screens, in which case we need to organize our objects into separate rooms. Today we'll be adding new levels so we can ease the player into more difficult content over time. Create a new Class feo OB b> Enter the room name Levelt It should show up under Classes We need to move over all of the stuff we did in the Game script into the new Levell room script. To do this: Open the [fcr Class and go to th Highlight all the code with your mouse, and copy it with Ctrl+C Start Fey Open the Class and go to the (Sena tab. Paste your code that you copied back in the Game class with Ctrl+V room_set(“Level1") es: as Here is what we have so far as an example. Your Level1 may look different depending on what you've added! Once you have pasted all the code into the Level1 room, you can delete all your previous code from the Game class’ start tab. Ifyou try to Play your game now, there will be nothing in the game! We need to tell the game which room to start in. This should now be the only line in the Game class’ Start Tab. It should look exactly like it did before. The only difference is now we can change which room we are in later on. For example, if we defeat all the enemies in the room, we can load the next level. a1 GAME GUIDE | CHAPTER TWELVE Let's add that feature now. We will need to know: > How many enemies are in a given room > How many enemies have been defeated so far Let's add a variable to track how many enemies are in a room. game.enemiesNunber = @ Poon_set("Level1") Here we are creating a variable that simply stores the number of Every enemy created will increase this number, and every enemy defeated will reduce it If this variable gets to 0, that means that there aren't enemies in the game anymore. Notice how we create this variable before setting our room, That's because we want to initialize all variables before the game starts, Otherwise some objects might not find those variables when trying to access them, es: ax | The next step is adding code to our enemies. Everytime an enemy is created, it should increase the enemiesNumber variable. self.sprite = sprite("virushead. png") self.myBalloon = Balloon() self.tiner = @ self floating P self.direction = "right" game.enemiesNumber = game.enemiesNumber + 1 Here, everytime an enemy is created it will increase the enemiesNumber variable inside our Game class in 1 es: | We now need that each enemy reduces the enemiesNumber variable in 1 when they get destroyed. Let's modify that get_collision code we have inside our enemy's loop tab. playerHit = get_collision(self.myBalloon, “Player") f playerHit game.enemiesNumber = game.enemiesNumber - 1 playerHit.veloc 6 destroy(self.myBalloon) destroy(self) self.timer if self.tiner self.timer = @ if self. floating self. floating elif self.floating == "down" self. floating = “up Here, everytime an enemy is destroyed it will decrease the enemiesNumber variable inside our Game class in 1 es: | Finally, we want our room to send us to a different room everytime our enemiesNumber variable gets to 0. First, we are going to need another room to move to, Create a new Class Enter the room namo Level2 It should show up under Classes eed @ Level2 83 GAME GUIDE | CHAPTER TWELVE Now let's make our Level1 room to set the Level2 room when there aren't any more enemies alive py e ers if game.enemiesNunber == @: ‘room_set("Level2") Here, we set the Level2 room if there aren't any more enemies alive in the current room, ex - au | > Go ahead and decorate your room with Walls and floors > Enemies > Bubble spawners > Whatever else you want! You can continue this infinitely! Make Level3, Level4, Level5 and so on and so forth. Try to make each one a little more challenging than the last. Maybe in some of the levels it's like a break room with only 1 enemy but a bunch of bubbles to pop. It’s up to you! Congratulations, you've finished PixelPAD’s Balloon Course! Congratulations on completing this course! You can always keep working ‘on your game. You just need to log into your PixelPAD account from any other computer connected to the internet! SHARING MY GAME Ifyou want to share your game with your friends, you just have to follow these simple steps: 1. Goto the MyPad Section MY CLASSROOMS EXPLORE LEARN FIC SIM GAME GUIDE | CHAPTER TWELVE 2. Find the game you want to send to your friends and click on the triangle beside the game's name. Then, click Play. a = | Terrario Pia rel Gest 4h Code lz Report 3. Now you just need to find this page's link to send to your friends. You can easily find it at the top of your browser window. Simply copy that link and send it to your friends. 4, Done! Your friends should now be able to play your game! a AUTON S ren The following activities are optional and should be added to your current game. Most of them can be added during your game's development, but some might require your game to be already completed. You can check the prerequisite chapters beside the activities to know if you are able or not to do it at the stage you are now in the course. # Prerequisite Activity 1 Chapter3 ‘Add hazard objects to your game. hazards are like enemies in that the player will be destroyed after touching one, but cannot be destroyed by the player; they can only be avoided What sprite you choose, where you put it- those are up to you 2 Chapter Keep working with the hazard you added in the previous activity > -Make it move up like a bubble (it can have different speed) > -Create a hazardSpawner that will keep spawning hazards at the bottom of your screen 3 Chapter 7 Keep working with the hazard you added in the previous activity > Add collisions between the Player and Hazard > Ifthe player collides with a hazard, the player gets destroyed 4 Chapter 12 ‘Add 5 Levels to your game. SAME GUIDE | EXTRA ACTIVITIES 5 6 Chapter 12 Chapter 12 ‘Add a "MainMenu room to your game > When you press Play, your game starts in that room > When you press the space key, the MainMenu room sets the Level room as the current room (starts the game) Add a "YouLose" room to your game > When your player gets destroyed, set the Youlose room as the current room > When you press the space key, the Youlose room sets the Level! room as the current room (restarts the game) 29 GLOSSARY WHAT IS PIXELPAD? PixelPAD is an online platform we will be using to create our own apps or games! The PixelPAD IDE is composed of 4 areas: ASSETS: Your assets are where you can add and access your classes and sprites. Classes are step-by-step instructions that are unique to the object. For example, the instructions for how your player moves will be different from the way your asteroid moves! Sprites is another word for image, and these images give your objects an appearance! CODE: In this section, you will write instructions for your game. To write your code, click within the black box and on the line you want to type on. To E make a new line, click the end of the previous line and then press "Enter" g on your keyboard. g > STAGE: The stage is where your game will show up after you write your % code and click Play (or Stop and then Play). Don't forget to click save after g you make changes to your code! 3 CONSOLE: Your console is where you will see messages when there are & errors in your code, and also where you can have messages from your game show up such as the score, or instructions on how to play your game. 90 SCRIPTS Scripts and Assets Two of the asset types, rooms and classes, are script assets. Script assets (or scripts) are assets that have code inside them. Sprites are not considered scripts, because they do not contain any code. Creating Scripts and Assets: To Create an asset, you start by clicking the + next to "Rooms", “Classes” or "Sprites" pinelpad i says Then type in any name you'd like. My particular convention looks like this: MainTown” -> room > "Player" -> player class > "background -> background sprite > “stepGrass" > steps sound effect Classes and rooms (scripts) should follow the "TitleCase" standard, where all words are capitalized. For sprites and sounds (assets) we use the “camelCase* standard, where the first word is lowercase, and every word that follows is capitalized. This isn’t necessary, but keeps your code neat and readable. The Game Class There is one class that always exists in every project: the game class. The purpose of the game class is to load all of the other assets in our project. The game class represents our entire game Pano eo eee Sounds 1 GAME GUIDE | GLOSSARY DEFAULT OBJECT PROPERTIES Sprite, scaleX and scaleY Every class inherits default properties when created in PixelPAD. The First of these few properties you should learn about are: sprite, scaleX, and .scaleY sprite is the image of the object. The value of .sprite is an image object which we will get to later. .scaleY takes a float between 0 and 1 and stretches the sprite of the object lengthwise. .sca/eX takes a float between 0 and 1 and stretches the sprite of the object widthwise. X, Y and Z Coordinates The position of an object is where the object is. In programming, we usually describe an object's position using a pair of numbers: its X coordinate and its ¥ coordinate. An object's X coordinate tells us where the object is horizontally (le and right), and its ¥ coordinate tells us where the object is vertically (up and down). [0,0] is the middle of the screen > x takes the value of the x position of the object. The higher .x’s, the farther to the right its. > y takes the value of the y position of the object. The higher -y is, the higher up the object is. > 2 takes the value of the z position of the object. The higher .zis, the closer to you the object is. DOT NOTATION AND SELF Dot Notation Dot notation is like an apostrophe s ('s) Like "Timmy's ball" or "Jimmy's shoes" The ‘stells you who you're talking about. In code instead of using apostrophes we use dots to talk about ownership. So when we say playerx we're really saying "player's x" Examples of Dot Notation > playerx > refers to the player's x value > playerscaleX-> refers to player's x scale (percentage) The "Self" Property Self refers to whichever class you are currently in. So if you're typing code inside the Spaceship class, saying "self" refers to the Spaceship class itself. & Examples of Self J> Code inside "Spaceship" self.x = 50 self.y = 30 self.scalex self.scaleY The code would make Spaceship move to the right by 5Opx, up by 30px, and reduce its image size in half. J COLLISIONS What Are Collisions? Think of collision as checking whenever two objects touch. In Mario, whenever he collides with a coin, it runs the code to add a score In PixelPAD, it's when the “bounding boxes" of sprites touch. This includes the transparent areas of the sprite as welll We will use collisions in our game to determine when our ship is hit by obstacles, when we've collected a power-up or health refill, and when we've managed to shoot down an asteroid. The get_collision Function When we want to check for a collision between two objects, we use an if statement combined with a special function called get_collision Here is an example of a get_collision function: We start with an ordinary if statement. For our condition, we specify get_collision We then write a pair of parentheses (0). Next, we write self. This specifies that we want to check for collisions against the current object. > Finally, we write “Asteroid”. This specifies the other class we want to check for collisions with. In this case, we are checking for collisions with any object created from the Asteroid class. 93 GAME GUIDE | GLOSSARY BOUNDING BOXES Sprite’s Bounds Every object has a bounding box, which is the rectangle that contains the object's entire sprite. The get_collision condition checks for overlaps between the bounding boxes of objects, not the actual sprites. This can sometimes create surprising results. Here is an example of two objects that don't look like they should be colliding, but do: ge And here they are again, with their bounding boxes shown: DESTROYING OBJECTS The destroy Function When two objects collide, we generally would like to destroy at least one of them. Destroying an object removes it from the game. When an object is destroyed, it no longer exists, and trying to use it could make your game behave strangely or crash Destroying an object is very simple. Here is an example of destroying the player object Itt oN@ er) THE START AND THE LOOP TABS Start and Loop Say you decide to go for a run. You put on your runners and then you run Running would be the loop because it repeats (one foot in front of the other), and putting runners on would be the start because it only happens in the beginning Similarly, in PixelPAD the "start" describes an instruction that only happens once, such as the starting position of a robot, Whereas the “loop” could describe its animation The Game Loop Loops exist in our day-to-day life. For example, you wake up, get ready, go to school, come back home, go to sleep and repeat these things every day! So looping is the act of repeating, In programming, loops describe instructions that repeat instead of having to code each instruction again and again! Loops can happen every day, or they can repeat a specific number of times For example, a programmer can code a robot to jump 100 times, or code the robot to keep jumping forever! Video games are built around a game loop. Specifically for PixelPAD, our game loop runs the code 60 times every second! The loop starts when we click the Play button, and stops when we click the Stop button. It goes around and around for as long as the game is playing, updating each of our objects a litle bit at a time. ‘Object Created Start Section Next Iteration of 95 SAME GUIDE | GLOSSARY %6 How Do We Use The Game Loop? When we write code for our objects, we can choose to place it in one of two sections: the Start Section or the Loop Section, Code placed in the Start Section is executed as soon as we create the object. Code placed in the Loop Section, however, is added to the game loop, which means it will be executed over and over until the game stops. CONDITIONALS Conditions So far, whenever we've written any code, all we've done is give the computer a list of commands to do one after the other. Using conditions, we can tell the computer to make a decision between doing one thing or another. If Statements The way we write conditions in our code is by using if statements. Here is an example of a simple if statement: > Start with the word if > Next, we write our condition. The condition of an if statement is a true or false question that we ask the computer to answer for us. In the above example, our condition is key_is_pressedi'D'), which is asking, "Is the D key being pressed?" > After the condition, we write a full colon (), and then make a new line. > Next, we indent our code, which means we start typing ita little bit further to the right than we normally would > Finally, we write the body of the if statement. If the condition of the if statement turns out to be true, then the computer will run whatever code ‘we put inside the body. In the above example, the body is selfx= selfx +1 INDENTATION IN PYTHON Indentation Indentation is when code is shifted to the right by adding at least two spaces to the left of the code. Indentation is important for two reasons > Code that is indented is considered to be part of the body of the if statement by the computer. As soon as we stop indenting the code, we are no longer inside of the if statement. > Indentation helps us visually see the structure of our program based on ‘the shape of our code. This helps us navigate our code and find bugs more easily For example ect) bulletL Serer Sierets phere i Bier ieee We can see clearly that the statements only run if the condition is met. E.g. the player presses the SPACE button Itis very important that all of the code in the same body be indented using the same number of spaces on every line. KEY PRESS Keyboard Input One kind of condition we can use is a keyboard check. Keyboard checks can be used to determine whether a keyboard key is being pressed or not. We can use keyboard checks to make things happen when the player presses or releases a keyboard key. Keyboard checks are done using the key_is_pressed function. Here is an example of using the key_is_pressed function: if key_is_pressed("W"): print("You are pressing the W key!") 7 GAME GUIDE | GLOSSARY ‘The code inside the apostrophes is the name of a keyboard key. Most keys are named the same as the letter or word on their keyboard key. A few keys have specific names: > The space bar's name is space. > The arrow keys are named arrowLeft, arrowRight, arrowUp, and arrowDown. > Ifyou have a return key, itis named enter COMPARISONS Comparisons If we have code like: x > 300, this is a specific kind of condition called a comparison. Comparisons are true/false questions we can ask the computer about pairs of numbers. There are six main kinds of comparisons, each with its own operator (special symbol). This table shows an example of each kind of comparison: Example Question Example Code Isx-smaller than y? xey Is x bigger than y? xy Is x smaller than or equal to y? xey Is xbigger than or equal to y? xoey Isxequal to y? xaey Isxnot equal to y? x ley There are other kinds of conditions, but comparisons are the kind that we will be using most often. Adding Boundaries Example We can add boundaries to our game using if statements and comparisons. In our Player class’ Loop Section, add this new code at the bottom The Left Boundary if self.x < -600: self.x = -620 The Right Boundary if self.x > 600: self. x = 620 The Top Boundary if self.y > 300: self.y = 300 The Bottom Boundary if self.y < -300: self.y = -300 COMMENTS, Explaining Code with Comments Computer code is complicated. That's why, a long time ago, some very smart programmers invented code comments. Comments are like little notes that you can leave for yourself in your programs. The computer completely ignores comments in your code. You can write whatever you want inside of a comment. How to Write a Comment You can write a comment by starting a line of code with a pound sign, which is the # symbol (you might call this symbol a hashtag). Here is an example of some well-commented code Comments are an extremely useful tool, and you should get in the habit of writing them. Comments help us remember what our code does, help others understand our code, and help us keep our code organized. 99 | Glossary 100 TYPES OF BAD COMMENTS Misleading Comments It's important to remember that comments are notes. The computer doesn’t read our comments when it's deciding what to do next. Because of this, comments can sometimes be inaccurate. We should always read the code, even if itis commented, to make sure it does what we think itis doing Here is an example of a misleading comment The comment says that this code makes the player move upwards, but when we read the code, it actually makes them move downwards. If we just, read the comment without checking it against the cade, we would have no idea why our game wasn’t working properly. Obvious Comments Another type of bad comment is an obvious comment. Obvious comments don’t add any meaningful information to your code; they usually just re- state what the code is saying in plain English. Here is an example of an obvious comment: Obvious comments clutter up our code and can slowly turn into misleading comments if we're not careful. fa comment doesn't add anything meaningful to our code, it’s best to just delete it Vague Comments Vague comments are comments that don't actually explain anything. Vague comments are usually written without much thought, or because the author of the comment was told to comment on their code. Here is an example of a vague comment: corr] CPUs Du asa eet

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy