def test_default_event_handling(self): screen = SampleGameScreenUI(GameConfig(), GameModel()) loop_events = GameLoopEvents(screen) loop_events.loop_setup() loop = GameLoop(loop_events) # Python sorcery! loop._GameLoop__handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN)) self.assertTrue(screen.evh.is_called)
def test_dry_run(self, pygame_init, clock_tick, flip, quit): config = GameConfig() model = GameModel() screen = GameScreen(config, model) loop_events = LoopEventsMock(config, screen) loop = GameLoop(loop_events) loop.go() self.assertTrue(pygame_init.called) self.assertTrue(clock_tick.called) self.assertTrue(flip.called) self.assertTrue(quit.called)
def test_event_handling_basic(self): """ Test basic event handling of non-keyboard-based events. """ screen = GameScreen(GameConfig(), GameModel()) loop_events = GameLoopEvents(screen) evh1 = EventHandlerMock() loop_events.add_event_handler(self.btn_down_event, evh1.handle_event) loop = GameLoop(loop_events) # More Python sorcery! loop._GameLoop__handle_event(self.btn_down_event) self.assertTrue(evh1.is_called)
def test_event_handling_kbd_basic(self): """ Test basic event handling of keyboard-based events. """ screen = GameScreen(GameConfig(), GameModel()) loop_events = KeyboardHandlingLoopEventsMock(screen) evh1 = EventHandlerMock() loop_events.key_controls.register_key(pygame.K_UP, evh1.handle_event) loop_events.attach_event_handlers() loop = GameLoop(loop_events) # More Python sorcery! loop._GameLoop__handle_event(self.press_up_key_event) self.assertTrue(evh1.is_called)
def test_event_handling_multiple(self): """ Test that multiple event handlers can listen on the same non-keyboard-based event. """ screen = GameScreen(GameConfig(), GameModel()) loop_events = GameLoopEvents(screen) evh1 = EventHandlerMock() evh2 = EventHandlerMock() loop_events.add_event_handler(self.btn_down_event, evh1.handle_event) loop_events.add_event_handler(self.btn_down_event, evh2.handle_event) loop = GameLoop(loop_events) # More Python sorcery! loop._GameLoop__handle_event(self.btn_down_event) self.assertTrue(evh1.is_called) self.assertTrue(evh2.is_called)
def test_dry_run(self, pygame_init, clock_tick, flip, quit): config = GameConfig() config.set_config_val("clock_rate", 60) config.set_config_val("window_size", (600, 600)) config.set_config_val("window_title", "SNAKE!") config.set_config_val("debug_mode", True) config.set_config_val("log_to_terminal", True) config.set_config_val("difficulty", 1) screen = SnakeScreen(config, (10, 10)) loop_events = SnakeGameEvents(screen) loop = GameLoop(loop_events, is_test=True) loop.go() self.assertTrue(pygame_init.called) self.assertTrue(clock_tick.called) self.assertTrue(flip.called) self.assertTrue(quit.called)
from components.core import GameConfig from components.core import GameLoop from components.core import GameLoopEvents from components.core import GameScreen from components.shapes import Point class DisplayRectangle(GameScreen): def __init__(self, screen_size): super(DisplayRectangle, self).__init__(screen_size) self.__rectangle = Rectangle(Point(10, 10), Point(40, 40), Colors.BLACK) @property def rectangle(self): return self.__rectangle def draw_screen(self, window): super(DisplayRectangle, self).draw_screen(window) self.rectangle.draw(window) if __name__ == "__main__": config = GameConfig() config.clock_rate = 12 config.window_size = [500, 500] config.window_title = "Rectangle Test" rscreen = DisplayRectangle(config.window_size) rloop_events = GameLoopEvents(config, rscreen) loop = GameLoop(rloop_events) loop.go()
def loop_setup(self): super(ImageLoader, self).loop_setup() init_x = super(ImageLoader, self).config.window_size[GameConfig.WIDTH_INDEX] - \ self.game_screen.meteormon.width self.game_screen.meteormon.position = Point(init_x, 0) class ImageScreen(GameScreen): @property def meteormon(self): return self.__meteormon def setup(self): super(ImageScreen, self).setup() self.__meteormon = Image(os.path.join("sample_sprites","meteormon_clueless.png")) def draw_screen(self, window): super(ImageScreen, self).draw_screen(window) window.fill(Colors.WHITE) self.meteormon.draw(window) config = GameConfig() config.window_size = [500, 500] config.clock_rate = 12 config.window_title = "Image Class Test" screen = ImageScreen(config.window_size) image_gle = ImageLoader(config, screen) gl = GameLoop(image_gle) gl.go()
def loop_invariant(self): parent_invariant = super(AmoebaLoopEvents, self).loop_invariant() return self.__i < self.__limit and parent_invariant def loop_event(self): self.window.fill(Colors.WHITE) upper_bound = len(super(AmoebaLoopEvents, self).game_screen.mutating_shape.point_list) - 1 insertion_point = random.randint(0, upper_bound) rand_x = random.randint(0, self.config.window_size[GameConfig.WIDTH_INDEX]) rand_y = random.randint(0, self.config.window_size[GameConfig.HEIGHT_INDEX]) super(AmoebaLoopEvents, self).game_screen.mutating_shape.add_point(Point(rand_x, rand_y), insertion_point) super(AmoebaLoopEvents, self).loop_event() self.__i += 1 gameconfig = GameConfig() gameconfig.window_size = [500, 500] gameconfig.clock_rate = 12 gameconfig.window_title = "Mutating Amoeba" game_screen = AmoebaScreen(gameconfig.window_size) loop_events = AmoebaLoopEvents(gameconfig, game_screen) gameloop = GameLoop(loop_events) gameloop.go()
A simple demo of using the framework that randomly draws gray circles on a white canvas. @author Chad Estioco """ class GrayCircle(GameLoopEvents): def __init__(self, config): super(GrayCircle, self).__init__(config, GameScreen([500,500])) self.__limit = 100 self.__count = 0 def loop_invariant(self): parent_invariant = super(GrayCircle, self).loop_invariant() return self.__count < self.__limit and parent_invariant def loop_event(self): x = random.randint(0, 500) y = random.randint(0, 500) pygame.draw.circle(self.window, [218, 218, 218], [x, y], 50, 2) self.__count += 1 gconfig = GameConfig() gconfig.clock_rate = 10 gconfig.window_size = [500, 500] gconfig.window_title = "Framework test" gc_object = GrayCircle(gconfig) game_loop = GameLoop(gc_object) game_loop.go()
from components.core import GameConfig, GameLoop from grid_model import Grid from screen import GridLoopEvents, GridScreen if __name__ == "__main__": config = GameConfig(window_size=(400, 600), debug_mode=False) model = Grid((10, 10), max_size=(400, 400)) screen = GridScreen(config, model) loop_events = GridLoopEvents(screen) loop = GameLoop(loop_events) loop.go()