|
| 1 | +# Programming New Game Modes |
| 2 | + |
| 3 | +## Overview |
| 4 | +Game modes really come down to what color should your RGB be and what should happen when you get hit. |
| 5 | + |
| 6 | +## Base Class |
| 7 | +To implement a new game mode, you need to create a new class that extends/implements the [BaseGame class](https://github.com/javaplus/MadScientist/blob/3be6801566cf3852717db674f972e4c576ec4078/code/basegame.py#L3). |
| 8 | + |
| 9 | +```Python |
| 10 | +from motor_class import MotorController |
| 11 | + |
| 12 | +class BaseGame: |
| 13 | + # Constructor gets motor_controller and rgb |
| 14 | + def __init__(self, motor_controller, rgb, laser): |
| 15 | + self.motor_controller = motor_controller |
| 16 | + self.rgb = rgb |
| 17 | + self.laser = laser |
| 18 | + |
| 19 | + |
| 20 | + ## Implement Setup (initialize rgbs and motor status) |
| 21 | + def setup(self): |
| 22 | + pass |
| 23 | + |
| 24 | + ## implement on Hit. |
| 25 | + def onHit(self): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +## Custom Game Class |
| 32 | + |
| 33 | +When you extend this class you get the constructor by default which takes in the motor_controller, the rgb, and the laser. |
| 34 | + |
| 35 | +These are the three things with the current hardware you can control. |
| 36 | + |
| 37 | +Here's an example of how to extend it with a simple implementation: |
| 38 | + |
| 39 | +```Python |
| 40 | +from basegame import BaseGame |
| 41 | +from motor_class import MotorController |
| 42 | + |
| 43 | +class BasicGame(BaseGame): |
| 44 | + |
| 45 | + ## Implement Setup |
| 46 | + def setup(self): |
| 47 | + # set color to green |
| 48 | + self.rgb.color = (0, 255, 0) |
| 49 | + |
| 50 | + ## implement on Hit. |
| 51 | + def onHit(self): |
| 52 | + print("Basic Game been hit!!") |
| 53 | + # Let's flash our eyes |
| 54 | + # go red |
| 55 | + self.rgb.color = (255, 0, 0) |
| 56 | + self.motor_controller.spin_lock() |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +Notice there is no need to create a constructor when you implement the BaseGame class as you inherit it's constructor. |
| 61 | + |
| 62 | +This BasicGame class simply sets the color to green on setup. |
| 63 | + |
| 64 | +The `onHit()` function simply changes the color to red and then causes the motor to do a spin_lock, which is basically just a forced spin for a certain amount of time. |
| 65 | + |
| 66 | + |
| 67 | +## How the Game Class is used |
| 68 | + |
| 69 | +The `setup()` function gets called as soon as the game mode is changed or initiated. |
| 70 | +Currently the game mode can change based on the command from the bluetooth remote app. |
| 71 | +In the [main.py](/code/main.py) file, the `execute_command()` function has a an if block to set the game mode. The `initializeGame()` function takes in the game mode string to determine what game mode to use by calling the `setGameMode()` function also in `main.py` that will instantiate the appropriate game class and return it. |
| 72 | + |
| 73 | +Here is the `initializeGame()` function: |
| 74 | + |
| 75 | +```Python |
| 76 | +def initializeGame(gamemode): |
| 77 | + global hitevent |
| 78 | + |
| 79 | + # reset HitEvent to remove previous subscribers |
| 80 | + hitevent.reset() |
| 81 | + |
| 82 | + # get the game |
| 83 | + game = setGameMode(gamemode, motor_controller, rgb) |
| 84 | + |
| 85 | + # setup game |
| 86 | + game.setup() |
| 87 | + |
| 88 | + ## Set the method to be called when hitevent fires! |
| 89 | + hitevent.subscribe(game.onHit) |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +Notice the `initializeGame()` function resets the game events. Currently there's only `hitevent`. |
| 94 | +It also, then subscribes the game class returned by `setGameMode()` to the `hitevent`. This allows the game class's `onHit()` function to be called when there is a hit detected. |
| 95 | + |
| 96 | +Let's look at the `setGameMode()` function now: |
| 97 | + |
| 98 | +```Python |
| 99 | + |
| 100 | +## to select the game mode |
| 101 | +def setGameMode(gamemode, motor_controller, rgb, laser): |
| 102 | + if gamemode == "Virus": |
| 103 | + print("Virus mode") |
| 104 | + # Return VirusGame impl |
| 105 | + elif gamemode == "Disco": |
| 106 | + print("Disco mode") |
| 107 | + # Return Disco Game impl |
| 108 | + elif gamemode == "Hungry": |
| 109 | + print("Disco mode") |
| 110 | + # Return Hungry game impl |
| 111 | + elif gamemode == "WTF": |
| 112 | + print("WTF mode") |
| 113 | + # Return whatever WTF mode is |
| 114 | + else: |
| 115 | + print("Default mode") |
| 116 | + return BasicGame(motor_controller, rgb, laser) |
| 117 | + |
| 118 | +``` |
| 119 | + |
| 120 | +This function is a simple if/else block that instantiates the correct game class to return based on the `gamemode` parameter. This is where you would instantiate your custom game mode. |
| 121 | + |
| 122 | +If this `setGameMode()` returns your custom game class instance, then it will then have it's `setup()` function called almost immediately and then it's `onHit()` function will be called whenever it gets hit. |
| 123 | + |
| 124 | +To make things more interesting you may want to implement new methods on the `MotorController` class in the [motor_class.py](/code/motor_class.py) that you can call in your `onHit()` function. |
| 125 | + |
| 126 | +Another future enhancement could be to add a fireevent copying the hitevent pattern to be able to do something when the fire button is hit. (Fire button may be coming soon to the mad scientist app). |
| 127 | + |
0 commit comments