Student challenge 1
Present Day I: The Museum - Navigating the game
While working at the Scriptsonian museum for class credit, you have accidentally activated the mysterious Kurono time machine! It's down to you and your classmates to work out how to use it. Read through the instructions below that show you how to move around the map.
Introduction
Kurono is a game that helps you develop your Python programming skills. You write code to move an avatar around a grid world, avoiding obstacles and other players and picking up objects you find on the map.
Kurono uses a turn-based approach. This is a bit like a board game where you can’t move unless it’s your turn. You need to write code to decide what move to make in a subroutine called next_turn()
and the game itself calls this subroutine when it’s your turn to move. Turns happen quite quickly though, so you don’t need to wait for long.
Your next_turn()
subroutine is sent the world_state
and avatar_state
objects which you will use in later worksheets to give you access to what is on the map and where you are on the grid. You can make use of the following built-in constants to move in a fixed direction:
direction.NORTH
direction.EAST
direction.SOUTH
direction.WEST
Notice that the map includes a compass to show you where North is. See Figure 1.
Subroutine
A subroutine is a section of code that is separate from the main code and needs to be called in order to run. You use subroutines in Python all the time. print()
and input()
are both subroutines but they are part of Python. You should already know how to write your own subroutines but if you don’t, just make sure that all the code you write is inside the def
block. See the example in Figure 1. The code on the left stores a northward move in the action variable and then returns it so the game can action it.
1. Change Direction
When you first open Kurono, you will notice that the code in Fig. 2 is already there. If you leave that code running, you will eventually get stuck on an obstacle block or the edge of the map. Try changing the code so that your avatar moves in a different direction instead. Remember to use one of the direction constants shown previously.
2. Move in random directions
Now, can you write some code that moves your avatar in a random direction instead of just one fixed direction? Don't forget to add import random
at the top of your program as you would normally do. You will also need to use an if...elif...else
statement to help you here.
Need a hint?
You need to get Python to give you a random number between 1 and 4 and then use this to choose a direction to move in. The code below prints a random number to the console log and then moves North. Try it to work out what the numbers in the brackets are for and then finish the code.
3. Investigate location
Now your avatar is randomly wandering the map, they are likely bumping into obstacles and might be getting stuck for a few turns. In this next task, you will find out if you have collided with something. Start with your solution to task 2.
Add the line of code below before the return
:
print(avatar_state.location)
Look at the console log to work out what this code does. What do you think those numbers represent?
The Kurono grid world uses x-y coordinates for each square (or cell) on the grid. The code you added tells you the x-y coordinates of the cell your avatar is in. You can check if you can move onto a cell before you try to move using this code. Add it before the return.
Once you have done this, can you shorten this code so that it only outputs a message when you can't move?
Make sure that you return a WaitAction
if you can't move. You can do that like this:
Summary
Well done! You have learned about the basics of using Kurono and moving your avatar around the game board. In the next session, you will find out how to avoid obstacles and how to pick things up that you found on the map. You have investigated:
subroutines
generating random numbers
complex selection statements with
if..elif...else
some properties of the game board to find out where you are
Last updated