Kodu Speed Pool part 2

Kodu Sports - Lesson 6

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

Recap the last lesson:

  • What have we added to our game so far?
  • What problems have we overcome so far?
  • What still needs to be coded? 

We finished last lesson by adding one of the game ‘ball’ pucks. Before making any other pucks, we need to add code to this first one. Can you make this puck disappear when it enters the pockets? Can you also make it give you a point and make a noise when it’s potted? 

 

 

Test this and ensure it disappears when it touches the colour that makes up your pockets and that you get a point for that. While they are testing the game ask the students, can you find a bug that allows us to cheat at the game? If so, can you fix it? 

The pucks will bounce off anything, including Kodu, so initially it’s possible to just push them into the pocket with Kodu, which makes the cue ball pointless, so we need to fix this and make sure they don’t move if we bump into them.

Row 4 below solves this problem. It allows the puck to bounce off anything, but when it bumps into Kodu it freezes. We’ve also added some extra special effects to make things more interesting.

 

Can you make your puck light up only when it is hit? Can you make it make a noise when the ball hits it?

 

 

Rows 5 and 6 here are similar in function to using an ‘if/else’ statement, as found in Scratch and other programming languages.

 

 

They give two options from an if statement. Here, it says ‘If you bump anything glow, else (not bumping) turn the glow off’ .

Once all that is working, we can then make nine more copies of the puck to give us ten pucks in total. These are traditionally arranged in a triangle in pool or snooker. It’s easier to get them all in line and well spaced out if your orbit the camera to look down from above the table, also use the lines of squares on the table as guidelines:

 

 

You can have one colour for all of the pucks, alternate between two colours, or there are enough colours to have a different colour for each puck if you prefer. 

In our example, the pucks will be multicoloured but all behave in the same way and be worth the same points. The students may choose to adapt their game later and make different colours worth different points. 

 

 

Score / Time

We have added points to our game already as we pot the pucks, but at this point we are going to label the red score as ‘points’ and then create a new variable that we will use for a timer. 

In the world settings, scroll down to Score Visibility: Red (or the colour you chose for your score) and change it to Loud labelled, type ‘Score’ in the text box that appears.

 

 

Click back to the world settings menu and find Score Visibility: Blue. Change this to Quiet Labelled and type ‘Time’ in the text box.

 

 

Cue ball code

When the cue ball is potted in Pool it is a foul and the player is penalised. The next job is to add some code to the white ball to allow it to also be potted.

Discuss with the students, what should happen when the cue ball is potted in the game? What sort of penalty could we impose on the player?

The code below shows one way to impose a time penalty: 

 

 

On row 1, When the ball touches the land type of the pockets, one black point is added. This point will not visible on screen at any time, it just acts as a variable on/off switch. Other characters can then react to this variable so, for example, things happen when the black score equals 1 or 0.

As the black score changes to 1, three other things are nested below that so they happen at the same time: the ball vanishes, a negative sound effect plays and 3 ‘points’ are subtracted from the blue variable. What will this actually do in the game? What is represented by the blue variable? As blue represents time in our game, the player will lose three seconds of their game time if they pot the white ball.

The code all happens sequentially from top to bottom, so after all that has taken place, row 5 senses when the black score has been switched to 1 and immediately subtracts 1 from it, to return the variable to 0, thus resetting the ball to its normal state.

Before testing this, go into the world settings and set the Score Visibility: Black to Off.

 

 

Then ask the students to test that their white ball disappears if they fire it into a pocket. What problem do we now have though? The ball will disappear but won't come back! 

 

Re-spawning the white ball

We are going to use a similar method to the football game to re-spawn the white ball when it gets potted. Add a rock just behind the ball on the centre marker of the baulk line.

 

 

Program the rock. As the ball is potted we made the black variable change to 1, we are going to use that here to trigger a new ball to appear. Row 1 (below) does this, when the black variable is equal to 1 the rock creates a copy of the creatable ball once.

It’s really important that they choose the creatable ball character and not just a ball from the objects menu as this would not contain the code for the ball, and so would not work in the same way as the creatable version. What would happen if we didn’t have the ‘once’ tile at the end? Lots of balls will appear from the rock! 

 

 

Below line 1 we need to add some more code to make the rock the controller of some other elements of our game:

  • Time
  • Winning
  • Losing

 

Set these challenges for your students (all the code goes on the rock):

  • Can you make the rock set the clock to one minute at the start of the game?
  • Can you make the timer count down 1 every second?
  • Can you make the game end when the clock reaches 0?
  • Can you make the player win when all the pucks have been potted?

 

 

Finally, the rock needs hiding from the game, so go to its character settings and turn on Immobile, Invisible and Ghost.

 

 

Final things to do are:

  • Set the start position of the camera and Kodu
  • Change your sky colour (optional)
  • Change your world lighting (optional)
  • Add a world description, controls explanation and choose how the game starts

 

Reviewing the game

Ensure they save a final copy of their completed game. 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.

 

 

You can download the example version of our Kodu Speed pool game below. Click the link, then when the Drive screen opens, save the file to your computer by clicking on the download arrow at the top of the screen, and then copy and paste the downloaded file into the following folder on your computer 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.

 

Download Kodu Speed Pool demo version

< Previous Lesson