Air Hockey part 2

Kodu Sports - Lesson 2

Objectives

  • Use variables to create game systems
  • Use logical reasoning to predict outcomes and debug algorithms
  • Evaluate a program that they have created and say what they liked and what could be done to improve it.

Introduction

Begin by recapping last week’s lesson. Ask the students:

  • Can you briefly describe some of the tools and processes we used to create our board last week?
  • What was the last challenge you were set?
  • How can we make our saucers move?
  • How can we restrict them from entering the opposite side of the board?

 

Making them move

Go through solving these issues as a group, taking the students’ suggestions as you go. Making the first character move is simple, just use the arrow keys and move as shown below:

 

 

To restrict movement into the opposite half of the board we can use the flooring type. On the second line of code for the first flying saucer, for WHEN go to More and choose On land and then follow this with a Types tile, and choose the floor style you used for the opposite side of the board:

 

 

On the Do side add Move and then choose a direction from the options of north, south, east and west. At this point you may need to return to your game screen to check which direction to use. A compass point can be seen in the bottom right of the screen. In the example below the blue player is looking west as they face their opponent. As we move our saucer into the other half we want it to stop us moving that way and send us back, which is east. This is an example of a conditional statement in your code; something has to happen to activate the code. 

 

 

In newer versions of Kodu, you now also need to drag this line of code to be row 1, so it takes priority. So the code to make it work looks like this:

 

 

It allows movement in any direction, but when the character is on the flooring type of the opponent’s half it forces them east, pushing them back into their own half.  You can still adjust the saucer’s movement and acceleration speeds in their character settings if you wish. In our example we have the forward speed set at 1.5 and the acceleration at 2.0.

If the students haven’t already got their saucers moving ask them to add this code to their game for their first saucer and then consider, what needs to be changed to make it work for the second saucer? The land type and the direction will need to be changed for the second player.

 

The Puck

Next it’s time to add the air hockey puck to the game. Add a new character and choose the Puck from the Objects menu. It’s perfect for this game as it’s inbuilt characteristics make it whizz and bounce around the Kodu world with very little friction (you can adjust the level of friction in the character settings if you want it to move even faster, or a little slower).

 

 

Place it in the centre of the board. You may wish to adjust its size and lower its height to the board. In our example we set size to 1.4 and lowered the height to 0.21.

 

 

Most of the code for the Puck is centred around the scoring in the game. How can we make it award points to either player when it strikes the goal areas? This is done again by using the flooring type as the trigger (another example of a condition). When the puck is on that particular land type we tell it to award 1 red/blue point to the player (depending who scores). What do you want to happen after a goal is scored? Should the game just continue or should it be re-started? 

Nested under the first line of code (so it happens at the same time, but in sequence after the point is awarded) is a short sound effect (find Play in actions) for some audio feedback to the player and reset > world (find this in the Game category when adding it to the Do). 

The order of the lines of code does matter here, what would happen if we switched lines 2 and 3? If this was the case then the world would reset on line 2 and therefore the sound effect would never play as resetting the world cancels any running code, for a similar reason, long sound effects don’t work as the world gets reset before they can finish playing. 

These are optional things that the students can choose to include or not, or they may have other ideas of things they want to happen at this point.

 

Player 1 scoring code:

 

Player 2 scoring code:

 

We also need to set a winning target for the players and a way to win/lose the game. In official Air Hockey rules (yes there is such a thing!)  It’s first to seven points to win a game, so we’re sticking to that in our example game too. Can you add two more lines of code that will declare which player has won when one of them gets to seven points (or a number of your choosing)?

Being a two player game we want it to declare ‘winner’ whoever gets to seven first, so we also need to remember to add a colour to the win tile to differentiate who the winner is:

 

 

Adding anything related to scoring in Kodu automatically will make the number score appear in the corner of the screen. It will also match the colours to those you have chosen in your code, but if you want to add a label to the score as well, this can be done, along with some other scoring tweaks in the world settings. Scroll down (quite a long way) until you find Score visibility for the colours you have chosen. There are five options to choose from:

  • Loud labeled - Allows you to add a label to the score and makes a noise and shows a +1 graphic as score goes up.
  • Loud - Makes a noise and shows a +1 graphic as score goes up.
  • Quiet labeled - Allows you to add a label to the score but no score effects happen
  • Quiet - Score will appear in the corner but no score effects happen
  • Off - Score is not visible in the corner

 

Labelling the score can be useful for more than just adding ‘player 1, 2’ etc. It also means that you can use a numeric value for other things such as a timer or number of items collected etc.

In our example we are using Loud Labeled and just adding ‘Red Score’ / ’Blue Score’.

 

 

At this point you should have a fully functioning game that needs to be tested to check for any bugs in the code or settings that might need tweaking. Ask the students to play their game with a partner and report back their findings on the game

 

Adding extras

From now anything else you want to add are ‘extras’ that are not essential but might improve the look and feel of the game. Ask the students to discuss with a partner:

  • What else could we add to the game now?
  • What special effects could we add?
    • Sound?
    • Graphical effects?
  • Could you add to the world to make it look better? 

 

Here’s some ideas of extras to add.

Graphical and sound effects for the puck. Here the puck changes to the colour of the last player to bump it and also plays a short sound effect when bumped. 

 

 

Changing the look and feel of the game environment can be quickly achieved by changing the sky and lighting settings in the World settings

 

 

You can also add some surroundings to your game table to make it look better and give it a theme, though keep an eye on the barometer on the right which shows how ’full’ your game is, if it reaches red your computer will start struggling with your game and it may start to crash and slow down. You may also need to adjust your set camera position to make the surroundings visible. 

Here’s a couple of game environment examples:

 

‘The pond’

 

‘Space Neon’

 

Finish off by asking students to evaluate someone else’s final game. You can use our Game review sheet or use your own criteria and system if you prefer.

 

 

Ensure everyone saves their games as a new version by changing the version (v) number up one and perhaps adding ‘final version’ or something similar to the title.

 

 

You can download all three versions of our Kodu Air Hockey games below. Click the link, then when the Drive screen opens, save the file to your computer by clicking on the download arrow that appears as you move your cursor to the top of the screen. Then copy and paste the file you download into the following folder so you can open it in Kodu: Documents > Saved Games > Boku > Player1 > Imports

Then from the main Kodu menu go to Load game and the file should appear in the Downloads section.

 

< Previous Lesson

Next Lesson >