import curses
import time
import queue
import sys

class Game:
    
    def __init__(self, graphics, serial, stdscr, play, framerate=30):
        self.play = play
        self.stdscr = stdscr
        self.graphics = graphics
        self.serial = serial
        self.sprites = set()
        self.framerate = framerate
        self.input_queue = queue.Queue()
        self.keys = set()

    def send_input(self, name):
        self.input_queue.put(name)

    def handle_events(self):
        self.keys = set()

        try:
            while True:
                key = self.stdscr.getkey().lower()
                if key == '\x1b':
                    self.play.exec_game('menu')
                    return
                self.keys.add(key)
        except curses.error:
            pass

        try:
            while True:
                item = self.input_queue.get_nowait().lower()
                self.keys.add(item)
        except queue.Empty:
            pass
        
    def loop(self):
        self.graphics.clear()
        for sprite in self.sprites:
            sprite.draw(self.graphics)

        self.graphics.draw(self.serial)
        time.sleep(1 / self.framerate)

    def stop(self):
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            self.handle_events()
            self.loop()

@@ -44,16 +44,207 @@
 rt sys%0A%0A
+BUZZER = 15%0ABEEPER = 13%0A%0Atry:%0A    import requests%0A    import RPi.GPIO as gpio%0A    gpio.setmode(gpio.BOARD)%0A    gpio.setup(BUZZER, gpio.OUT)%0A    gpio.setup(BEEPER, gpio.OUT)%0Aexcept:%0A    pass%0A%0A
 class Ga
@@ -246,21 +246,16 @@
 ss Game:
-%0A    
 %0A    def
@@ -1544,8 +1544,340 @@
 .loop()%0A
+%0A    def trigger(self, item, act):%0A        if kind == %22alert%22 and requests:%0A            requests.get(%22localhost:5000/%7B%7D/a/7%22.format(act))%0A        elif kind == %22buzzer%22 and gpio:%0A            gpio.output(BUZZER, 1 if act == %22on%22 else 0)%0A        elif kind == %22beeper%22 and gpio:%0A            gpio.output(BEEPER, 1 if act == %22on%22 else 0)%0A

Add noise makers to game