Kodu Football part 1
Kodu Sports - Lesson 3
Objectives
- Plan and design a 3D game environment
- Create and refine sequences of commands to accomplish specific goals
- Use logical reasoning to predict outcomes debug algorithms
Lesson Resources
Lesson 1 - Air Hockey part 1
Lesson 2 - Air Hockey part 2
Lesson 3 - Kodu Football part 1
Lesson 4 - Kodu Football part 2
Lesson 5 - Kodu Speed Pool part 1
Lesson 6 - Kodu Speed Pool part 2
Introduction
Briefly recap the air hockey project from the last two weeks. What went well? What were the hardest parts?
Introduce the next part which is going to be a Kodu football game! Which football video games do you know of? Football video games have also changed a lot over the years and today many look very realistic, but they didn’t always, and there are still different kinds of games available based on football as the main concept, take a look at some of these classics:
NASL Soccer (1979)
Nintendo Soccer (1985)
FIFA International Soccer (1993)
Sensible World of Soccer (1994)
FIFA 99 (1998)
Pro Evolution 4 (2004)
Soccer Physics (2015)
FIFA 21 (2020)
So the football simulation games, like the FIFA series have come a long way and strive to be as realistic as possible, but football is a very complex game and Kodu does have it’s limitations, so discuss with a partner:
- What challenges do you foresee in making a Kodu football game?
- What things about football might we need to simplify?
- What is essential in the game?
In computer science terms we call this decomposing the problem (breaking it down into smaller steps)
Take some feedback on their ideas, they may suggest some of these issues:
- Controlling lots of players
- Creating artificially intelligent players
- Coding all the rules of football
To simplify some of these things and to make use of some of the options and inbuilt characteristics of Kodu, this example game will be closer to the rules and style of indoor football, where the ball never leaves the pitch and can be bounced off the walls. You are also forbidden to enter the opponent's goal areas.
Making the pitch
Like the air hockey game, it’s best to start with the playing area, in this case the football pitch. As suggested in lesson 1, add a Kodu character before you build the pitch to give you an idea of scale.
Then use a large square brush to paint a large rectangular pitch:
Then use a much thinner, white linear square brush to add some pitch markings to your field. The normal round or square brush will be needed if you add a centre circle. It’s up to the students how much detail they add at this point. We’ve kept our example field fairly simple with it’s markings:
Next add the goals, these need to be made in a particular way for them to work later in the game. First use a square brush to paint a goal area that hangs off the end of the pitch. Then, choose a different colour and with a very thin square brush, draw an outline for the goal area on three of the four sides, leaving the side that faces the pitch clear (see below). Next add one square in a third new colour to each front corner of the goal area, these will become goal posts and need to be a different colour to the rest of the goal.
At this point, switch to the Up/Down tool and use the magic brush option. Raise the goal posts and the back/sides of the goal by left-clicking them, but leave the floor area flat.
Once the sides and posts have been raised go back to the paintbrush tool and select the same colour that you used for the floor of the goal and, with the magic brush (fill) tool, colour the back and sides of the goal the same colour as the floor, leaving the posts a different colour. This needs to be done in that order so it is possible to raise the edges of the goal separately to the floor, however they need to end up all the same colour for the game to work later.
Repeat this process with different colours at the other end of the pitch. The front goal posts can be the same colour at both ends though.
Your pitch should look a bit like this at the end:
Finally, go to the world settings and set the camera in a fixed position looking horizontally at the pitch so it can all be viewed by both players at all times.
It’s sensible to ask the students to save their game as they go, so if they haven't already, save here.
The players
Our next step is to add the players to the game. As we discussed at the start having lots of players is going to make it a very complicated game, so in our example, we’re sticking to one outfield player and a goalkeeper.
With the Kodu we added at the start (for scale) go to it’s character settings and put all the speed and acceleration settings up to full.
Then right-click the Kodu and copy it, right-click again on the empty pitch and paste in three copies of the Kodu, giving you four in total.
Once they are on the pitch, position them one in each goal and another on each side of the centre of the pitch. You may also want to rotate them so they are facing the correct way, looking inwards to the centre of the pitch, which is where the ball will be at the beginning of the game. Right-click them and choose rotate to do this.
Coding the Goalkeepers
In this example, the goalkeepers are going to be AI (artificially Intelligent) characters (though not that intelligent in truth!). Switching between control of different characters (like you do in most football simulation games, like FIFA) is possible, but not easy, so we want the goalkeeper characters in this example to just move back and forth between their goal posts to make it a bit harder to score. How could you make this work? Ask the students to have a go at this on their own first. Can you also make the goalkeeper kick the ball away if it hits them?
The solution to this is to use a path. Place on the goal line a short path with just two nodes. Colour it the same colour as the Kodu team for that goal by using the left/right arrow keys when adding the path.
For both goalkeepers the code is simple, leave the WHEN empty so they always perform the actions of moving on their coloured path. On line 2 we’ve added some code so the goalkeeper kicks the ball away when it is hit by the ball. The direction (west here) should be checked in relation to the compass in the bottom right of your game screen. In the example below north can be seen as parallel to the goal line so west is away from the goal and onto the pitch. Therefore, the goalkeeper at the other end of the pitch would need to kick east when clearing the ball.
Coding the players
Before we get into coding the two outfield players, there is one more (optional) addition to add to the pitch itself. If possible (though not essential), choose a new flooring type that is a similar colour to the team/goal colours you have chosen, but not the actual same colour as you used for the goals. Fill in the floor of the goal area with this colour using a square brush (see below). This will let us create a no-go area for opposing teams so you can’t just walk the ball into the goal.
You can also add a ball (it’s with coins and hearts in the objects category) to the centre of the pitch so we can use it to test things shortly. We’ll come back and spend more time on coding the ball next week, but it has basic ball properties as it is (it rolls and bounces etc).
We are making a two player game so both teams will be controlled by human players. That means the red and blue teams will need different control keys. It’s common when this is the case for one player to use the arrow keys and the other to use the WASD keys (W= up, S= down, A= left, D= right). This means that with most keyboards two people can sit side-by side and use opposite ends of the keyboard to play the game together.
Ask the students to start with one of the outfield players and have a go at adding code to make the following things happen:
- Make your Kodu move with the keys (what’s the best option for this when using a fixed camera and a two player game?)
- Stop the Kodu being able to go in the opposition goal area (e.g. red cannot go in the blue box and vice versa - You can go in your own area)
- When touching the ball, make Kodu collect it so it travels with Kodu.
- When you press a key, make Kodu ‘kick’ the ball to take a shot.
- When Kodu is tackled by the other team, make Kodu lose control of the ball
- Make the player win when they score five goals
Here’s the code (for the red team Kodu) that solves all of those problems:
- Move with the keys (just ‘move’ not forwards is better with a fixed camera)
- Freeze when on the flooring type of the opponent’s area
- Pick up the ball when you touch it
- Kick the ball (shoot) when you hit a key (pick something on the same side of the keyboard as the directional controls)
- When you’re tackled by the other player drop the ball
- When you score 5 you win
You can copy and paste this page of code for the other team’s outfield Kodu, however, it will need some small adjustments, can you recognise where the changes are are needed for the other team’s Kodu?:
- WASD instead of arrows on row 1
- Land type for the other goal area on row 2
- The shooting key on row 4
- The colour on row 5
- The colours of row 6
Ask them to test everything they have added so far and make sure both team’s outfield player is working correctly.
Plenary
Finish off the lesson by discussing their solutions to the problems you set them for controlling Kodu. Discuss ideas for what could be added in the next lesson and ask them to save their game carefully as a new incremented version using the version arrows in the top right of the save box.