Last active
April 27, 2021 12:40
-
-
Save MarcScott/39e919a9c95152f155fb27686bc99460 to your computer and use it in GitHub Desktop.
surrogate.tv buggy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from gpiozero import Robot | |
from time import sleep | |
from surrortg import Game | |
from surrortg.inputs import Switch | |
from surrortg.inputs import Joystick | |
robot = Robot((7,8), (9,10)) | |
class MyJoystick(Joystick): | |
def __init__(self, score_cb): | |
# Store reference to score callback function | |
self.score_cb = score_cb | |
async def handle_coordinates(self, x, y, seat=0): | |
if y == 1.0: | |
robot.forward | |
self.score_cb() | |
elif y == -1.0: | |
robot.backward() | |
self.score_cb() | |
elif x == 1.0: | |
robot.left() | |
self.score_cb() | |
elif x == -1.0: | |
robot.right() | |
self.score_cb() | |
elif y == 0.0 and x == 0.0: | |
robot.stop() | |
async def reset(self, seat=0): | |
logging.info(f"reset") | |
class Run_Buggy(Game): | |
def update_score(self): | |
self.score += 1 | |
logging.info(f"score: {self.score}") | |
if self.score >= 10: | |
# End game by sending final score to game engine. | |
self.io.send_score(score=self.score, final_score=True) | |
# Disable all registered inputs. | |
self.io.disable_inputs() | |
else: | |
# Send updated score to game engine. | |
self.io.send_score(score=self.score) | |
async def on_init(self): | |
self.io.register_inputs({"joystick_main":MyJoystick(self.update_score)}) | |
async def on_prepare(self): | |
# Set score to 0 before game starts | |
self.score = 0 | |
if __name__ == "__main__": | |
# Start running the game | |
Run_Buggy().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment