Game Development Reference
In-Depth Information
Cheat Codes
Cheat codes are like candy to game players. Type in the magic words and all of a sudden you've
skipped a level, gained invincibility, or received a load of weapons with unlimited ammo.
Sometimes though, the game developer tricks you and you find yourself surrounded by an evil
horde of enemies. For developers, cheat codes have always been a method to test particular
features of the game. You may not strictly need them, as you have the source for your game under
your fingertips, but the players will love them.
Here is how you can create cheat codes. In our bouncing cog example, we'll use the cheat
code gimmemore to burst 100 extra cogs from the center of the screen.
Start with: Reference/Framework/bouncing2.gmk
Creating a Cheat Code
1. Create a new object and call it obj_cheatcode . Don't give it a sprite.
2. Add a Key Release event for <Any key> . Include an Execute Code action and insert the
following lines:
1: {
2: if ( string_pos('gimmemore',string_lower(keyboard_string)) )
3: {
4: repeat ( 100 ) instance_create(320,240,obj_cog);
5: keyboard_string = '';
6: sound_play(snd_activate_cheat);
7: }
8: }
This script uses Game Maker's system variable keyboard_string , which returns a string
of, at most, the last 1024 characters that have been entered on the keyboard. The
function string_pos returns the position where the text 'gimmemore' was found. If it's
larger than 0, the condition is automatically true and the lines between the brackets
are executed. So it doesn't matter if any other keys are typed during the game, we're
specifically looking for one sequence of characters between them. The function
string_lower makes all keyboard input lowercase, so that we don't have to worry
about the state of the Caps-Lock key.
Line 4 does what our cheat was all about, which is creating 100 cogs from the center
of the room. Line 5 clears all previous keyboard input, to make sure the cheat isn't
triggered again. Finally, line 6 plays a sound to indicate the cheat has been activated.
3. Place one instance of obj_cheatcode in the game's room.
Result: Reference/Result/cheat_code.gmk
Note Cheat codes must be typed in lowercase within the script code, but it doesn't matter whether you
type it in uppercase or lowercase during the game!
 
 
Search WWH ::




Custom Search