def check_events(self): """Input checking.""" cursor_keys = ("move_up", "move_down", "move_left", "move_right") screen_keys = ("quit", "new_game") for e in event.get(): k = "" if e.type == JOYAXISMOTION: if e.axis == 1: if -1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "up" if 1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "down" if 0 == round(e.value): _type = "keyup" k = self._last_joystick_action if e.axis == 0: if -1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "left" if 1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "right" if 0 == round(e.value): _type = "keyup" k = self._last_joystick_action elif e.type in (KEYDOWN, KEYUP): k = key.name(e.key) if e.type == KEYDOWN: _type = "keydown" elif e.type == KEYUP: _type = "keyup" if k in State.controls: if self._last_joystick_action: sprite = State.joystick if _type == "keyup": self._last_joystick_action = None else: sprite = State.cursor control = getattr(actions, State.controls[k]) if _type == "keydown": State.pressed.append(control) ctrl = State.controls[k] if ctrl in cursor_keys: control (sprite) else: control(State.screen) elif _type == "keyup": if control in State.pressed: del State.pressed[State.pressed.index(control)] actions.move_stop(sprite) if State.pressed: State.pressed[-1](sprite)
def check_events(self): """Input checking.""" cursor_keys = ("move_up", "move_down", "move_left", "move_right") screen_keys = ("quit", "new_game") for e in event.get(): if e.type in (KEYDOWN, KEYUP): k = key.name(e.key) if k in State.controls: control = getattr(actions, State.controls[k]) if e.type == KEYDOWN: State.pressed.append(control) ctrl = State.controls[k] if ctrl in cursor_keys: control(State.cursor) else: control(State.screen) if e.type == KEYUP: if control in State.pressed: del State.pressed[State.pressed.index(control)] actions.move_stop(State.cursor) if State.pressed: State.pressed[-1](State.cursor)
def check_events(self): """Input checking.""" cursor_keys = ("move_up", "move_down", "move_left", "move_right") screen_keys = ("quit", "new_game") for e in event.get(): k = "" if e.type == JOYAXISMOTION: if e.axis == 1: if -1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "up" if 1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "down" if 0 == round(e.value): _type = "keyup" k = self._last_joystick_action if e.axis == 0: if -1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "left" if 1 == round(e.value): _type = "keydown" self._last_joystick_action = k = "right" if 0 == round(e.value): _type = "keyup" k = self._last_joystick_action elif e.type in (KEYDOWN, KEYUP): k = key.name(e.key) if e.type == KEYDOWN: _type = "keydown" elif e.type == KEYUP: _type = "keyup" if k in State.controls: if self._last_joystick_action: sprite = State.joystick if _type == "keyup": self._last_joystick_action = None else: sprite = State.cursor control = getattr(actions, State.controls[k]) if _type == "keydown": State.pressed.append(control) ctrl = State.controls[k] if ctrl in cursor_keys: control (sprite) else: control(State.screen) if _type == "keyup": if control in State.pressed: del State.pressed[State.pressed.index(control)] actions.move_stop(sprite) if State.pressed: State.pressed[-1](sprite) def update(self): """Called whenever the game clock determines that game mechanics are ready to be updated.""" self.check_events() def draw(self): """Called whenever the game clock determines that a frame is ready to be drawn.""" State.screen.draw() def run(self, screen): """Main game loop.""" State.running = True State.restore(screen) while State.running: State.clock.tick() if State.clock.update_ready(): self.update() if State.clock.frame_ready(): self.draw()