class GameController: # 設定初值 def __init__(self): self.app = GameView(master=Tk()) self.app.button["command"] = self.action self.app.mainloop() # 按下按鈕的事件 def action(self): self.app.result["text"] = "按鈕被按"
def main(): model = GameModel() view = GameView(800, 600) controller = GameController(model, view) # --- while (True): controller.update()
def __init__(self): self._game_process = GameProcess(rang=self._RANG, ) self._game_view = GameView( rang=self._RANG, title=self._TITLE, window_size=self._WINDOW_SIZE, game_process=self._game_process, )
def createView(self, name, text=''): if name == 'Menu': return MenuView(self.userSocket, self) elif name == 'GameView': return GameView(self.userSocket, self) elif name == 'Result': return ResultView(self.userSocket, self, text) elif name == 'Error': return ErrorView(self.userSocket, self)
def __init__(self): self.__model = GameModel() self.__sprites = pg.sprite.Group() self.__attacks = pg.sprite.Group() self.__view = GameView(self.__model.player, self.__model.level, self.__sprites, self.__attacks) self.__player = self.__model.player self.__level = self.__model.level self.__clock = pg.time.Clock() # inicializa os modulos do pygame e retorna uma tupla com resultados self.__modules = pg.init() self.__running = True self.__menu = True self.__game_over = False self.__start_playing = False self.__win = False
def start(): game = Game(random.randint(1, 100) * 100) pg.init() clock = pg.time.Clock() running = True view = GameView(game, 40) while running: clock.tick(60) for e in pg.event.get(): if e.type == pg.QUIT: running = False elif e.type == pg.KEYDOWN: if e.key == pg.K_ESCAPE: running = False view.update(game) pg.display.flip() pg.quit()
def play(config: Config): pg.init() game = Game(config.width, config.height, config.seed, config.generations, config.population, config.selection, config.crossover, config.mutation) view = GameView(game, config.tile) pause = False clock = pg.time.Clock() # type: Clock while game.is_running: pause = handle_user_input(game, pause) if not pause: if game.current_id < config.top: clock.tick(config.fps) game.run(save=True, save_ann=True) if game.is_running: view.update(game) pg.display.flip() else: game.run(save=True) pg.quit()
def main(): pygame.init() event_manager = EventManager() game_model = GameModel(event_manager) keyboard_controller = KeyboardController(event_manager) time_controller = TimeController(event_manager) game_view = GameView( event_manager, game_model, time_controller) # sollte von GameModel aufgemacht werden time_controller.run()
def on_update(self, delta_time: float): # Dictionary to hold all our maps if self.started: done, self.progress, self.map_list = load_maps() if done: self.window.views['game'] = GameView(self.map_list) self.window.views['game'].setup() self.window.views['inventory'] = InventoryView() self.window.views['inventory'].setup() self.window.views['main_menu'] = MainMenuView() self.window.views['main_menu'].setup() self.window.show_view(self.window.views['game'])
def replay(config): pg.init() game = Game(config.width, config.height, config.seed, config.generations, config.top, config.selection, config.crossover, config.mutation, create=False) view = GameView(game, config.tile) pause = False clock = pg.time.Clock() # type: Clock while game.is_running: clock.tick(config.fps) pause = handle_user_input(game, pause) if not pause: game.run() if game.is_running: view.update(game) pg.display.flip() pg.quit()
def main(): """ Main method """ gd = gameData() window = arcade.Window(const.SCREEN_WIDTH, const.SCREEN_HEIGHT, const.SCREEN_TITLE) gd.start_view = StartView() gd.serve_view = ServeView() gd.game_view = GameView() gd.game_over_view = GameOver() # install a reference to the game data in the starting view gd.start_view.setup(gd) window.show_view(gd.start_view) arcade.run()
def main(): pygame.init() pygame.display.set_caption("The 15 Puzzle") win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) clock = pygame.time.Clock() event_manager = EventManager() game_board = GameModel() game_view = GameView(win) spinner = CPUSpinnerController(event_manager, clock) game_controller = GameController(game_board, game_view) event_manager.registerListener(TickEvent(), game_controller) event_manager.registerListener(QuitEvent(), spinner) spinner.run() pygame.quit()
class GameController: def __init__(self): pygame.init() self.__GameModel = GameModel(self) self.__GameView = GameView(self, self.__GameModel) self.__sounds = Sound() def start_screen(self): self.__sounds.princess_hit_sound() self.__sounds.background_sound() waiting = True while waiting: self.__GameView.draw_start_screen() self.start_events() def game_over_screen(self): self.__sounds.orc_sound() waiting = True new_highscore = False if self.__GameModel.score > self.__GameModel.highscore.data: self.__GameModel.highscore.new_hs(self.__GameModel.score) new_highscore = True self.__GameView.draw_game_over(new_highscore) #time.sleep para fazer com que a tela não desapareça mto rápido #um draw antes pra paralisar na tela correta time.sleep(1.1) while waiting: self.__GameView.draw_game_over(new_highscore) for sprite in self.__GameModel.all_sprites: sprite.kill() self.start_events() def win_screen(self): waiting = True new_highscore = False if self.__GameModel.score > self.__GameModel.highscore.data: self.__GameModel.highscore.new_hs(self.__GameModel.score) new_highscore = True self.__GameView.draw_win_screen(new_highscore) #time.sleep para fazer com que a tela não desapareça mto rápido #um draw antes pra paralisar na tela correta time.sleep(1.1) while waiting: self.__GameView.draw_win_screen(new_highscore) for sprite in self.__GameModel.all_sprites: sprite.kill() self.start_events() def start_game(self): self.__GameModel.score = 0 self.__GameModel.load_map() playing = True while playing: self.game_events() self.__GameModel.update_positions() self.__GameView.draw_game() enemy_hits = pygame.sprite.spritecollide( self.__GameModel.princess, self.__GameModel.enemies, False, pygame.sprite.collide_mask) door_found = pygame.sprite.collide_rect(self.__GameModel.princess, self.__GameModel.door_tile) if enemy_hits or door_found: if enemy_hits: self.game_over_screen() if door_found: self.win_screen() def game_events(self): # Eventos do jogo for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() def start_events(self): #self.__sounds.background_sound() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYUP: self.start_game()
class GameController: def __init__(self): self.__model = GameModel() self.__sprites = pg.sprite.Group() self.__attacks = pg.sprite.Group() self.__view = GameView(self.__model.player, self.__model.level, self.__sprites, self.__attacks) self.__player = self.__model.player self.__level = self.__model.level self.__clock = pg.time.Clock() # inicializa os modulos do pygame e retorna uma tupla com resultados self.__modules = pg.init() self.__running = True self.__menu = True self.__game_over = False self.__start_playing = False self.__win = False def run(self): self.__modules self.load_level() self.__view.music("The_Mandalorian_OST_Main_Theme.wav", -1) # view while self.__running: self.__clock.tick(self.__model.FPS) # logica de game over while self.__game_over: self.events() self.warning("GAME OVER") pg.display.flip() sleep(3) self.__game_over = False self.__model.data(True, False) self.__menu = True while self.__win: self.events() self.warning("YOU WIN") pg.display.flip() sleep(3) self.__win = False self.__menu = True while self.__menu: self.events() # Vou passar para dentro de update * self.menu() pg.display.flip() # sincroniza o loop de eventos com o clock self.events() self.update() self.__view.draw() # funcao de saida do pygame chamada em caso de fechamento de janela def quit(self): self.__running = False pg.quit() def events(self): for event in pg.event.get(): # se fecha a janela termina o programa if event.type == pg.QUIT: self.__model.data(True, True) self.quit() self.commands(event) # logica de comportamento dos inimigos if self.__start_playing == True: for enemy in self.__level.enemies: enemy.follow_rect(self.__player) self.enemy_attack(enemy) def update(self): self.physics() self.kill_the_dead() self.__view.update_scene() def load_level(self): # posicao do jogador, deve ser carregada de level self.__player.pos = self.__level.spawn_point def button(self, msg, x, y, w, h, inactive, active, action=None): mouse = pg.mouse.get_pos() click = pg.mouse.get_pressed() if (x+w) > mouse[0] > x and (y+h) > mouse[1] > (h): pg.draw.rect(self.__view.screen, active, (x, y, w, h)) if click[0] == 1 and action != None: if action == "start": self.__menu = False if action == "quit": self.__model.data(True, True) self.__quit() if action == "load": self.__model.data(False, False) self.__menu = False if action == "menu": self.__menu = True else: pg.draw.rect(self.__view.screen, inactive, (x, y, w, h)) self.__button, self.__button_rect = self.__view.message( BLACK, msg, None, 20, (x+(w/2)), (y+(h/2))) self.__view.screen.blit(self.__button, self.__button_rect) def menu(self): self.__message, self.__message_rect = self.__view.message( BLUE, "Baby Yoda's Rescue", None, 100, (WIDTH/2), (HEIGHT/2)) self.__bg = pg.image.load(data + "background-1.png") self.__view.screen.blit(self.__bg, self.__bg.get_rect()) self.__view.screen.blit(self.__message, self.__message_rect) self.button("START", (HEIGHT/4)+30, (WIDTH/2), 100, 50, AZUL_BONITO, AZUL_BONITO_CLARO, "start") self.button("LOAD", (HEIGHT/4)+200, (WIDTH/2), 100, 50, AZUL_BONITO, AZUL_BONITO_CLARO, "load") self.button("QUIT", (HEIGHT/4)+(WIDTH/2)-30, (WIDTH/2), 100, 50, RED, LIGHT_RED, "quit") def pause(self): pass def warning(self, warning): # "GAME OVER" ou "YOU WIN" self.__message, self.__message_rect = self.__view.message( RED, warning, None, 100, (WIDTH/2), (HEIGHT/2)) self.__view.screen.blit(self.__bg, self.__bg.get_rect()) self.__view.screen.blit(self.__message, self.__message_rect) def physics(self): self.collisions() self.lazer_movement() self.attack_collision() self.__player.char_physics() for enemy in self.__level.enemies: enemy.char_physics() def lazer_movement(self): for lazer in self.__attacks.sprites(): lazer.pos.x += math.cos(lazer.angle) * lazer.vel lazer.pos.y += math.sin(lazer.angle) * lazer.vel lazer.rect.center = lazer.pos # criar funcao pra destruir lazers out_of_border = (lazer.rect.right >= WIDTH or lazer.rect.left <= 0 or lazer.rect.bottom >= HEIGHT or lazer.rect.top <= 0) if out_of_border: lazer.kill() def kill_the_dead(self): for sprite in self.__level.enemies: if sprite.health <= 0: sprite.kill() if self.__player.health <= 0.0: self.__game_over = True self.__player.kill() def collisions(self): # Causa a colisão def collisions_rect(rect): collision_tolerance = 10 hits_platforms = pg.sprite.spritecollide( rect, self.__level.platforms, False, False) for platform in hits_platforms: if abs(rect.rect.bottom - platform.rect.top) < collision_tolerance: rect.collisions["bottom"] = platform.rect.top else: rect.collisions["bottom"] = False if abs(rect.rect.top - platform.rect.bottom) < collision_tolerance: rect.collisions["top"] = platform.rect.bottom if (abs(rect.rect.left - platform.rect.right) < collision_tolerance): rect.collisions["left"] = platform.rect.right else: rect.collisions["left"] = False if (abs(rect.rect.right - platform.rect.left) < collision_tolerance): rect.collisions["right"] = platform.rect.left else: rect.collisions["right"] = False if not hits_platforms: rect.collisions["bottom"] = False rect.collisions["top"] = False rect.collisions["right"] = False rect.collisions["left"] = False collisions_rect(self.__player) for enemy in self.__level.enemies: collisions_rect(enemy) # Colisao com itens: hits_items = pg.sprite.spritecollide( self.__player, self.__level.items, True) if hits_items: self.__player.key = True # Colisao com a saida: hits_exit = pg.sprite.spritecollide( self.__player, self.__level.exit, False) if hits_exit and self.__player.key == True: self.__win = True # define a colisao de ataques def attack_collision(self): hits = pg.sprite.groupcollide(self.__attacks, self.__level.enemies, False, False) # destroi lazers que batem na plataforma pg.sprite.groupcollide( self.__attacks, self.__level.platforms, True, False) hits_player = pg.sprite.spritecollide( self.__player, self.__attacks, False) # adiciona eventuais colisoes em player for item in hits_player: hits.update({item: [self.__player]}) # itera sobre dict for attack, sprite in hits.items(): # se o ataque nao e do atacante if attack.shooter != sprite[0]: # diminui vida do sprite atingido sprite[0].health -= attack.damage * (random.randint(1, 10)/10) attack.kill() def enemy_attack(self, enemy): random_attack = random.randint(0, 15) trooper_imprecision = random.randint(-100, 100) if random_attack < 2: lazer = self.__model.gen_lazer( enemy, self.__player.pos + (trooper_imprecision, trooper_imprecision)) self.__attacks.add(lazer) self.__view.update_attacks() def commands(self, event): # logica de comandos keys = pg.key.get_pressed() # Se player se movimentar ou atirar: está jogando if self.__player.acc.x != 0 or self.__player.vel.y == self.__player.jump_acc: self.start_playing = True # seta esquerda if keys[pg.K_a]: # and not self.__player.collisions["left"]: self.__player.animation("left") self.__player.acc.x = -1 * self.__player.std_acc # seta direita if keys[pg.K_d]: # and not self.__player.collisions["right"]: self.__player.animation("right") self.__player.acc.x = self.__player.std_acc if not keys[pg.K_d] and not keys[pg.K_a]: self.__player.acc.x = 0 # logica de salto if (keys[pg.K_SPACE] or keys[pg.K_w]) and self.__player.air_timer < 10: self.__player.vel.y = self.__player.jump_acc # clique de mouse mais posicao if event.type == pg.MOUSEBUTTONDOWN: lazer = self.__model.gen_lazer(self.__player, pg.mouse.get_pos()) self.__attacks.add(lazer) self.__view.update_attacks() @ property def running(self): return self.__running @ running.setter def running(self, new_value): self.__running = new_value @ property def sprites(self): return self.__sprites @ property def start_playing(self): return self.__start_playing @ start_playing.setter def start_playing(self, new_value): self.__start_playing = new_value
__author__ = 'sean' import pygame from game import Game from game_controller import GameController from game_view import GameView from constants import Constants clock = pygame.time.Clock() game_controller = GameController() game_view = GameView() game = Game() while 1: deltat = clock.tick(Constants.FRAMES_PER_SECOND) game_controller.update(game) game_view.update(game)
def __init__(self): pygame.init() self.__GameModel = GameModel(self) self.__GameView = GameView(self, self.__GameModel) self.__sounds = Sound()
def main(): # MVC pattern (Model View Controller) model = GameModel() controller = GameController(model) view = GameView(model, controller)
def __init__(self): self.app = GameView(master=Tk()) self.app.button["command"] = self.action self.app.mainloop()
# Set windows caption pygame.display.set_caption("Conquer " + conquer_version) # Resources are greatly saved with this pygame.event.set_blocked(pygame.MOUSEMOTION) # Fill the screen with black color screen.fill((0, 0, 0)) # Load images into image container, IH. (but not the interface images yet) gamemenu.load_image_files_but_not_interface_image_files(IH, graphics_path) # Create the Game View # Parameters: pygame screen, image container, game path and game board gv = GameView(screen, IH, path[0]) # Load the interface images... at the moment they need # to be loaded after the Game Board has an instance IH.add_image(pygame.image.load(graphics_path + gv.sc.get("interface_filename", "leiska.png")).convert(), "interface") IH.add_image(pygame.image.load(graphics_path + gv.sc.get("menu_interface_filename", "menu.png")).convert(), "menu_interface") # We have nothing to lose if we try to use psyco. try: import psyco except ImportError: pass # If Psyco is not installed it is not a problem