Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Last active May 12, 2021 17:08
Show Gist options
  • Save MarcScott/3ae959ae20a2e9aa38ea0cb0ac7891ec to your computer and use it in GitHub Desktop.
Save MarcScott/3ae959ae20a2e9aa38ea0cb0ac7891ec to your computer and use it in GitHub Desktop.
import logging
from gpiozero import Robot
from time import sleep
from surrortg import Game
from surrortg.inputs import Joystick
#robot = Robot(left=(19, 26), right=(6, 13))
class MyJoystick(Joystick):
def __init__(self, score_cb):
# Store reference to score callback function
self.score_cb = score_cb
# Set up the robot
self.robot = Robot(left=(19, 26), right=(6, 13))
async def handle_coordinates(self, x, y, seat=0):
#Handle robot directions
if y == 1.0:
self.robot.forward()
self.score_cb()
elif y == -1.0:
self.robot.backward()
self.score_cb()
elif x == 1.0:
self.robot.left()
self.score_cb()
elif x == -1.0:
self.robot.right()
self.score_cb()
elif y == 0.0 and x == 0.0:
self.robot.stop()
async def reset(self, seat=0):
#Stop the robot when the game ends
self.robot.stop()
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 no of moves 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