In this tutorial, we're going to implement cheat codes inside our game project.
Without any further ado, let's get started.
Open up the logic bricks, add an always sensor,
a keyboard sensor which is going to record
our keystrokes and a python controller having it's script type to
Module. Create a new property, name it as record
whose data type will be boolean and another property named as
cheats whose type will be string.
Moving to the keyboard sensor we've just added, we need to record the keys we're
pressing so enable All Keys. The Log Toggle field asks for a property
which let's us to record the keystrokes, if the property is false we
can't record our keystrokes,
so go ahead select our property record
in that field and set it True (just enable it lol). The Target field asks
for a property with which we record our keystrokes, so select our
cheats property in that field.
If you run the game, you can that our keystrokes are being recorded.
Create a new file, name it as cheats.py, define the necessary like below.
try:
import Range # I'm using Range Engine
except:
import bge as Range
cont = Range.logic.getCurrentController()
own = cont.owner # define owner
def main(): # got the owner, so no need of using "main(cont)"
pass
Now the main runs in a continuos loop, we need to check our cheat code. Before that I'd like to create some code for our cheats.
Here are some cheats, the first two cheats changes the default value. If
we type the same cheat again, we must be able to get it back to normal mode.
Now to implement it, we check the if any of the above cheats was typed
we trigger what must be triggered.
def main():
if "nogravity" in own["cheats"].lower() and own.scene.gravity.z != 0.0:
own.scene.gravity = 0,0,0
elif "nogravity" in own["cheats"].lower() and own.scene.gravity.z == 0.0:
own.scene.gravity = (0,0,-9.80)
Here our cube floats when the cheat is enabled but when you type again, gravity doesn't go back to normal. So we need to clear the text field by doing so.
def main():
if "nogravity" in own["cheats"].lower() and own.scene.gravity.z != 0.0:
own.scene.gravity = 0,0,0
own["cheats"] = ""
elif "nogravity" in own["cheats"].lower() and own.scene.gravity.z == 0.0:
own.scene.gravity = (0,0,-9.80)
own["cheats"] = ""
If this was hard to understand, let me break it down for ya:
Checks if the user typed nogravity and
if the gravity's Z axis was not equal to zero.
.lower() converts any letters that
are randomly in capital to lower.
We check if the user typed the same cheat again and the gravity's Z axis is equal to zero.
And then we need to implement two more cheats, which are iamspeed that changes the player's speed and abigcube that spawns a big cube in front of the player. But we haven't created the player's movement, so we'll make one. You can skip to this part if you're not interested making the movement code.
if "speed" not in own:
own["speed"] = 1/8 # int type, this is important
def move():
keysdown = Range.logic.keyboard.activeInputs
x = 0
z = 0
if 45 in keysdown: # 45 = W key
y = speed
else:
y = 0.0
if 23 in keysdown and not 26 in keysdown:
zt = speed
elif 26 in keysdown and not 23 in keysdown:
zt = -speed
else:
zt = 0.0
own.applyMovement((x, y, z), True)
own.applyRotation((0.0, 0.0, zt), True)
The code might look complicated but lemme break it down for you.
For part you need an always sensor that runs in a loop, enable true level triggering.
Now we've created the player movement, now we check if the player typed "iamspeed"
def main():
cheats = own["cheats"].lower() # Defined to avoid lengthy lines
if "nogravity" in cheats and own.scene.gravity.z != 0.0:
own.scene.gravity = (0, 0, 0)
own["cheats"] = ""
elif "nogravity" in cheats and own.scene.gravity.z == 0.0:
own.scene.gravity = (0, 0, -9.80)
own["cheats"] = ""
# "iamspeed" Part here
elif "iamspeed" in cheats and own["speed"] == 1/8:
own["speed"] = 1
own["cheats"] = ""
elif "iamspeed" in cheats and own["speed"] == 1:
own["speed"] = 1/8
own["cheats"] = ""
else:
pass
The main reason own["speed"] = 1/8 is it results in
shorter float whose value will be 0.128. If you have
defined it speed property as 0.1 the float be will something like
0.100000022912 or so, which can be frustrating to
check that cheat for the second time.
And if you have defined speed instead of
own["speed"] you might enounter
UnboundLocalError.
Now for the final part, we're going to spawn a big cube infront of the player. For that we need to copy the orientation and position of the player. Instead of writing code that need to deal with Vector, we can simply parent an Empty to the player. Must be looking something like this.
Now we need to add the code that spawns the object...
# Spawn object mechanism
elif "abigcube" in cheats:
own.scene.addObject("dacube", "spawn", 0)
own["cheats"] = ""
In the addObject(), we put the name of our object we're going to spawn as string and name of spawning object as string which is used to copy the position, rotation and scale to spawn "dacube".
try:
import Range
except:
import bge as Range
from mathutils import Vector
cont = Range.logic.getCurrentController()
own = cont.owner
if "speed" not in own:
own["speed"] = 1/8
own.addDebugProperty("speed", True)
def main():
cheats = own["cheats"].lower()
if "nogravity" in cheats and own.scene.gravity.z != 0.0:
own.scene.gravity = (0, 0, 0)
own["cheats"] = ""
elif "nogravity" in cheats and own.scene.gravity.z == 0.0:
own.scene.gravity = (0, 0, -9.80)
own["cheats"] = ""
elif "iamspeed" in cheats and own["speed"] == 1/8:
own["speed"] = 1
own["cheats"] = ""
elif "iamspeed" in cheats and own["speed"] == 1:
own["speed"] = 1/8
own["cheats"] = ""
elif "abigcube" in cheats:
spawn = own.scene.objects["spawn"]
own.scene.addObject("dacube", spawn, 0)
own["cheats"] = ""
else:
pass
def move():
keysdown = Range.logic.keyboard.activeInputs
x = 0
z = 0
if 45 in keysdown: # 45 = W key
y = own["speed"]
else:
y = 0.0
if 23 in keysdown and not 26 in keysdown:
zt = own["speed"]
elif 26 in keysdown and not 23 in keysdown:
zt = -own["speed"]
else:
zt = 0.0
own.applyMovement((x, y, z), True)
own.applyRotation((0.0, 0.0, zt), True)
Must be working.... Download project file here.