def __init__(self, **kwargs): super(MooseInRocketGame, self).__init__(**kwargs) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self._on_keyboard_down) self._keyboard.bind(on_key_up=self._on_keyboard_up) # self.rocket = Rocket() self.enemies_factory = Enemies() with self.canvas: self.background_handler = BackgroundHandler() self.start_place = StartPlace() self.rocket = Rocket() self.ruby = Ruby() self.score = Score() self.start_screen = StartScreen() self.score_screen = ScoreScreen()
class MooseInRocketGame(Widget): rocket = ObjectProperty(None) background_handler = ObjectProperty(None) game_started = False def __init__(self, **kwargs): super(MooseInRocketGame, self).__init__(**kwargs) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self._on_keyboard_down) self._keyboard.bind(on_key_up=self._on_keyboard_up) # self.rocket = Rocket() self.enemies_factory = Enemies() with self.canvas: self.background_handler = BackgroundHandler() self.start_place = StartPlace() self.rocket = Rocket() self.ruby = Ruby() self.score = Score() self.start_screen = StartScreen() self.score_screen = ScoreScreen() def _keyboard_closed(self): self._keyboard.unbind(on_key_down=self._on_keyboard_down) self._keyboard = None def update(self, dt): if not self.game_started: return with self.canvas: self.enemies_factory.generate() self.background_handler.move() self.start_place.move() self.rocket.move() if self.ruby: self.ruby.move() if self.rocket.image.collide_widget(self.ruby): self.score.up() self.ruby.hide() if not self.rocket.dead: for enemie in self.enemies_factory.enemies: enemie.move() if self.rocket.image.collide_widget(enemie.collide): self.rocket.die() return if self.rocket.dead: for enemie in self.enemies_factory.enemies: enemie.hide() self.score_screen.show() def restart(self): if self.game_started: self.start_place.restart() self.rocket.restart() self.enemies_factory.reset() self.game_started = False def on_touch_down(self, *args, **kwargs): self._on_keyboard_down(1,2,3,4) def on_touch_up(self, *args, **kwargs): self._on_keyboard_up(1,2) def _on_keyboard_down(self, keyboard, keycode, text, modifiers): if self.start_screen.is_active(): self.start_screen.hide() return if self.score_screen.is_active(): self.restart() self.score_screen.hide() return if not self.game_started: self.game_started = True self.rocket.force = True def _on_keyboard_up(self, keyboard, keycode): self.rocket.force = False