def main(args): try: board = GameBoard(rows=args.rows, cols=args.cols, mines=args.mines) except RuntimeError as error: print(error) sys.exit(1) board.generate_board() game_renderer = GameRenderer(board) game_renderer.play_game() print('\n\nThanks for playing :)')
def startgame(): data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(1000) # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. while not game.game_data.game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() if event.type == pygame.MOUSEMOTION: bus.emit("mouse:hover", game.renderer, MouseHoverEvent(event.pos[0])) pygame.display.update() if event.type == pygame.MOUSEBUTTONDOWN: bus.emit("mouse:click", game, MouseClickEvent(event.pos[0])) if event.type == KEYDOWN: if event.key == pygame.K_z: mods: int = pygame.key.get_mods() if mods & pygame.KMOD_CTRL: bus.emit("game:undo", game) game.update() game.draw()
def start_game(board_path, player, markers_save_path): game_state = GameInProgressState(Board.from_file(board_path)) marker_manager = MarkerManager([0, 20, 340, 90, 270], markers_save_path) pygame.init() pygame.display.set_caption('AI cars') screen = pygame.display.set_mode((600, 600)) game_renderer = GameRenderer(screen) game_refresher = GameRefresher(player) while True: events = list(pygame.event.get()) if any([event.type == pygame.QUIT for event in events]): pygame.quit() sys.exit() game_state = game_refresher.refresh_game_state(game_state) game_renderer.render_game(game_state, marker_manager) pygame.display.update()
def evaluate(): if args.player1 == HUMAN or args.player1 == HUMAN: raise Exception("Both players need to be AI") p1 = getAgent(args.player1) p2 = getAgent(args.player2) #compare_agents(agent1: Agent, agent2: Agent, n=5, alternate=True, print_progress=True) data = GameData() game = ConnectGame(data, GameRenderer(screen, data)) res = game.compare_agents(p1, p2, n=12, alternate=True, print_progress=True) print("res:", res)
def run_game(): run = True GameRunner.reset_variables() while run: GameRunner.add_sidescroll_components() GameRunner.generate_needed_objects() GameRunner.delete_unneeded_objects() start_time = time.time() # If the player hits the exit button then the game closes for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() # Has to be above all the drawing because it paints the screen the color of the background window.fill(background) if GameRunner.doggo.current_health <= 0: run = False # Draw Everything has to be here so it can populate history keeper allowing the engines to see the last_platform object WallOfDeath.draw() if not GameRunner.game_is_paused(): GameRenderer.render_players_and_platforms( GameRunner.platforms, GameRunner.doggo) GameRenderer.render_enemies(GameRunner.enemies, GameRunner.platforms) GameRenderer.interaction_engine_runner(GameRunner.doggo, GameRunner.enemies) if PhysicsEngine.is_below_screen_bottom(GameRunner.doggo): run = False GameRenderer.draw_everything(GameRunner.doggo, GameRunner.enemies, GameRunner.platforms, GameRunner.game_is_paused()) pygame.display.update() end_time = time.time() time_taken = end_time - start_time # Why is this needed? if time_taken > 0: VelocityCalculator.time = time_taken GameRunner.reset_variables() GameRunner.run_game()
def start(): agent1 = MinimaxAgent() # red agent2 = RandomAgent() # yellow delay = 0.5 data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(10) agent1_turn = 0 # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. while not game.game_data.game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() sleep(delay) if data.turn == agent1_turn and not game.game_data.game_over: game.make_movement(agent1.get_move(data)) game.update() game.draw() else: game.make_movement(agent2.get_move(data)) game.update() game.draw() game.update() game.draw()
def start(): print("+++++++++++++++ start +++++++++++++++++++++++") #NOTE: None means Human player p1 = None # red p2 = None # yellow if args.player1 != HUMAN: # print ("p1 is NOT human") p1 = getAgent(args.player1) if args.player2 != HUMAN: # print ("p2 is NOT human") p2 = getAgent(args.player2) data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(1000) #NOTE: data.turn of 0 == player 1 # data.turn of 1 == player 2 # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. p_is_human, p = getPlayer(p1, p2, data) #TODO - better way to clean screen? bus.emit("mouse:hover", game.renderer, MouseHoverEvent(-50)) while not game.game_data.game_over: pygame.time.wait(10) if p_is_human: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() if event.type == pygame.MOUSEMOTION: bus.emit("mouse:hover", game.renderer, MouseHoverEvent(event.pos[0])) pygame.display.update() if event.type == pygame.MOUSEBUTTONDOWN: # print("HUMAN: Mouse:",PLAYER(data.turn),p_is_human,p) bus.emit("mouse:click", game, MouseClickEvent(event.pos[0])) p_is_human, p = getPlayer(p1, p2, data) # if event.type == KEYDOWN: # if event.key == pygame.K_z: # mods: int = pygame.key.get_mods() # if mods & pygame.KMOD_CTRL: # bus.emit("game:undo", game) else: #AI print("AI:", PLAYER(data.turn), p.get_name()) game.make_movement(p.get_move(data), p.get_name()) p_is_human, p = getPlayer(p1, p2, data) #keep pygame happy - otherwise freezes for event in pygame.event.get(): pass game.update() game.draw() print("--------------------- GAME OVER ----------------------------") pygame.display.update()
import pygame from pygame.locals import KEYDOWN from connect_game import ConnectGame from events import MouseClickEvent, MouseHoverEvent, bus from game_data import GameData from game_renderer import GameRenderer pygame.init() data = GameData() screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) game.print_board() game.draw() pygame.display.update() pygame.time.wait(1000) # Processes mouse and keyboard events, dispatching events to the event bus. # The events are handled by the ConnectGame and GameRenderer classes. while not game.game_data.game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game.quit() if event.type == pygame.MOUSEMOTION: bus.emit("mouse:hover", game.renderer, MouseHoverEvent(event.pos[0]))