Horse Race Game

From GoGoWiki

Jump to: navigation, search

Image:Horse race screenShot1.jpg

This example demonstrates how to use sensors to create computer games using Microworlds and the gogoboard. In addition to the technical description of the project, this example aims to highlight the process from which the project was initially conceived, how it evolved, and the possible directions which it could further grow. Finally, two mathematical ideas that became relevant during the project construction are discussed.

Contents

What You Need


Project Conception

The idea to build a horse race game emerged while a group of students were playing with a simple touch sensor that would control the movement of a cartoon character on the screen (we called him “Sam”). It was simple: Sam would move forward every time we pressed the touch sensor. The Logo program to do so looked like this

To SamRun
   Forever [
       if sensor1 < 100 [Sam, fd 5 ]
   ]
end

Image:Step sensor.jpg

The touch sensor has two terminals each wrapped with Aluminum foil. When the terminals make contact, it conducts electricity and the sensor value becomes 0 (the value while there is no contact is 1023). Therefore, the above code will move the turtle named “Sam” forward while the terminals are in contact. Note that we assume the sensor is plugged into sensor port 1.

Now that we can control the character, the horse race game idea emerged. The idea was to let Sam race a horse. The fact that it is counter intuitive to think that a person can race a horse makes this idea interesting.

Image:Horse race diagram.jpg

The goal was to have the horse run on its own with a constant speed and Sam will have to try to run faster. We needed to change our program a bit so that the Sam’s speed would be controlled by the speed of which the player hits (and releases) the touch sensor.

To SamRun
	Forever [
		If sensor1 < 100 [
			Sam, Fd 5 
			waitUntil [sensor1 > 100]
		]
	]
end

We basically added the waitUntil command into the loop. This way, the program will wait until the player releases the touch sensor before continuing. Without this, Sam would keep on running as long as we press the touch sensor, which won’t be so much fun to play.

The code for the horse was simple:

To HorseRun
	Forever [
		Horse, fd h_speed
		Wait 5
		]
end

We created a slider called “h_speed” and used it to control the horse’s speed by passing it as a parameter to the fd command.

Project Evolution

The game worked well and was quite fun to play. However, because we often had many people watching while we played the game, a new idea to improve the game emerged. The game could be more fun if the audience could participate. One way to do this is to build a second touch sensor and have two people race each other. But the idea that really got us excited was to use a microphone to control the horse. This way, the audience can cheer for the horse and we can program the game so that the louder they cheer the faster the horse will run.

Mathematics: The idea of offset and scaling

Using the microphone was not as straight forward as using the touch sensor. The following is the behavior of the sensor readings we obtained from the microphone:

Sound Level Sensor Value
No sound 140
Very Loud Clap and Cheering 600


If we were to use these values to directly control the horse and assuming that we connect the microphone to sensor2, the code would look like the following:

To HorseRun
	Forever [
		Horse, fd sensor2
		Wait 5
		]
end

This code would not work because the horse would move forward 140 steps even when there is no cheering. How can we fix this problem?

One solution is offset the value by substracting 140 from the sensor readings. Our procedure would become:

To HorseRun
	Forever [
		Horse, fd (sensor2 – 140)
		Wait 5
		]
end

Okay, now the horse will not move when there is no cheering. However, there is still another problem. When the audience are cheering the sensor value could go up to 600. In this case, the horse will move 460 steps (600 – 140), which is much too fast. How do we fix this issue?

Scaling can be used in this case. The following table shows the desired value we wanted for the horse:

Sound Level Sensor Value Desired Value
No sound 140 0
Very Loud Clap and Cheering 600 15

With some familiarity with the idea of scaling and offsetting, we can devise the following formula to produce the desired value:

Desired Value = (SensorValue – 140) / 15

Therefore, the procedure becomes:

To HorseRun
	Forever [
		Horse, fd (sensor2 – 140) / 15
		Wait 5
		]
end

Future Directions

During the development of such an open-ended project like this one it is quite normal for new ideas to emerge. They could be ideas for further improvement of the current project or for an entirely new project. The following are a few examples of ideas that emerged from this project:

  • Create a slider to control the overall speed of the horse. This would allow us to set the game’s difficulty level. One approach to implement this idea is to replace the scaling factor in our formula with a slider, making the scaling dynamic.
  • Use step sensors instead of touch. This will allow players to control Sam by actually “running” on the sensor. Two step sensors can also be used: one for each foot allowing more intensive game control.
  • Red, Yellow, and Green lights bulbs can be added and cotrolled by the GoGo board. These lights can be used to signal the beginning of the race: Ready “Red”, Set “Yellow”, GO! “Green.”
  • Add a second runner to intensify the competition.
Personal tools