Skip to content

Commit 23f144f

Browse files
committed
added laser to basegame and created game mode coding lesson
1 parent 3be6801 commit 23f144f

File tree

3 files changed

+133
-4
lines changed

3 files changed

+133
-4
lines changed

code/basegame.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
class BaseGame:
44
# Constructor gets motor_controller and rgb
5-
def __init__(self, motor_controller, rgb):
5+
def __init__(self, motor_controller, rgb, laser):
66
self.motor_controller = motor_controller
77
self.rgb = rgb
8+
self.laser = laser
89

910

1011
## Implement Setup (initialize rgbs and motor status)

code/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
hitevent = HitEvent()
4040

4141
## to select the game mode
42-
def setGameMode(gamemode, motor_controller, rgb):
42+
def setGameMode(gamemode, motor_controller, rgb, laser):
4343
if gamemode == "Virus":
4444
print("Virus mode")
4545
# Return VirusGame impl
@@ -54,16 +54,17 @@ def setGameMode(gamemode, motor_controller, rgb):
5454
# Return whatever WTF mode is
5555
else:
5656
print("Default mode")
57-
return BasicGame(motor_controller, rgb)
57+
return BasicGame(motor_controller, rgb, laser)
5858

5959
def initializeGame(gamemode):
6060
global hitevent
61+
global laser
6162

6263
# reset HitEvent to remove previous subscribers
6364
hitevent.reset()
6465

6566
# get the game
66-
game = setGameMode(gamemode, motor_controller, rgb)
67+
game = setGameMode(gamemode, motor_controller, rgb, laser)
6768

6869
# setup game
6970
game.setup()

lessons/game_mode_coding.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)