def process_input(self, events, pressed_keys): for event in events: if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.back_button.collidepoint(mouse_pos): from menu_scene import MenuScene self.switch_to_scene(MenuScene())
def init(): """ Scenes need pygame to be initialized (in main) before their creation. """ global scene_manager scenes = { SCN_MENU: MenuScene(), SCN_GAME: GameScene(), SCN_OVER: GameOverScene() } scene_manager = _SceneManager(scenes, SCN_MENU)
def __init__(self): self.director = Director((512, 480), 'Super Mario') self.menu_scene = MenuScene(self.director) self.game_scene = GameScene(self.director) self.scores_scene = ScoresScene(self.director) self.director.scene_list = { 'menu': self.menu_scene, 'game': self.game_scene, 'scores': self.scores_scene } self.director.set_scene('menu')
def __init__(self): self.director = Director((1200, 800), "Space Invaders") self.menu_scene = MenuScene(self.director) self.game_scene = GameScene(self.director) self.scores_scene = ScoresScene(self.director) self.director.scene_list = { "menu": self.menu_scene, "game": self.game_scene, "scores": self.scores_scene } self.director.set_scene("menu")
def process_input(self, events, pressed_keys): for event in events: if not self.game_paused and event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.menu_button.collidepoint(mouse_pos): self.save() self.game_paused = False from menu_scene import MenuScene self.switch_to_scene(MenuScene()) if self.pause_button.collidepoint(mouse_pos): self.game_paused = True if self.game_paused and event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.menu_pause_button.collidepoint(mouse_pos): self.save() self.game_paused = False from menu_scene import MenuScene self.switch_to_scene(MenuScene()) if self.continue_button.collidepoint(mouse_pos): self.game_paused = False if not self.game_paused and event.type == pygame.KEYDOWN: if event.key == pygame.K_w: self.space_ship.speed += 0 elif event.key == pygame.K_s: self.space_ship.speed -= 0 elif event.key == pygame.K_a: self.space_ship.angle_speed = -4 elif event.key == pygame.K_d: self.space_ship.angle_speed = 4 elif not self.game_paused and event.type == pygame.KEYUP: if event.key == pygame.K_a: self.space_ship.angle_speed = 0 elif event.key == pygame.K_d: self.space_ship.angle_speed = 0
def __init__(self, screen, width, height, debug=False, start_level=1): self.target_fps = 60 self.clock = pygame.time.Clock() self.running = False self.test_text = "Hello world" self.font = pygame.font.Font('./assets/font/vcr.ttf', 28) self.screen = screen self.width = width self.height = height self.current_scene = None self.restarting = False self.input = Input(pygame.key, pygame.mouse) self.current_scene = MenuScene(self, start_level) self.next_scene = None self.debug = debug
def process_input(self, events, pressed_keys): for event in events: if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.play_button.collidepoint(mouse_pos): if self.name_input_form.get_text() != '': self.save() self.switch_to_scene(GameScene()) self.blank_from = False else: self.hint_text = self.FONT.render( 'Enter name!', False, pygame.Color(RED)) if self.back_button.collidepoint(mouse_pos): from menu_scene import MenuScene self.switch_to_scene(MenuScene()) self.name_input_form.handle_event(event)
def main(): # configure the logger before anything else happens logging.config.fileConfig('logging.conf') # The game logger is configured when the config module is imported. log = logging.getLogger('game') log.info('Main started') # load levels levels = load_level_set(LEVELS_FILENAME, LEVELS_MAXSIZE) # list pg.init() pg.display.set_caption('Sokobalt') pview.set_mode(BASE_RES) clock = pg.time.Clock() scenes = {SCN_MENU: MenuScene(levels), SCN_GAME: GameScene(levels)} cur_scene = scenes[SCN_MENU] while True: ms = clock.tick(FPS) # throttle # poll controls outcome = controller.poll() # get player input if outcome == OUT_QUIT: break elif outcome == OUT_FSCR: pview.toggle_fullscreen() cur_scene.redraw() # tick scene next_scene_id, kwargs = cur_scene.tick(ms) if next_scene_id == SCN_QUIT: # quit via dummy scene constant break elif next_scene_id is not None: # change scene cur_scene.pause() cur_scene = scenes[next_scene_id] cur_scene.resume(**kwargs)
from ezpygame import Application from menu_scene import MenuScene mod_pack = { "mods": ["minecraft"], "generator": "minecraft:SimpleGenerator", "controller": ["minecraft:ExploreGravityControl"], "player": ("minecraft:Player", (-5, 110)), "gravity": (0, -20) } app = Application("", (1, 1), 0) app.run(MenuScene(mod_pack))
class Game(object): def __init__(self, screen, width, height, debug=False, start_level=1): self.target_fps = 60 self.clock = pygame.time.Clock() self.running = False self.test_text = "Hello world" self.font = pygame.font.Font('./assets/font/vcr.ttf', 28) self.screen = screen self.width = width self.height = height self.current_scene = None self.restarting = False self.input = Input(pygame.key, pygame.mouse) self.current_scene = MenuScene(self, start_level) self.next_scene = None self.debug = debug # self.background = Background((800, 600), 'assets/img/bgs.png') def run(self): self.running = True current_time = 0 previous_time = time.time() while self.running: self.clock.tick( self.target_fps ) self.input.update() if not self.next_scene is None: self.current_scene = copy.copy(self.next_scene) self.next_scene = None for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.restarting = False self.input.handle_event(event) if game.input.is_down(pygame.K_ESCAPE): self.running = False self.restarting = False current_time = time.time() delta_time = (current_time - previous_time) self.update(delta_time) self.draw() if self.debug: fps = round(1.0 / delta_time) fps_text = 'FPS: {0}'.format(fps) fps_w, fps_h = self.font.size(fps_text) fps_text_pos = (self.width - fps_w - 40, 20) self.screen.blit(self.font.render(fps_text, 1, WHITE), fps_text_pos) pygame.display.update() previous_time = current_time if self.restarting: game.run() else: game.quit() def restart(self): self.running = False self.restarting = True def quit(self): pygame.quit() quit() def update(self, dt): self.current_scene.update(dt) def draw(self): self.current_scene.draw()
import pygame from config import * from menu_scene import MenuScene pygame.font.init() pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption(GAME_TITLE) clock = pygame.time.Clock() done = False active_scene = MenuScene() while not done: clock.tick(FPS) pressed_keys = pygame.key.get_pressed() filtered_events = [] for event in pygame.event.get(): if event.type == pygame.QUIT: done = True else: filtered_events.append(event) active_scene.process_input(filtered_events, pressed_keys) active_scene.update() active_scene.render(screen) active_scene = active_scene.next pygame.display.update()
import pygame from director import Director from game_scene import GameScene from menu_scene import MenuScene pygame.init() game_director = Director((800, 450), "Simple Ping Pong") game_scene = GameScene(game_director, True) menu_scene = MenuScene(game_director, game_scene) game_scene.set_menu_scene(menu_scene) game_director.change_scene(menu_scene) game_director.loop()
def receive(self, message, emitter=None, data=None): if message == TitleScene.Events.DONE: current_scene = MenuScene() current_scene.render() current_scene.loop() elif message == MenuScene.Events.CREATE_CHARACTER: current_scene = CreateFighterScene() current_scene.render() current_scene.loop() elif message == CreateFighterScene.Events.CREATE: Director.characters.append(data) elif message == CreateFighterScene.Events.DONE: current_scene = MenuScene() current_scene.render() current_scene.loop() elif message == MenuScene.Events.LIST_CHARACTER: self.characters = Fighter.all() current_scene = ShowCharactersScene(self.characters) current_scene.render() current_scene.show_fighters() elif message == MenuScene.Events.SELECT_CHARACTER: # TODO: print('PENDING: make select character scene') elif message == MenuScene.Events.REMOVE_CHARACTER: # TODO: print('PENDING: make remove character scene')