Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Last active April 27, 2021 12:40
Show Gist options
  • Save MarcScott/39e919a9c95152f155fb27686bc99460 to your computer and use it in GitHub Desktop.
Save MarcScott/39e919a9c95152f155fb27686bc99460 to your computer and use it in GitHub Desktop.
surrogate.tv buggy
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