I thought programming would have been really hard, but this wasn’t



Yüklə 197,55 Kb.
səhifə10/10
tarix21.06.2018
ölçüsü197,55 Kb.
1   2   3   4   5   6   7   8   9   10

Challenges


Making use of what you have learnt so far, try and complete the following challenges in your game.

  • Add explosions when the player’s spaceship is hit by alien rockets, either using the same animation or a different one.

  • Add explosions when the player’s spaceship collides with an alien ship.

  • Add sound effects for when rockets are fired and when explosions occur.

  • If you have a device on which you can test the tilt function, try it out and alter the tilt scale until you are happy with how the spaceship moves.

  • Go through the code and add comments to explain the main parts.

  • Publish your script.

Activity 4c – Creating a Space Invaders clone


In our last activity for this section, we are going to re-create one of the all-time classic arcade games, space invaders.

  • Create a new “physics game starter” script and name it whatever you want, I called my version TouchInvaders, but feel free to be more creative with the name.

  • Add an art resource for the game background and call it “Space Background”. I would suggest the one shown below.





  • Add an art resource for the player controlled base / tank and name it “player tank”.



  • Go into the main action and make the following changes and additions to the code, which will setup the game board and declare a global sprite variable for the player’s base. It also creates a global number variable for the base speed, which we will use later on. It is optional whether you want to add the comments or not, although it wouldn’t hurt.



  • Create a new action called resetgame as shown below. Inside it we are creating 3 global numbers variables to hold the values for lives, score and the number of alien waves. We are also setting the initial position of the player’s base. Then we prompt the player for a YES / NO response as to whether they want to use tilt screen controls for the base, the result of this prompt is a Boolean TRUE or FALSE which will be stored into the global Boolean variable “Tilt on”. Lastly we re-post the board to the wall (this is necessary later on when we will allow the player to play again without quitting out).



  • Call this new action from the main action and remove the line

“board->post to wall” from the main action.

  • Add the following code into the gameloop, which will move the player’s base left or right if the player touches the game board. If the player touches to the left of the base it subtracts the base speed variable from the current base speed x value, which will alter the direction of the base to the left. If the player hasn’t touched to the left (therefore he must have touched to the right) it adds this value instead of subtracting to alter the direction to the right.



  • Add the following code to add tilt screen controls to the base, this is the same method we used in the previous activity. You may alter the scale value of 100 to change the movement speed. Note that the code to move the base only runs if the “Tilt on” variable is set to true, which is set in the resetgame action we created previously.



  • Extract both bits of code above which move the player base to an action called “move base” and make sure it is called from the gameloop.

  • Run the script and checks that the controls work. You will probably find that the base can move outside of the limits of the screen. Let’s fix this by adding in the following IF statements to stop it from going out by reversing the speed when it hits the sides. The reason I didn’t use obstacles at either side, as we did in the previous activity, is because it would cause problems with the aliens who are meant to move as one when any one of them hits the sides.



  • Extract the IF statements above into a separate action called “check limits” and call it from the gameloop.

  • Next let’s add in our aliens. We need to add multiple rows of aliens, a bit like we did with the bricks in our breakout clone. First of all declare a global sprite set in main, as shown below.



  • Next add an alien graphic into art resources and name it alien.







  • Create a new action naming it addinvaders; it will we be used to add and position the alien sprites in rows and columns. Add the parameters opposite to the action. The reason that we are passing the sprite set and game board in, which might seem a bit unnecessary, is because it means there are no direct calls to global variables from this action, which means it could be put into a library and used as a template for building space invaders or similar games which require rows of sprites. Actions that do not directly access global variables should be considered good practice and something you should do whenever possible.

  • Add the following code into the new addinvaders action we just created. It first clears all sprites from the sprite set and then adds the correct numbers of aliens, by adding one alien each time the code inside the 2 FOR loops (rows and columns) repeat. It sets the size (width) and initial horizontal speed of each sprite. It uses the variables we passed in along with the row and column numbers from the 2 FOR loops to space out the positions of the sprites. Then finally it adds the new sprite to the sprite set.



  • Go into the resetgame action and add the following line of code, which calls the new addinvaders action, passing in the correct parameters to set-up 4 rows with 10 columns of alien sprites. Feel free to change the values of any of the parameters to see what happens.

You will find that as you enter or change each parameter that a box appears below, with the name and variable type of the current parameter you are entering highlighted, this is so that you know what the parameter is for. For instance in the example below I am entering the value for firsty which is a number, this is the y position value for the first alien in the set and changing this effects the positions of all the aliens in the sprite set.







  • Add the following action which checks for the aliens hitting either side of the screen and then moves them all down a little when they do. It also changes the horizontal speed and therefore the direction that they are all moving in when this occurs. It is a little bit tricky to rename the local sprite variable inside a “for each” loop, it will default to sprite. However if you then use sprite within the “for each” loop you can then select it and select the RENAME button to change it. You will need to do this to set the variable names of alien and alien2 for the local sprite variables inside the 2 for each loops shown below.





  • Now we just need to call the above method from the gameloop to make the aliens move. The number 5 we are passing in is how many pixels to move the alien sprites down by when it hits the sides, while the last 2 parameters are the left and right limits between which the aliens move, which I’ve set to the limits of the game board.



  • Run the script and you should now have aliens moving left to right, down & then right to left and so on.

  • Next let’s add in rows and columns of shields in a very similar fashion to the way we created the aliens. To do this we first need to create a global sprite set for the shields, do this in the MAIN action as shown below. I would suggest you add this line after the line where you declared the alien sprite set. Remember the order that you declare sprites in, is also the order they are drawn in, so if you declare it before the background then the shields would be hidden by the background being drawn on top of it.





  • We could probably have used the addinvaders action to do this, however we are going to do it a slightly different way and create layers of overlapping filled rectangles to create the shields. This means that as the top layers are destroyed the aliens still have to shoot through another couple of layers before they can shoot at you. I am using the set color command and using numeric RGB values to assign slightly different shades to each layer. However apart from the way I am creating the shields, the rest of the code is very similar. So first of all add in the following action as shown below.



  • Call this action from the resetgame action as shown below. The shields should now be visible if you run the script.



  • Add in the following action to delete all alien & shield sprites from the board, this is so that when we start a new game sprites aren’t left behind on the board from the previous game.



  • Call it from the resetgame action. It is important that you add it before you call addinvaders and addshields, as shown below.



  • Go into art resources and add in the rest of the graphics that we need for the game. We need a rocket or laser graphic for both the aliens & the player to fire. So add graphics into art resources for this; name one alienrocket and one rocket. You can search online media or upload your own graphics. We also need a graphic for a shoot button, so find one and name it shootbutton in art resources.

  • Go into main and declare the following global variables, as shown below, for the rockets and for text sprites to display lives and score.



  • Go into the resetgame action and add in the 2 lines opposite to hide both rockets at the start of the game.

  • Add in the 2 lines following into the gameloop to update the text sprites with the current score and lives.



  • Go into libraries and add in the 2 libraries shown below.



  • Create a new action and call it shoot, this action will be used to shoot both the alien and player rockets. We pass in a velocity for the rocket, the sprite we are shooting from and the sprite we are going to shoot. The action then sets the position of the rocket based on the position of the sprite we are firing from, it gives it a vertical speed and then it issues the show command on the rocket sprite to make it visible on the screen.



  • Add the IF statement marked in red on the following page, into the gameloop action so that the main game code only runs when the player has more than zero lives. Make sure you indent the 3 lines of code below the IF statement, as shown in the picture, as those 3 lines call the actions which move both the player sprite and the alien sprites and we only want them to move when the game is running.





  • Make the following changes, marked in red below, to the “move base” action, which check for the user touching and/or holding down the shoot button on the screen. When this occurs we fire the player’s rocket if it is not already visible (which we are taking to mean that it is not currently being fired). The second bit of code marked in red, checks for the rocket leaving the top of the screen and sets it to be hidden if it does, so we can re-fire it if the player presses shoot again. The middle bit of code, marked in blue, doesn’t need entered again, it just needs moved so that it only runs against the ELSE from the new IF statement. This is so that the game only moves the player’s base when they touch the screen if they aren’t pressing the shoot button.



  • Try running the script and make sure you can now move and shoot. However you will find that you can’t destroy the aliens. If you find that the player’s base moves towards the shoot button when you press it, have a look at the code on the previous page again and make sure you have indented it properly.

  • We need to add a few actions for events that will occur in the game, such as the base being hit by a rocket, aliens being destroyed and the shields being hit. Let’s start by adding an action called “Base Hit” for when the player’s base is hit by the alien rocket. This action basically just subtracts a life and hides the alien rocket. This would be a good place to start an explosion animation and/or add an explosion sound effect (this may be in the challenges later).



  • Next let’s add the following action which we will call when a shield has been destroyed. This action just hides the alien rocket, removes the shield from its sprite set and then deletes it from the game board. This is another good place for an explosion sound and animation.



  • Add the following action which we will call when an alien has been hit by a rocket. This action hides the rocket, removes the alien sprite from its sprite set, deletes the alien sprite from the game board and finally adds 5 onto the score. Once again this is another great place for an explosion sound and animation.



  • Add the following action to handle collisions between the alien rockets and the shields, the alien rockets and the player’s base, as well between the player’s rocket and the aliens.



  • Go through this new action and make sure you understand what each bit does, the comments should help explain what each main part does. Basically though, it checks for collisions between sprites and calls the respective action when they do collide. For instance if the rocket hits an alien, it calls “kill alien” and passes in the sprite of the alien that was hit. Take special note of the part which fires the alien rocket, this part basically checks every alien to see if it is directly above the player’s base and if it is and if the alien rocket is not visible (therefore not already fired), it fires the rocket. The very last IF statement in the action checks for the alien sprite set count reaching zero, basically meaning all the alien sprites have been destroyed, at which point a new wave is created by calling addinvaders. Notice though that we are altering one thing in the next wave, which is the speed the aliens move at, so that as the player progresses through wave after wave of aliens it becomes faster and therefore harder & harder.



  • Add one final action (shown below), which will only be called when the game ends (lives reaches 0). This action posts the player’s score to the bazaar high score table and displays it for 5 seconds, along with a “Game Over” message. It then prompts the player to ask if they want to play again and then either calls the resetgame action or stops the script, depending on the response.





  • To finish the game all we need to do now is to call the two actions we just created from within the gameloop, do this as shown below (marked in red). Make sure the endgame action is called against the final ELSE, which if you look carefully should mean it will only be called when lives>0 is not true, therefore when lives is equal to or less than 0.



  • Run your game script and check everything works. It should look something like the screen capture shown below.


Challenges


Making use of what you have learnt so far, try and complete the following challenges in your game.

  • Add explosion animations and explosion sound effects for when an alien ship is destroyed, for when the base is hit by an alien rocket and for when a shield is destroyed. You can use more than one sprite sheet animation graphic in the same game, as shown below.




  • Add sound effects for when both rockets are fired.

  • Add an alien mothership that appears every so often, which the player can shoot for bonus points.

  • Change the graphics for the aliens after each wave is destroyed.

  • Publish your script.

End of session 4.


Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   10


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2017
rəhbərliyinə müraciət

    Ana səhifə