Going only in straight lines is no fun. We can't have a racing game without turning so we need to get that put in as well.
First, we want to add a new variable. Place the following code just below the line where we create the
self.accel
variable:self.handling = 20
Next, we're going to write another method for turning. Add the following code just below the end of the
cycleControl()
method:def turn(self, dir, dt): turnRate = self.handling * (2 - (self.speed / self.maxSpeed)) if(dir == "r"): turnRate = -turnRate self.cycle.setH(self.cycle, turnRate * dt)
Finally, add the following code above the line in the
cycleControl()
method that callsself.speedCheck(dt)
to put the new method to use:if(self.keyMap["d"] == True): self.turn("r", dt) elif(self.keyMap["a"] == True): self.turn("l", dt)
Check the file
chp04_05.py
in theChapter04/Examples
folder to see the code, if necessary.Save the file as
chp04_05.py
and run...