def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.display.set_caption('After You!') self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode( self.screen_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component(MovementComponent()) self.component_manager.register_component(ExampleComponent()) self.component_manager.register_component(AnimationComponent()) self.component_manager.register_component(DrawComponent()) self.component_manager.register_component(InputMovementComponent()) self.component_manager.register_component(DrawHitBoxComponent()) self.component_manager.register_component(DrawCircleComponent()) self.component_manager.register_component(SmokeScreenComponent()) self.component_manager.register_component(PlayerCollisionComponent()) self.component_manager.register_component(DecoyMovementComponent()) self.component_manager.register_component(SelfDestructComponent()) self.component_manager.register_component(SpawnDecoyComponent()) self.component_manager.register_component(DrawScoreComponent()) self.component_manager.register_component(MinefieldComponent()) self.component_manager.register_component(DrawTimerComponent()) self.component_manager.register_component(UpdateTimerComponent()) self.component_manager.register_component(SpeedBoostComponent()) self.component_manager.register_component(ButtonInterpreterComponent()) self.component_manager.register_component(DrawActionsComponent()) self.component_manager.register_component(SpawnVortexComponent()) self.component_manager.register_component(DrawVortextComponent()) self.component_manager.register_component(GrowVortextComponent()) self.component_manager.register_component(TrapComponent()) self.component_manager.register_component(HideComponent()) self.component_manager.register_component(AIActionComponent()) self.component_manager.register_component(AIMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('data', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.renderer = GLRenderer() self.renderer.resize(self.screen_size) self.view = View([SimpleLayer('draw'), SimpleLayer('ui')])
def __init__(self): self.render_engine = RenderEngine() self.input_manager = InputManager() self.player = load_game() if self.player is None: self.player = Player() game_map = generate_map(1) game_map.take_over_monster(self.player) game_map = self.player.entity.game_map map_scene = MapScene(self.player, game_map) self.current_scene = map_scene
def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=48000) pygame.init() pygame.display.set_caption("It's All Come to This") self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size, pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_module(component) self.component_manager.register_module(event) self.component_manager.register_module(locationcomponets) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('entity', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.resource_manager.register_loader('text', LoadText) self.resource_manager.register_loader('event', LoadEvent) self.input_manager = InputManager() self.view = View(pygame.display.get_surface(), [SolidBackgroundLayer((128,192,128,255)), SimpleLayer('draw-location'), SimpleLayer('draw-human'), SimpleLayer('draw-ui')])
def __init__(self): self.screen_size = (500,500) self.map_size = (128,128) self.tile_size = (5,5) self.world_size = (self.tile_size[0] * self.map_size[0], self.tile_size[1] * self.map_size[1]) self.grid = Grid(self.world_size[0],self.world_size[1]) GridGenerator(self.grid,Vec2(self.tile_size[0],self.tile_size[1])) pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((500,500)) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component('MovementComponent', MovementComponent()) self.component_manager.register_component('ExampleComponent', ExampleComponent()) self.component_manager.register_component('TileDraw', TileDraw()) self.component_manager.register_component('InputMovementComponent', InputMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(os.path.join(sys.path[0], 'res')) self.resource_manager.register_loader('definition', LoadEntityDefinition) self.resource_manager.register_loader('sprite', LoadImage) self.input_manager = InputManager() self.input_manager.init_joysticks() self.entities_to_update = [] self.entities_to_input = [] self.entities_to_draw_tiles = []
def __init__(self): self.inputMgr = InputManager.get_instance() self.eventMgr = EventManager() self.recorder = None self.state = self.ST_MENU self.rootMenu = None
def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.display.set_caption('After You!') self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component(MovementComponent()) self.component_manager.register_component(ExampleComponent()) self.component_manager.register_component(AnimationComponent()) self.component_manager.register_component(DrawComponent()) self.component_manager.register_component(InputMovementComponent()) self.component_manager.register_component(DrawHitBoxComponent()) self.component_manager.register_component(DrawCircleComponent()) self.component_manager.register_component(SmokeScreenComponent()) self.component_manager.register_component(PlayerCollisionComponent()) self.component_manager.register_component(DecoyMovementComponent()) self.component_manager.register_component(SelfDestructComponent()) self.component_manager.register_component(SpawnDecoyComponent()) self.component_manager.register_component(DrawScoreComponent()) self.component_manager.register_component(MinefieldComponent()) self.component_manager.register_component(DrawTimerComponent()) self.component_manager.register_component(UpdateTimerComponent()) self.component_manager.register_component(SpeedBoostComponent()) self.component_manager.register_component(ButtonInterpreterComponent()) self.component_manager.register_component(DrawActionsComponent()) self.component_manager.register_component(SpawnVortexComponent()) self.component_manager.register_component(DrawVortextComponent()) self.component_manager.register_component(GrowVortextComponent()) self.component_manager.register_component(TrapComponent()) self.component_manager.register_component(HideComponent()) self.component_manager.register_component(AIActionComponent()) self.component_manager.register_component(AIMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('data', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.renderer = GLRenderer() self.renderer.resize(self.screen_size) self.view = View([SimpleLayer('draw'), SimpleLayer('ui')])
def __init__(self, size, core, cache, resolution_factor): self.core = core self.cache = cache self.fonts = {} self.background = None self.current_screen = library_index # Init variables in init self.base_size = None self.size = None self.screens = None self.track = None self.input_manager = InputManager(size) self.down_bar_objects = ScreenObjectsManager() self.down_bar = None self.keyboard = None self.update_type = BaseScreen.update_all self.resolution_factor = resolution_factor self.init_manager(size)
class Game: def __init__(self): self.render_engine = RenderEngine() self.input_manager = InputManager() self.player = load_game() if self.player is None: self.player = Player() game_map = generate_map(1) game_map.take_over_monster(self.player) game_map = self.player.entity.game_map map_scene = MapScene(self.player, game_map) self.current_scene = map_scene def run(self): while not tcod.console_is_window_closed(): self.render_engine.render_scene(self.current_scene) if self.current_scene.data is not None: input_return = self.current_scene.manage_input(None) else: game_input = self.input_manager.get_input() input_return = self.current_scene.manage_input(game_input) if input_return is None: continue if input_return['action']: if input_return['action'] == 'confirm': input_return = self.current_scene.need_confirm if input_return['action'] == 'cancel' or input_return['action'] == 'cancel_with_action': if self.current_scene.previous_scene is not None: current = self.current_scene self.current_scene = self.current_scene.previous_scene current.previous_scene = None if input_return['action'] == 'cancel_with_action' and isinstance(self.current_scene, MapScene): self.current_scene.player_took_action = True self.current_scene.render_next = True del current else: if self.current_scene.need_confirm is not None: return True self.current_scene.need_confirm = input_return add_log_message( LogMessage( get_message("quit_game.confirm"), tcod.light_cyan ) ) self.current_scene.render_next = True elif input_return['action'] == 'change_scene': self.current_scene = input_return['scene'] self.current_scene.render_next = True if not self.player.entity.dead: save_game(self.player)
def __init__(self, min_cycle_time=10): self.min_cycle_time = min_cycle_time self._run = False self._last_update = 0 self._last_mouse_pos = None self._selecting = False self._select_start_pos = None self._selection_rect = None self._mouse_right_down = False pygame.init() screen_width, screen_height = screen_size = (1024, 768) self._screen = pygame.display.set_mode(screen_size, pygame.DOUBLEBUF) self._map = Map('default') self._camera = Camera(screen_size) self._renderer = Renderer(self._screen, self._camera) self._objects = ObjectManager(self._map.size) self._event_mgr = EventManager() self._event_mgr.subscribe(pygame.QUIT, self._handle_quit) self._event_mgr.subscribe(self._camera.move_event, self._camera_moved) self._input = InputManager(self._event_mgr) self._input.set_keybind(pygame.K_ESCAPE, self.stop) self._input.set_keybind(pygame.K_q, self.stop) self._input.set_hotarea((0, 0, screen_width, 2), self._camera.set_move, {'y': -1}) self._input.set_hotarea((0, screen_height - 2, screen_width, 2), self._camera.set_move, {'y': 1}) self._input.set_hotarea((0, 0, 2, screen_height - 2), self._camera.set_move, {'x': -1}) self._input.set_hotarea((screen_width - 2, 0, 2, screen_height), self._camera.set_move, {'x': 1}) self._input.set_hotarea((2, 2, screen_width - 4, screen_height - 4), self._camera.stop_moving) self._event_mgr.subscribe(self._input.mouse_drag_start, self._select_start) self._event_mgr.subscribe(self._input.mouse_drag_update, self._select_update) self._event_mgr.subscribe(self._input.mouse_drag_end, self._select_end) self._event_mgr.subscribe(self._input.lclick, self._leftclick) self._event_mgr.subscribe(self._input.rclick, self._rightclick) self._event_mgr.subscribe(self._input.rsclick, self._right_shiftclick)
def __init__(self, size, core, cache, resolution_factor): self.core = core self.cache = cache self.fonts = {} self.background = None self.currentScreen = mainScreenIndex # Init variables in init self.baseSize = None self.size = None self.screens = None self.track = None self.input_manager = InputManager(size) self.down_bar_objects = ScreenObjectsManager() self.down_bar = None self.keyboard = None self.updateType = BaseScreen.update_all self.resolution_factor = resolution_factor self.init_manager(size)
def __init__(self, screen_size, resource_path): self.mode = None self.screen_size = screen_size self.map_size = (128,128) self.tile_size = (32,32) self.world_size = (self.tile_size[0] * self.map_size[0], self.tile_size[1] * self.map_size[1]) self.world_rooms = (8,8) pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component('MovementComponent', MovementComponent()) self.component_manager.register_component('ExampleComponent', ExampleComponent()) self.component_manager.register_component('AnimationComponent', AnimationComponent()) self.component_manager.register_component('DrawComponent', DrawComponent()) self.component_manager.register_component('InputMovementComponent', InputMovementComponent()) self.component_manager.register_component('PlayerCollisionComponent', PlayerCollisionComponent()) self.component_manager.register_component('ZombieAIComponent', ZombieAIComponent()) self.component_manager.register_component('ZombieAIComponent', ZombieAIComponent()) self.component_manager.register_component('CarComponent', CarComponent()) self.component_manager.register_component('DrawHitBoxComponent', DrawHitBoxComponent()) self.component_manager.register_component('AttackComponent', AttackComponent()) self.component_manager.register_component('ItemComponent', ItemComponent()) self.component_manager.register_component('InputActionComponent', InputActionComponent()) self.component_manager.register_component('DeadComponent', DeadComponent()) self.component_manager.register_component('ZombieCollisionComponent', ZombieCollisionComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('definition', LoadEntityData) self.resource_manager.register_loader('sprite', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.input_manager.init_joysticks()
class Game(object): def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=48000) pygame.init() pygame.display.set_caption("It's All Come to This") self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size, pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_module(component) self.component_manager.register_module(event) self.component_manager.register_module(locationcomponets) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('entity', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.resource_manager.register_loader('text', LoadText) self.resource_manager.register_loader('event', LoadEvent) self.input_manager = InputManager() self.view = View(pygame.display.get_surface(), [SolidBackgroundLayer((128,192,128,255)), SimpleLayer('draw-location'), SimpleLayer('draw-human'), SimpleLayer('draw-ui')]) def run(self, mode): self.entity_manager.add_entity(Entity('camp')) self.entity_manager.add_entity(Entity('report')) origin = Vec2d(600, 200) num_humans = 6 names = self.resource_manager.get('text','names.txt').split('\n') for i in range(num_humans): pos = Vec2d(100,0).rotated(360.0*i/(num_humans)) + origin image = random.choice(('male.png', 'female.png')) self.entity_manager.add_entity(Entity('human', name=random.choice(names), home_x=pos.x, home_y=pos.y, image=image)) self.entity_manager.add_entity(Entity('mouse')) self.entity_manager.add_entity(Entity('farm')) self.entity_manager.add_entity(Entity('wall')) self.entity_manager.add_entity(Entity('well')) self.entity_manager.add_entity(Entity('rest-camp')) self.entity_manager.add_entity(Entity('work-camp')) self.entity_manager.add_entity(Entity('explore')) self.entity_manager.add_entity(Entity('infirmary')) self.entity_manager.add_entity(Entity('construction-site')) self.entity_manager.add_entity(Entity('dump')) self.entity_manager.add_entity(Entity('hospital')) self.entity_manager.add_entity(Entity('rival-camp')) self.entity_manager.add_entity(Entity('water-tower')) self.entity_manager.add_entity(Entity('forest')) self.entity_manager.add_entity(Entity("camp")) self.entity_manager.add_entity(Entity('camp-food-meter-ui')) self.entity_manager.add_entity(Entity('camp-defense-meter-ui')) self.entity_manager.add_entity(Entity('camp-shelter-meter-ui')) self.entity_manager.add_entity(Entity('camp-water-meter-ui')) self.entity_manager.add_entity(Entity('camp-medicine-meter-ui')) self.entity_manager.add_entity(Entity('button')) self.entity_manager.add_entity(Entity('info-window')) self.entity_manager.commit_changes() self.change_mode(mode) self.running = True self.entity_manager.commit_changes() while self.running: dt = self.clock.tick(60) / 1000.0 events = self.input_manager.process_events() for event in events: if event.target == 'GAME': if event.action == 'QUIT' and event.value > 0: self.running = False elif event.action == 'FULLSCREEN' and event.value > 0: pygame.display.toggle_fullscreen() elif event.action == 'RELOAD' and event.value > 0: self.resource_manager.clear() else: self.mode.handle_event(event) self.mode.update(dt) self.mode.draw() self.entity_manager.commit_changes() pygame.display.flip() def change_mode(self, new_mode): print self.mode, '->', new_mode if self.mode: self.mode.leave() self.mode = new_mode self.mode.enter() self.entity_manager.commit_changes()
class ScreenManager(): def __init__(self, size, core, cache, resolution_factor): self.core = core self.cache = cache self.fonts = {} self.background = None self.current_screen = library_index # Init variables in init self.base_size = None self.size = None self.screens = None self.track = None self.input_manager = InputManager(size) self.down_bar_objects = ScreenObjectsManager() self.down_bar = None self.keyboard = None self.update_type = BaseScreen.update_all self.resolution_factor = resolution_factor self.init_manager(size) def init_manager(self, size): self.size = size self.background = DynamicBackground(self.size) self.base_size = self.size[1] / self.resolution_factor font = resource_filename(Requirement.parse("mopidy-touchscreen"), "mopidy_touchscreen/icomoon.ttf") self.fonts['base'] = pygame.font.SysFont("arial", int(self.base_size * 0.9)) self.fonts['icon'] = pygame.font.Font(font, int(self.base_size * 0.9)) try: self.screens = [ SearchScreen(size, self.base_size, self, self.fonts), MainScreen(size, self.base_size, self, self.fonts, self.cache, self.core, self.background), Tracklist(size, self.base_size, self, self.fonts), LibraryScreen(size, self.base_size, self, self.fonts), PlaylistScreen(size, self.base_size, self, self.fonts), MenuScreen(size, self.base_size, self, self.fonts, self.core) ] except: traceback.print_exc() self.track = None # Menu buttons button_size = (self.size[0] / 6, self.base_size) # Search button button = TouchAndTextItem(self.fonts['icon'], u" \ue986", (0, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_0", button) x = button.get_right_pos() # Main button button = TouchAndTextItem(self.fonts['icon'], u" \ue600", (x, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_1", button) x = button.get_right_pos() # Tracklist button button = TouchAndTextItem(self.fonts['icon'], u" \ue60d", (x, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_2", button) x = button.get_right_pos() # Library button button = TouchAndTextItem(self.fonts['icon'], u" \ue604", (x, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_3", button) x = button.get_right_pos() # Playlist button button = TouchAndTextItem(self.fonts['icon'], u" \ue605", (x, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_4", button) x = button.get_right_pos() # Menu button button = TouchAndTextItem(self.fonts['icon'], u" \ue60a", (x, self.size[1] - self.base_size), button_size, center=True) self.down_bar_objects.set_touch_object("menu_5", button) # Down bar self.down_bar = pygame.Surface( (self.size[0], self.size[1] - self.base_size), pygame.SRCALPHA) self.down_bar.fill((0, 0, 0, 200)) self.options_changed() self.mute_changed(self.core.playback.mute.get()) playback_state = self.core.playback.state.get() self.playback_state_changed(playback_state, playback_state) self.screens[menu_index].check_connection() self.change_screen(self.current_screen) self.update_type = BaseScreen.update_all def get_update_type(self): if self.update_type == BaseScreen.update_all: self.update_type = BaseScreen.no_update return BaseScreen.update_all else: if self.keyboard: return BaseScreen.no_update else: if self.background.should_update(): return BaseScreen.update_all else: if self.screens[self.current_screen].should_update(): return BaseScreen.update_partial else: return BaseScreen.no_update def update(self, screen): update_type = self.get_update_type() if update_type != BaseScreen.no_update: rects = [] surface = self.background.draw_background() if self.keyboard: self.keyboard.update(surface) else: self.screens[self.current_screen].\ update(surface, update_type, rects) surface.blit(self.down_bar, (0, self.size[1] - self.base_size)) self.down_bar_objects.render(surface) if update_type == BaseScreen.update_all or len(rects) < 1: screen.blit(surface, (0, 0)) pygame.display.flip() else: for rect in rects: screen.blit(surface, rect, area=rect) pygame.display.update(rects) def track_started(self, track): self.track = track self.screens[main_screen_index].track_started(track.track) self.screens[tracklist_index].track_started(track) def track_playback_ended(self, tl_track, time_position): self.screens[main_screen_index].track_playback_ended( tl_track, time_position) def event(self, event): event = self.input_manager.event(event) if event is not None: if self.keyboard is not None: self.keyboard.touch_event(event) elif not self.manage_event(event): self.screens[self.current_screen].touch_event(event) self.update_type = BaseScreen.update_all def manage_event(self, event): if event.type == InputManager.click: objects = \ self.down_bar_objects.get_touch_objects_in_pos( event.current_pos) return self.click_on_objects(objects, event) else: if event.type == InputManager.key and not event.longpress: dir = event.direction if dir == InputManager.right or dir == InputManager.left: if not self.screens[self.current_screen]\ .change_screen(dir): if dir == InputManager.right: self.change_screen(self.current_screen + 1) else: self.change_screen(self.current_screen - 1) return True return False def volume_changed(self, volume): self.screens[main_screen_index].volume_changed(volume) self.update_type = BaseScreen.update_all def playback_state_changed(self, old_state, new_state): self.screens[main_screen_index].playback_state_changed( old_state, new_state) self.update_type = BaseScreen.update_all def mute_changed(self, mute): self.screens[main_screen_index].mute_changed(mute) self.update_type = BaseScreen.update_all def tracklist_changed(self): self.screens[tracklist_index].tracklist_changed() self.update_type = BaseScreen.update_all def options_changed(self): self.screens[menu_index].options_changed() self.update_type = BaseScreen.update_all def change_screen(self, new_screen): if new_screen > -1 and new_screen < len(self.screens): self.down_bar_objects.get_touch_object( "menu_" + str(self.current_screen)).set_active(False) self.current_screen = new_screen self.down_bar_objects.get_touch_object( "menu_" + str(new_screen)).set_active(True) self.update_type = BaseScreen.update_all def click_on_objects(self, objects, event): if objects is not None: for key in objects: if key[:-1] == "menu_": self.change_screen(int(key[-1:])) return True return False def playlists_loaded(self): self.screens[playlist_index].playlists_loaded() self.update_type = BaseScreen.update_all def search(self, query, mode): self.screens[search_index].search(query, mode) self.update_type = BaseScreen.update_all def resize(self, event): self.init_manager(event.size) self.update_type = BaseScreen.update_all def open_keyboard(self, input_listener): self.keyboard = Keyboard(self.size, self.base_size, self, self.fonts, input_listener) self.update_type = BaseScreen.update_all def close_keyboard(self): self.keyboard = None self.update_type = BaseScreen.update_all
class Game(object): """Manages other game modules.""" def __init__(self, min_cycle_time=10): self.min_cycle_time = min_cycle_time self._run = False self._last_update = 0 self._last_mouse_pos = None self._selecting = False self._select_start_pos = None self._selection_rect = None self._mouse_right_down = False pygame.init() screen_width, screen_height = screen_size = (1024, 768) self._screen = pygame.display.set_mode(screen_size, pygame.DOUBLEBUF) self._map = Map('default') self._camera = Camera(screen_size) self._renderer = Renderer(self._screen, self._camera) self._objects = ObjectManager(self._map.size) self._event_mgr = EventManager() self._event_mgr.subscribe(pygame.QUIT, self._handle_quit) self._event_mgr.subscribe(self._camera.move_event, self._camera_moved) self._input = InputManager(self._event_mgr) self._input.set_keybind(pygame.K_ESCAPE, self.stop) self._input.set_keybind(pygame.K_q, self.stop) self._input.set_hotarea((0, 0, screen_width, 2), self._camera.set_move, {'y': -1}) self._input.set_hotarea((0, screen_height - 2, screen_width, 2), self._camera.set_move, {'y': 1}) self._input.set_hotarea((0, 0, 2, screen_height - 2), self._camera.set_move, {'x': -1}) self._input.set_hotarea((screen_width - 2, 0, 2, screen_height), self._camera.set_move, {'x': 1}) self._input.set_hotarea((2, 2, screen_width - 4, screen_height - 4), self._camera.stop_moving) self._event_mgr.subscribe(self._input.mouse_drag_start, self._select_start) self._event_mgr.subscribe(self._input.mouse_drag_update, self._select_update) self._event_mgr.subscribe(self._input.mouse_drag_end, self._select_end) self._event_mgr.subscribe(self._input.lclick, self._leftclick) self._event_mgr.subscribe(self._input.rclick, self._rightclick) self._event_mgr.subscribe(self._input.rsclick, self._right_shiftclick) def run(self): """Run the main game loop.""" self._load() self._last_update = pygame.time.get_ticks() self._run = True while self._run: gametime = pygame.time.get_ticks() time_passed = gametime - self._last_update if time_passed < self.min_cycle_time: pygame.time.wait(1) # give other threads some cpu time continue self._update(gametime, time_passed) self._draw() self._shutdown() def _load(self): unit_tex = self._renderer.load_texture('cross.png') for x in range(0, 48, 16): unit_pos = (x, 16) unit_rect = Rect(unit_pos, self._renderer.texture_size(unit_tex)) unit_id = self._objects.create(Unit, unit_rect) self._renderer.assign_texture(unit_id, unit_tex) def _shutdown(self): pass def _update(self, gametime, time_passed): self._event_mgr.update() self._objects.update(gametime) self._camera.update(time_passed) self._last_update = gametime #noinspection PyUnusedLocal def _camera_moved(self, event): if self._selecting: self._update_selection_rectangle(self._last_mouse_pos) def _select_start(self, event): self._selecting = True self._select_start_pos = self._camera.point_to_map(event.pos) self._update_selection_rectangle(event.pos) #noinspection PyUnusedLocal def _select_end(self, event): self._selecting = False self._objects.select_area(self._selection_rect) def _select_update(self, event): self._last_mouse_pos = event.pos self._update_selection_rectangle(event.pos) def _update_selection_rectangle(self, pos): map_pos = self._camera.point_to_map(pos) self._selection_rect = Rect(min(self._select_start_pos[0], map_pos[0]), min(self._select_start_pos[1], map_pos[1]), abs(map_pos[0] - self._select_start_pos[0]), abs(map_pos[1] - self._select_start_pos[1])) def _leftclick(self, event): self._objects.select_at(self._camera.point_to_map(event.pos)) def _rightclick(self, event): self._objects.send_selected(self._camera.point_to_map(event.pos)) def _right_shiftclick(self, event): self._objects.send_selected(self._camera.point_to_map(event.pos), True) def _draw(self): self._renderer.frame_start() self._renderer.draw_map(self._map) objs = self._objects.query(self._camera.view_rect) self._renderer.draw_objects(objs) if self._selecting: self._renderer.draw_rectangle(self._selection_rect, (255, 0, 0)) self._renderer.frame_end() def _draw_objects(self, surface, area): pass #noinspection PyUnusedLocal def _handle_quit(self, event): self.stop() def stop(self): self._run = False
class Game(object): def __init__(self): self.screen_size = (500,500) self.map_size = (128,128) self.tile_size = (5,5) self.world_size = (self.tile_size[0] * self.map_size[0], self.tile_size[1] * self.map_size[1]) self.grid = Grid(self.world_size[0],self.world_size[1]) GridGenerator(self.grid,Vec2(self.tile_size[0],self.tile_size[1])) pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((500,500)) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component('MovementComponent', MovementComponent()) self.component_manager.register_component('ExampleComponent', ExampleComponent()) self.component_manager.register_component('TileDraw', TileDraw()) self.component_manager.register_component('InputMovementComponent', InputMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(os.path.join(sys.path[0], 'res')) self.resource_manager.register_loader('definition', LoadEntityDefinition) self.resource_manager.register_loader('sprite', LoadImage) self.input_manager = InputManager() self.input_manager.init_joysticks() self.entities_to_update = [] self.entities_to_input = [] self.entities_to_draw_tiles = [] def register_for_updates(self, entity): self.entities_to_update.append(entity) def register_for_input(self, entity): self.entities_to_input.append(entity) def register_for_draw_tiles(self,entity): self.entities_to_draw_tiles.append(entity) def run(self): #test_entity = Entity('test-include') character = Entity('character') self.characters = [character for m in xrange(4)] self.renderer = Render(self) self.current_camera = 0 while True: dt = self.clock.tick() / 1000.0 for e in pygame.event.get(): if e.type == pygame.QUIT: sys.exit() elif e.type == pygame.KEYDOWN: if e.key == pygame.K_TAB: self.current_camera = (self.current_camera +1)%4 if e.key == pygame.K_a: #self.cameras[self.current_camera].props.dx = 1 pass elif e.type == pygame.KEYUP: if e.key == pygame.K_a: #self.cameras[self.current_camera].props.dx = 0 pass if e.type == pygame.JOYAXISMOTION or \ e.type == pygame.JOYBALLMOTION or \ e.type == pygame.JOYBUTTONDOWN or \ e.type == pygame.JOYBUTTONUP or \ e.type == pygame.JOYHATMOTION or \ e.type == pygame.KEYDOWN or \ e.type == pygame.KEYUP: event = InputEvent(e) for entity in self.entities_to_update: if e.type == pygame.JOYAXISMOTION: entity.handle('move', event) else: entity.handle('input', event) for entity in self.entities_to_draw_tiles: entity.handle('draw-tiles',self.world_surface) for entity in self.entities_to_update: entity.handle('update', dt) self.renderer.render()
class ScreenManager(): def __init__(self, size, core, cache, resolution_factor): self.core = core self.cache = cache self.fonts = {} self.background = None self.currentScreen = mainScreenIndex # Init variables in init self.baseSize = None self.size = None self.screens = None self.track = None self.input_manager = InputManager(size) self.down_bar_objects = ScreenObjectsManager() self.down_bar = None self.keyboard = None self.updateType = BaseScreen.update_all self.resolution_factor = resolution_factor self.init_manager(size) def init_manager(self, size): self.size = size self.baseSize = self.size[1] / self.resolution_factor self.background = DynamicBackground(self.size) font = resource_filename(Requirement.parse("mopidy-nowplayingtouch"), "mopidy_nowplayingtouch/FontAwesome.otf") self.fonts['base'] = pygame.font.SysFont("arial", int(self.baseSize*0.9)) self.fonts['icon'] = pygame.font.Font(font, int(self.baseSize*0.9)) try: self.screens = [ MainScreen(size, self.baseSize, self, self.fonts, self.cache, self.core, self.background), QueueScreen(size, self.baseSize, self, self.fonts), PlaylistScreen(size, self.baseSize, self, self.fonts), BrowseScreen(size, self.baseSize, self, self.fonts), SearchScreen(size, self.baseSize, self, self.fonts), MenuScreen(size, self.baseSize, self, self.fonts, self.core)] except: traceback.print_exc() self.track = None # Menu buttons button_size = (self.size[0] / 6, self.baseSize) # Main button button = TouchAndTextItem(self.fonts['icon'], u" \ue001", (0, self.size[1] - self.baseSize), button_size, center=True) self.down_bar_objects.set_touch_object("menu_0", button) x = button.get_right_pos() # Search button button = TouchAndTextItem(self.fonts['icon'], u" \ue002", (x, self.size[1] - self.baseSize), button_size, center=True) self.down_bar_objects.set_touch_object("menu_1", button) x = button.get_right_pos() # Menu button button = TouchAndTextItem(self.fonts['icon'], u" \ue60a", (x, self.size[1] - self.baseSize), button_size, center=True) self.down_bar_objects.set_touch_object("menu_5", button) # Down bar self.down_bar = pygame.Surface( (self.size[0], self.size[1] - self.baseSize), pygame.SRCALPHA) self.down_bar.fill((0, 0, 0, 200)) self.options_changed() self.mute_changed(self.core.playback.mute.get()) playback_state = self.core.playback.state.get() self.playback_state_changed(playback_state, playback_state) self.screens[MenuIndex].check_connection() self.change_screen(self.currentScreen) self.updateType = BaseScreen.update_all def get_update_type(self): if self.updateType == BaseScreen.update_all: self.updateType = BaseScreen.no_update return BaseScreen.update_all else: if self.keyboard: return BaseScreen.no_update else: if self.background.should_update(): return BaseScreen.update_all else: if self.screens[self.currentScreen].should_update(): return BaseScreen.update_partial else: return BaseScreen.no_update def update(self, screen): update_type = self.get_update_type() if update_type != BaseScreen.no_update: rects = [] surface = self.background.draw_background() if self.keyboard: self.keyboard.update(surface) else: self.screens[self.currentScreen].update(surface, update_type, rects) surface.blit(self.down_bar, (0, self.size[1] - self.baseSize)) self.down_bar_objects.render(surface) if update_type == BaseScreen.update_all or len(rects) < 1: screen.blit(surface, (0, 0)) pygame.display.flip() else: for rect in rects: screen.blit(surface, rect, area=rect) pygame.display.update(rects) def track_started(self, track): self.track = track self.screens[mainScreenIndex].track_started(track.track) self.screens[nowPlayingIndex].track_started(track) def track_playback_ended(self, tl_track, time_position): self.screens[mainScreenIndex].track_playback_ended(tl_track, time_position) def event(self, event): event = self.input_manager.event(event) if event is not None: if self.keyboard is not None: self.keyboard.touch_event(event) elif not self.manage_event(event): self.screens[self.currentScreen].touch_event(event) self.updateType = BaseScreen.update_all def manage_event(self, event): if event.type == InputManager.click: objects = self.down_bar_objects.get_touch_objects_in_pos(event.current_pos) return self.click_on_objects(objects, event) else: if event.type == InputManager.key and not event.longpress: dir = event.direction if dir == InputManager.right or dir == InputManager.left: if not self.screens[self.currentScreen]\ .change_screen(dir): if dir == InputManager.right: self.change_screen(self.currentScreen+1) else: self.change_screen(self.currentScreen-1) return True elif event.unicode is not None: if event.unicode == "n": self.core.playback.next() elif event.unicode == "p": self.core.playback.previous() elif event.unicode == "+": volume = self.core.playback.volume.get() + 10 if volume > 100: volume = 100 self.core.mixer.set_volume(volume) elif event.unicode == "-": volume = self.core.playback.volume.get() - 10 if volume < 0: volume = 0 self.core.mixer.set_volume(volume) elif event.unicode == " ": if self.core.playback.get_state().get() == \ mopidy.core.PlaybackState.PLAYING: self.core.playback.pause() else: self.core.playback.play() return False def volume_changed(self, volume): self.screens[mainScreenIndex].volume_changed(volume) self.updateType = BaseScreen.update_all def playback_state_changed(self, old_state, new_state): self.screens[mainScreenIndex].playback_state_changed(old_state, new_state) self.updateType = BaseScreen.update_all def mute_changed(self, mute): self.screens[mainScreenIndex].mute_changed(mute) self.updateType = BaseScreen.update_all def resize(self, event): self.init_manager(event.size) self.updateType = BaseScreen.update_all def tracklist_changed(self): self.screens[nowPlayingIndex].tracklist_changed() self.updateType = BaseScreen.update_all def options_changed(self): menuScreen = self.screens[MenuIndex] #self.screens[MenuIndex].options_changed() menuScreen.options_changed() self.updateType = BaseScreen.update_all def change_screen(self, new_screen): if new_screen > -1 and new_screen < len(self.screens): self.down_bar_objects.get_touch_object( "menu_" + str(self.currentScreen)).set_active(False) self.currentScreen = new_screen self.down_bar_objects.get_touch_object( "menu_" + str(new_screen)).set_active(True) self.updateType = BaseScreen.update_all def click_on_objects(self, objects, event): if objects is not None: for key in objects: if key[:-1] == "menu_": self.change_screen(int(key[-1:])) return True return False def nowPlaying(self): self.screens[nowPlayingIndex].nowPlaying_changed() self.updateType = BaseScreen.update_all def queue(self): self.screens[queueIndex].queue_changed() self.updateType = BaseScreen.update_all def playlists(self): self.screens[playlistsIndex].playlists_changed() self.updateType = BaseScreen.update_all def browse(self): self.screens[browseIndex].browse_changed() self.updateType = BaseScreen.update_all def stream(self): self.screens[streamsIndex].streams_changed() self.updateType = BaseScreen.update_all def stream_title_changed(self, title): self.screens[mainScreenIndex].stream_title_changed(title) self.updateType = BaseScreen.update_all def search(self, query, mode): self.screens[searchIndex].search(query, mode) self.updateType = BaseScreen.update_all def system(self): self.screens[SystemIndex].system_changed() self.updateType = BaseScreen.update_all def open_keyboard(self, input_listener): self.keyboard = Keyboard(self.size, self.baseSize, self, self.fonts, input_listener) self.updateType = BaseScreen.update_all def close_keyboard(self): self.keyboard = None self.updateType = BaseScreen.update_all
class Game(object): def __init__(self, screen_size, resource_path): self.mode = None self.screen_size = screen_size self.map_size = (128,128) self.tile_size = (32,32) self.world_size = (self.tile_size[0] * self.map_size[0], self.tile_size[1] * self.map_size[1]) self.world_rooms = (8,8) pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component('MovementComponent', MovementComponent()) self.component_manager.register_component('ExampleComponent', ExampleComponent()) self.component_manager.register_component('AnimationComponent', AnimationComponent()) self.component_manager.register_component('DrawComponent', DrawComponent()) self.component_manager.register_component('InputMovementComponent', InputMovementComponent()) self.component_manager.register_component('PlayerCollisionComponent', PlayerCollisionComponent()) self.component_manager.register_component('ZombieAIComponent', ZombieAIComponent()) self.component_manager.register_component('ZombieAIComponent', ZombieAIComponent()) self.component_manager.register_component('CarComponent', CarComponent()) self.component_manager.register_component('DrawHitBoxComponent', DrawHitBoxComponent()) self.component_manager.register_component('AttackComponent', AttackComponent()) self.component_manager.register_component('ItemComponent', ItemComponent()) self.component_manager.register_component('InputActionComponent', InputActionComponent()) self.component_manager.register_component('DeadComponent', DeadComponent()) self.component_manager.register_component('ZombieCollisionComponent', ZombieCollisionComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('definition', LoadEntityData) self.resource_manager.register_loader('sprite', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.input_manager.init_joysticks() def run(self, mode): #pygame.display.toggle_fullscreen() self.mode = mode self.music = self.resource_manager.get('sound', 'Teamawesome_zombies_LOOP.wav') self.music.play(loops=-1) self.entity_manager.add_entity(Entity('car')) for e in [Entity('character1'), Entity('character2'), Entity('character3'), Entity('character4')]: self.entity_manager.add_entity(e) self.item_names = ["engine", "gas-can", "radiator", "steering-wheel-2", "tire", "steering-wheel", "toolbox", "tire", "tire", "tire"] for i in self.item_names: x_pos = (self.world_size[0]/self.world_rooms[0] * randint(0, self.world_rooms[0]-1)) + self.world_size[0]/self.world_rooms[0]/2 y_pos = (self.world_size[1]/self.world_rooms[1] * randint(0, self.world_rooms[1]-1)) + self.world_size[1]/self.world_rooms[1]/2 self.entity_manager.add_entity(Entity(i, x=x_pos, y=y_pos)) self.entity_manager.add_entity(Entity('splashscreen')) self.renderer = Render(self) self.zombies = [] for _ in range(50): x_pos = (self.world_size[0]/self.world_rooms[0] * randint(0, self.world_rooms[0]-1)) + self.world_size[0]/self.world_rooms[0]/2 y_pos = (self.world_size[1]/self.world_rooms[1] * randint(0, self.world_rooms[1]-1)) + self.world_size[1]/self.world_rooms[1]/2 self.entity_manager.add_entity(Entity("zombie", x=x_pos, y=y_pos)) while True: dt = self.clock.tick(60) / 1000.0 events = self.input_manager.process_events() for event in events: if event.target == 'GAME': if event.action == 'QUIT' and event.value > 0: sys.exit() elif event.action == 'FULLSCREEN' and event.value > 0: pygame.display.toggle_fullscreen() elif event.action == 'RELOAD' and event.value > 0: self.resource_manager.clear() else: self.mode.handle_event(event) self.mode.update(dt) self.mode.draw() pygame.display.flip() pygame.display.set_caption('fps: %.0d' % self.clock.get_fps())
class Game(object): def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.display.set_caption('After You!') self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode( self.screen_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component(MovementComponent()) self.component_manager.register_component(ExampleComponent()) self.component_manager.register_component(AnimationComponent()) self.component_manager.register_component(DrawComponent()) self.component_manager.register_component(InputMovementComponent()) self.component_manager.register_component(DrawHitBoxComponent()) self.component_manager.register_component(DrawCircleComponent()) self.component_manager.register_component(SmokeScreenComponent()) self.component_manager.register_component(PlayerCollisionComponent()) self.component_manager.register_component(DecoyMovementComponent()) self.component_manager.register_component(SelfDestructComponent()) self.component_manager.register_component(SpawnDecoyComponent()) self.component_manager.register_component(DrawScoreComponent()) self.component_manager.register_component(MinefieldComponent()) self.component_manager.register_component(DrawTimerComponent()) self.component_manager.register_component(UpdateTimerComponent()) self.component_manager.register_component(SpeedBoostComponent()) self.component_manager.register_component(ButtonInterpreterComponent()) self.component_manager.register_component(DrawActionsComponent()) self.component_manager.register_component(SpawnVortexComponent()) self.component_manager.register_component(DrawVortextComponent()) self.component_manager.register_component(GrowVortextComponent()) self.component_manager.register_component(TrapComponent()) self.component_manager.register_component(HideComponent()) self.component_manager.register_component(AIActionComponent()) self.component_manager.register_component(AIMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('data', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.renderer = GLRenderer() self.renderer.resize(self.screen_size) self.view = View([SimpleLayer('draw'), SimpleLayer('ui')]) def run(self, mode): p1 = Entity("player1") p2 = Entity("player2") self.entity_manager.add_entity(p1) self.entity_manager.add_entity(p2) self.entity_manager.add_entity(Entity("scoreui-player1")) self.entity_manager.add_entity(Entity("scoreui-player2")) self.entity_manager.add_entity(Entity("actionui-player1")) self.entity_manager.add_entity(Entity("actionui-player2")) self.entity_manager.add_entity(Entity("timerui")) self.renderer.createBackground() self.change_mode(mode) self.running = True self.entity_manager.commit_changes() while self.running: dt = self.clock.tick(60) / 1000.0 events = self.input_manager.process_events() for event in events: if event.target == 'GAME': if event.action == 'QUIT' and event.value > 0: self.running = False elif event.action == 'FULLSCREEN' and event.value > 0: pygame.display.toggle_fullscreen() elif event.action == 'RELOAD' and event.value > 0: self.resource_manager.clear() else: self.mode.handle_event(event) self.mode.update(dt) self.mode.draw() self.entity_manager.commit_changes() pygame.display.flip() def change_mode(self, new_mode): if self.mode: self.mode.leave() self.mode = new_mode self.mode.enter()
def __init__(self, game, display): self.game = game game.rootMenu = self self.display = display # setup menu stage self.setup_empty_stage() self.image = pygame.Surface((config.SCREEN_W, config.SCREEN_H)).convert_alpha() self.rect = (0, 0) K = input.key def record_on(ctx): config.RECORD = 1 def record_off(ctx): config.RECORD = 0 def play_human_vs_AI(ctx): game.setup_stage({ 'world-size':(16, 16), 'snakes': [ ((8, 8), Directions.RIGHT, 5), ((7, 7), Directions.RIGHT, 5), ], 'rule': rule[0], }, display) game.join_human_player("Player", [K('w'), K('s'), K('a'), K('d')]) game.join_player(AIPlayer("AI Player")) game.start() def play_human_vs_human(ctx): game.setup_stage({ 'world-size':(16, 16), 'snakes': [ ((8, 8), Directions.RIGHT, 5), ((7, 7), Directions.RIGHT, 5), ], 'rule': rule[0], }, display) game.join_human_player("Foo", [K('w'), K('s'), K('a'), K('d')]) game.join_human_player("Bar", [K('i'), K('k'), K('j'), K('l')]) game.start() play_4_AI = play_AI_vs_AI = play_human_vs_human # def play_AI_vs_AI(ctx): # pass # def play_4_AI(ctx): # pass rule = [(gamerule.DeathModeRule, ())] def mode_set(mode, args): def setter(ctx): rule[0] = (mode, args) return setter super(RootMenu, self).__init__('$>', [ Menu('play', [ Menu('1: human v.s. AI', [], callback=play_human_vs_AI), Menu('2: human v.s. human', [], callback=play_human_vs_human), Menu('3: AI v.s. AI', [], callback=play_AI_vs_AI), Menu('4: 4 AI', [], callback=play_4_AI), ]), Choices('game rule', [ Option('death mode', callback=mode_set(gamerule.DeathModeRule, ())), Option('scoring mode', callback=mode_set(gamerule.ScoringModeRule, (30,))), Option('fixed round mode', callback=mode_set(gamerule.FixedRoundModeRule, (128,))), ]), Help('help'), Choices('set record', [ Option('record on', callback=record_on), Option('record off', callback=record_off), ]), InputBox('record'), ReplayRecord('replays'), Choices('quit', [ Option('confirm', callback=lambda ctx: exit(0)), ]) ], cancel=False) self.ctx = Context(self) # bind keys inputMgr = InputManager.get_instance() def handle_key(event): if game.state == game.ST_MENU: self.ctx.handle_key(event) inputMgr.bind(input.key_down_type(), handle_key) def escape(event): if game.state != game.ST_MENU: game.quit() else: exit(0) inputMgr.bind(input.key_down_type('ESCAPE'), escape) def back_to_menu(event): self.setup_empty_stage() inputMgr.bind(input.key_down_type('HOME'), back_to_menu)
class ScreenManager(): def __init__(self, size, core, cache, resolution_factor): self.core = core self.cache = cache self.fonts = {} self.background = None self.current_screen = menu_index self.locked = False # Init variables in init self.base_size = None self.size = None self.screens = None self.track = None self.input_manager = InputManager(size) self.down_bar_objects = ScreenObjectsManager() self.down_bar = None self.keyboard = None self.update_type = BaseScreen.update_all self.resolution_factor = resolution_factor self.init_manager(size) def init_manager(self, size): self.size = size self.background = DynamicBackground(self.size) self.base_size = self.size[1] / self.resolution_factor font = resource_filename( Requirement.parse("mopidy-touchscreen"), "mopidy_touchscreen/icomoon.ttf") self.fonts['base'] = pygame.font.Font(font, int(self.base_size*0.9)) self.fonts['icon'] = pygame.font.Font(font, int(self.base_size*0.9)) self.fonts['small'] = pygame.font.Font(font, int(self.base_size*0.4)) print(pygame.font.get_fonts()) try: self.screens = [ SearchScreen(size, self.base_size, self, self.fonts), MainScreen(size, self.base_size, self, self.fonts, self.cache, self.core, self.background), Tracklist(size, self.base_size, self, self.fonts), LibraryScreen(size, self.base_size, self, self.fonts), PlaylistScreen(size, self.base_size, self, self.fonts), MenuScreen(size, self.base_size, self, self.fonts, self.core)] except: traceback.print_exc() self.track = None # Menu buttons button_size = (self.size[0] / 6, self.base_size) # Search button button = TouchAndTextItem(self.fonts['icon'], u" \ue986", (0, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_0", button) x = button.get_right_pos() # Main button button = TouchAndTextItem(self.fonts['icon'], u" \ue911", (x, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_1", button) x = button.get_right_pos() # Tracklist button button = TouchAndTextItem(self.fonts['icon'], u" \ue9bb", (x, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_2", button) x = button.get_right_pos() # Library button button = TouchAndTextItem(self.fonts['icon'], u" \ue920", (x, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_3", button) x = button.get_right_pos() # Playlist button button = TouchAndTextItem(self.fonts['icon'], u" \ue922", (x, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_4", button) x = button.get_right_pos() # Menu button button = TouchAndTextItem(self.fonts['icon'], u" \uea09", (x, self.size[1] - self.base_size+3), button_size, center=True) self.down_bar_objects.set_touch_object("menu_5", button) # Down bar self.down_bar = pygame.Surface( (self.size[0], self.base_size), pygame.SRCALPHA) self.down_bar.fill((0, 0, 0, 200)) self.options_changed() self.mute_changed(self.core.playback.mute.get()) playback_state = self.core.playback.state.get() self.playback_state_changed(playback_state, playback_state) #self.screens[menu_index].check_connection() self.change_screen(self.current_screen) self.update_type = BaseScreen.update_all def get_update_type(self): if self.update_type == BaseScreen.update_all: self.update_type = BaseScreen.no_update return BaseScreen.update_all else: if self.keyboard: return BaseScreen.no_update else: if self.background.should_update(): return BaseScreen.update_all else: if self.screens[self.current_screen].should_update(): return BaseScreen.update_partial else: return BaseScreen.no_update def update(self, screen): update_type = self.get_update_type() if update_type != BaseScreen.no_update: rects = [] surface = self.background.draw_background() if self.keyboard: self.keyboard.update(surface) else: self.screens[self.current_screen].\ update(surface, update_type, rects) surface.blit(self.down_bar, (0, self.size[1] - self.base_size)) self.down_bar_objects.render(surface) if update_type == BaseScreen.update_all or len(rects) < 1: screen.blit(surface, (0, 0)) pygame.display.flip() else: for rect in rects: screen.blit(surface, rect, area=rect) pygame.display.update(rects) def track_started(self, track): self.track = track self.screens[main_screen_index].track_started(track.track) self.screens[tracklist_index].track_started(track) def track_playback_ended(self, tl_track, time_position): self.screens[main_screen_index].track_playback_ended( tl_track, time_position) def event(self, event): event = self.input_manager.event(event) if event is not None: if self.keyboard is not None: self.keyboard.touch_event(event) elif not self.manage_event(event): self.screens[self.current_screen].touch_event(event) self.update_type = BaseScreen.update_all def manage_event(self, event): if event.type == InputManager.click: objects = \ self.down_bar_objects.get_touch_objects_in_pos( event.current_pos) return self.click_on_objects(objects, event) else: if event.type == InputManager.key and not event.longpress: dir = event.direction if dir == InputManager.right or dir == InputManager.left: if not self.screens[self.current_screen]\ .change_screen(dir): if dir == InputManager.right: self.change_screen(self.current_screen+1) else: self.change_screen(self.current_screen-1) return True return False def volume_changed(self, volume): self.screens[main_screen_index].volume_changed(volume) self.update_type = BaseScreen.update_all def playback_state_changed(self, old_state, new_state): self.screens[main_screen_index].playback_state_changed( old_state, new_state) self.update_type = BaseScreen.update_all def mute_changed(self, mute): self.screens[main_screen_index].mute_changed(mute) self.update_type = BaseScreen.update_all def tracklist_changed(self): self.screens[tracklist_index].tracklist_changed() self.update_type = BaseScreen.update_all def options_changed(self): self.screens[menu_index].options_changed() self.update_type = BaseScreen.update_all def change_screen(self, new_screen): if new_screen > -1 and new_screen < len(self.screens): self.down_bar_objects.get_touch_object( "menu_" + str(self.current_screen)).set_active(False) self.current_screen = new_screen self.down_bar_objects.get_touch_object( "menu_" + str(new_screen)).set_active(True) self.update_type = BaseScreen.update_all def click_on_objects(self, objects, event): if objects is not None: for key in objects: if key[:-1] == "menu_": if key == "menu_0": self.change_screen(int(key[-1:])) self.open_keyboard(self.screens[search_index]) return True elif key == "menu_3": self.change_screen(int(key[-1:])) self.screens[library_index].return_to_start_directory() elif key == "menu_4" and self.locked: pass else: self.change_screen(int(key[-1:])) return True return False def playlists_loaded(self): self.screens[playlist_index].playlists_loaded() self.update_type = BaseScreen.update_all def search(self, query, mode): self.screens[search_index].search(query, mode) self.update_type = BaseScreen.update_all def resize(self, event): self.init_manager(event.size) self.update_type = BaseScreen.update_all def set_locked(self): self.locked = not self.locked def open_keyboard(self, input_listener): self.keyboard = Keyboard(self.size, self.base_size, self, self.fonts, input_listener) self.update_type = BaseScreen.update_all def close_keyboard(self): self.keyboard = None self.update_type = BaseScreen.update_all
class Game(object): def __init__(self, screen_size, resource_path): global _game _game = self self.mode = None self.running = False self.screen_size = screen_size pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.display.set_caption('After You!') self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(self.screen_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.HWSURFACE) self.component_manager = componentmanager.ComponentManager() self.component_manager.register_component(MovementComponent()) self.component_manager.register_component(ExampleComponent()) self.component_manager.register_component(AnimationComponent()) self.component_manager.register_component(DrawComponent()) self.component_manager.register_component(InputMovementComponent()) self.component_manager.register_component(DrawHitBoxComponent()) self.component_manager.register_component(DrawCircleComponent()) self.component_manager.register_component(SmokeScreenComponent()) self.component_manager.register_component(PlayerCollisionComponent()) self.component_manager.register_component(DecoyMovementComponent()) self.component_manager.register_component(SelfDestructComponent()) self.component_manager.register_component(SpawnDecoyComponent()) self.component_manager.register_component(DrawScoreComponent()) self.component_manager.register_component(MinefieldComponent()) self.component_manager.register_component(DrawTimerComponent()) self.component_manager.register_component(UpdateTimerComponent()) self.component_manager.register_component(SpeedBoostComponent()) self.component_manager.register_component(ButtonInterpreterComponent()) self.component_manager.register_component(DrawActionsComponent()) self.component_manager.register_component(SpawnVortexComponent()) self.component_manager.register_component(DrawVortextComponent()) self.component_manager.register_component(GrowVortextComponent()) self.component_manager.register_component(TrapComponent()) self.component_manager.register_component(HideComponent()) self.component_manager.register_component(AIActionComponent()) self.component_manager.register_component(AIMovementComponent()) self.entity_manager = EntityManager() self.resource_manager = ResourceManager(resource_path) self.resource_manager.register_loader('data', LoadEntityData) self.resource_manager.register_loader('image', LoadImage) self.resource_manager.register_loader('inputmap', LoadInputMapping) self.resource_manager.register_loader('sound', LoadSound) self.input_manager = InputManager() self.renderer = GLRenderer() self.renderer.resize(self.screen_size) self.view = View([SimpleLayer('draw'), SimpleLayer('ui')]) def run(self, mode): p1 = Entity("player1") p2 = Entity("player2") self.entity_manager.add_entity(p1) self.entity_manager.add_entity(p2) self.entity_manager.add_entity(Entity("scoreui-player1")) self.entity_manager.add_entity(Entity("scoreui-player2")) self.entity_manager.add_entity(Entity("actionui-player1")) self.entity_manager.add_entity(Entity("actionui-player2")) self.entity_manager.add_entity(Entity("timerui")) self.renderer.createBackground() self.change_mode(mode) self.running = True self.entity_manager.commit_changes() while self.running: dt = self.clock.tick(60) / 1000.0 events = self.input_manager.process_events() for event in events: if event.target == 'GAME': if event.action == 'QUIT' and event.value > 0: self.running = False elif event.action == 'FULLSCREEN' and event.value > 0: pygame.display.toggle_fullscreen() elif event.action == 'RELOAD' and event.value > 0: self.resource_manager.clear() else: self.mode.handle_event(event) self.mode.update(dt) self.mode.draw() self.entity_manager.commit_changes() pygame.display.flip() def change_mode(self, new_mode): if self.mode: self.mode.leave() self.mode = new_mode self.mode.enter()