def grid_size(): #user can select size of connect 4 grid while True: print("\t1 = SMALL(5x4)\t 2 = Default(7x6)\t3 = Large(9x8)\n") print ("\t4 = Return to Mode Menu\n") try: #try/except statement, prevents from python displaying error when excecuted g =int(input("Enter the number of the grid you wish to play with: ")) print(" ") if g == 1: #small connect 4 GUI grid g_gui = Grid_mode() g_gui.small_grid() gui = g_gui pause() main() elif g == 2: # standard 7x6 connect 4 GUI grid b_gui = GameGUI() b_gui.intialise_dynamic() gui = b_gui pause() main() elif g == 3: # large connect 4 GUI grid g_gui = Grid_mode() g_gui.large_grid() gui = g_gui pause() main() elif g == 4: modes_menu() #retruns to modes_menu else: print("") print("invalid menu option") grid_size() except ValueError: # if user enters a value/number the print statement displayed print("\nTry Again!!!")
def __init__(self, host, port): self.screen = SCREEN self.host = host self.port = port pygame.init() self.connectToServer() self.kh = KeyHandler() self.dx = 0 self.dy = 0 self.prevMove = (0,0) self.running = True self.sendPosCooldown = 0.0 self.prevTime = 0.0 self.speed = 0.05 self.displayMap = None # GUI self.gameGui = GameGUI(self) self.loginGui = LoginGUI(self) self.mode = "login" self.update = self.updateLogin
def __init__(self, enable_gui=True, highlight_sensors=True, fps=30): self.view_gui = enable_gui self.view_highlight_sensors = highlight_sensors self.fps = fps self.gui = GUI(highlight_sensors=self.view_highlight_sensors, fps=self.fps) if self.view_gui else None self.map = tm() self.new_game = True self.running = False self.moves = 0 self.score = 0
def _run(width, height, playerA, playerB, depth=3, useDecisionTree=True, dynamicDepth=False): print "Running... " board = GameBoard(width, height, playerA, playerB, depth, useDecisionTree, dynamicDepth) board.turn = 1 board.scores = [0, 0] try: gui = GameGUI(board) print "Hello" except Exception as e: pass
def play(player_2_is_human, player_1_is_human, evaluate, search, max_depth, pause_for=0, gui_mode=False): """ A game loop that has either a human player or the AI playing interactively against another human or the AI itself. Recall that player 2 plays first! :param player_2_is_human: True iff the a human is controlling player 2 :type player_2_is_human: bool :param player_1_is_human: True iff the a human is controlling player 1 :type player_1_is_human: bool :param evaluate: a function taking a state and an expanded state and returning a heuristic estimate of the state's utility for the current player :type evaluate: (array of bytes, dict(byte, char)) => numeric :param search: a search function taking a state, an expanded state, an evaluation function, a remaining depth, and returning a (<utility>, <move>) pair :type search: (array of bytes, dict(byte, char), (array of bytes, dict(byte, char)) => numeric, int) => (numeric, (byte, byte)) :param max_depth: the maximum search depth; must be at least 1 :type max_depth: int :param pause_for: number of seconds to pause for after getting the move from the human player or the AI before actually making the move; defaults to 0 :type pause_for: int or float :param gui_mode: should the game be played in gui mode? defaults to False :type gui_mode: bool """ move_number, state, expanded_state = setup_game() if not gui_mode: while True: if play_ply(state, expanded_state, player_2_is_human, pause_for, move_number, evaluate, search, max_depth): break if play_ply(state, expanded_state, player_1_is_human, pause_for, move_number, evaluate, search, max_depth): break move_number += 1 else: from gui import GameGUI GameGUI(evaluate, search, max_depth, player_1_is_human, player_2_is_human)
def _runStats(width, height, playerA, playerB, depth=3, useDecisionTree=True, dynamicDepth=False): line = "%d, %d, %s, %s, %d, %d, %d\n" % ( width, height, playerA, playerB, depth, useDecisionTree, dynamicDepth) writeToStats(line, useDecisionTree) start = time.clock() board = GameBoard(width, height, playerA, playerB, depth, useDecisionTree, dynamicDepth) board.turn = 1 board.scores = [0, 0] try: gui = GameGUI(board) except Exception as e: pass end = time.clock() line = "%f\n\n" % (end - start) writeToStats(line, useDecisionTree)
#! /usr/bin/env python3 # SUGGESTION: use a "hash bang" to make your script executable # https://stackoverflow.com/questions/2429511/why-do-people-write-the-usr-bin-env-python-shebang-on-the-first-line-of-a-pyt import random import sys import time import turtle from constants import FISH from gui import GameGUI from game import Game if __name__ == '__main__': game = Game(max_turns=10, fish_options=FISH, game_gui=GameGUI()) game.printInstructions() game.runGameLoop() # sleep for 30 seconds before exiting time.sleep(1000 * 30) sys.exit()
def game_from_GUI(): GameGUI().mainloop()
def load_board(): #refrences the GUI of connect 4, however it also refereces the loading method. This is to load the game into the GUI. b_gui = GameGUI() b_gui.load = True; b_gui.intialise_dynamic() gui = b_gui
def plays(): # the play method which is is called by the game_menu b_gui = GameGUI() # assign the class of the standard Connect 4 game GUI to variable b_gui (creates new instance of class) b_gui.intialise_dynamic() # creating a method reference to the creation of the Connect 4 Grid GUI gui = b_gui # assigning the variable to the file
class Game(GameClient): def __init__(self, host, port): self.screen = SCREEN self.host = host self.port = port pygame.init() self.connectToServer() self.kh = KeyHandler() self.dx = 0 self.dy = 0 self.prevMove = (0,0) self.running = True self.sendPosCooldown = 0.0 self.prevTime = 0.0 self.speed = 0.05 self.displayMap = None # GUI self.gameGui = GameGUI(self) self.loginGui = LoginGUI(self) self.mode = "login" self.update = self.updateLogin def connectToServer(self): GameClient.__init__(self, self.host, self.port) def addPlayer(self, name, x, y): if name == "anonymous": return print "adding player to map : %s, at %s, %s" % (name, x, y) self.displayMap.addPlayer(name, x, y) def delPlayer(self, name): self.displayMap.delPlayer(name) def addMob(self, name, x=50.0, y=50.0): self.displayMap.addMob(name, 1, x, y) def delMob(self, name): self.displayMap.delMob(name) def setMap(self, mapFileName, x, y): if not self.displayMap: print "Entering map %s" % (mapFileName) self.mode = "game" self.update = self.updateGame self.name = self.loginGui.name self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y) else: if self.displayMap.filename == mapFileName: print "Warping in current map (%s)" % (mapFileName) self.displayMap.players[self.name].setPos(x, y) else: print "Changing map for %s" % (mapFileName) self.displayMap = Map(mapFileName) self.addPlayer(self.name, x, y) def getClosestMobName(self): myRect = self.displayMap.players[self.name].mapRect minDist = 2000.0 closestMob = None for mobName, mob in self.displayMap.mobs.items(): dist = getDist(mob.mapRect, myRect) if dist < minDist: minDist = dist closestMob = mobName return closestMob def sitPlayer(self): sprite = self.displayMap.players[self.name]._sprite if not sprite.sitting: self.SendSitRequest() def addDirtyRect(self, rect): if not self.displayMap: return self.displayMap.addDirtyRect(rect) def updateNetwork(self): self.Loop() def updateLogin(self): self.updateNetwork() self.loginGui.update() def updateGame(self): self.updateNetwork() #time.sleep(0.015) t = pygame.time.get_ticks() x, y = pygame.mouse.get_pos() dt = t - self.prevTime #if dt<10: # return #print "update time : dt = %s" % (dt) self.prevTime = t self.prevMove = (self.dx, self.dy) if self.name not in self.displayMap.players: #print "not connected to map" return events = pygame.event.get() self.kh.handleEvents(events) moved = False # keyboard handling # player direction #self.dx = self.kh.keyDict[KEY_RIGHT] - self.kh.keyDict[KEY_LEFT] #self.dy = self.kh.keyDict[KEY_DOWN] - self.kh.keyDict[KEY_UP] self.dx = pygame.key.get_pressed()[KEY_RIGHT] - pygame.key.get_pressed()[KEY_LEFT] self.dy = pygame.key.get_pressed()[KEY_DOWN] - pygame.key.get_pressed()[KEY_UP] if not self.gameGui.chatWindow.entry.has_focus: if (self.prevMove != (self.dx, self.dy)):# or (t>self.sendPosCooldown): self.displayMap.players[self.name].setMovement(self.dx, self.dy) #self.sendPosCooldown = t+25 #print "Player direction changed from %s to %s/%s" % (self.prevMove, self.dx, self.dy) self.SendUpdateMove(self.displayMap.players[self.name].x, self.displayMap.players[self.name].y, self.dx, self.dy) else: self.dx = 0 self.dy = 0 offx = self.displayMap.players[self.name].mapRect.x-SCREEN_WIDTH/2 offy = self.displayMap.players[self.name].mapRect.y-SCREEN_HEIGHT/2 self.displayMap.setOffset(offx, offy) self.displayMap.update(dt) for event in events: if event.type == pygame.KEYDOWN: if self.gameGui.chatWindow.entry.has_focus: continue key = event.key if key == pygame.K_ESCAPE: #print "Escape and no typing : quit" #pygame.quit() self.running = False if key == pygame.K_SPACE: #print "Starting to type text..." self.SendWarpInfoRequest() self.displayMap.warpVisible = not self.displayMap.warpVisible self.displayMap.collisionVisible = not self.displayMap.collisionVisible self.displayMap.needFullBlit = True if key == KEY_SELECT_TARGET: mobName = self.getClosestMobName() if mobName: self.displayMap.selectTarget(mobName) else: self.displayMap.unselectTarget() if key == KEY_ATTACK: if self.displayMap.selected: self.SendAttackMob(self.displayMap.selected) if key == KEY_SIT: self.sitPlayer() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.displayMap.handleClick() elif event.type == pygame.QUIT: #pygame.quit() self.running = False self.gameGui.handleEvents(events) # graphics self.displayMap.blit(self.screen) # gui display self.gameGui.blit() pygame.display.flip()
from level_data import levels from gui import GameGUI pygame.init() # Initialize Game Screen screen = pygame.display.set_mode((640, 480)) logo_img = pygame.image.load('Assets/Player_1.png') logo_sprite = pygame.transform.scale(logo_img, (32, 32)) pygame.display.set_icon(logo_sprite) pygame.display.set_caption('Invasion Force') # Create Game Instance current_game = game.Game(screen) current_game.create_game() game_gui = GameGUI(screen) print(len(levels)) # Game Loop while current_game.running: game_gui.score_text = game_gui.font.render(f"Score: {current_game.score}", True, (255, 255, 255)) game_gui.level_text = game_gui.font.render( f"Level: {current_game.current_level + 1}", True, (255, 255, 255)) try: game_gui.wave_text = game_gui.font.render( f"Wave: {current_game.game_levels[current_game.current_level].current_wave}", True, (255, 255, 255)) except IndexError: pass
def updateAdventureGame(mouse): nonlocal gameGUI if not self.__gameBoard: adventure.hideCutscene() self.play(isAdventure = True, **adventure.getPlaySettings()) gameGUI = GameGUI(self.__width, self.__height, self.__gameBoard) gameGUI.sync() gameGUI.draw(self) gameGUI.sync() gameGUI.checkClick(mouse) self.__gameBoard.clickTile(mouse) if not self.__gameBoard.isRunning: self.state = 'ended' if self.__gameBoard.hasWon: self.__gameBoard.undraw() gameGUI.undraw() adventure.win() self.__gameBoard = False if adventure.isDone: self.state = 'menu' else: adventure.showCutscene(self)
class TartarusGame(object): def __init__(self, enable_gui=True, highlight_sensors=True, fps=30): self.view_gui = enable_gui self.view_highlight_sensors = highlight_sensors self.fps = fps self.gui = GUI(highlight_sensors=self.view_highlight_sensors, fps=self.fps) if self.view_gui else None self.map = tm() self.new_game = True self.running = False self.moves = 0 self.score = 0 def create_new_game(self): self.map.new_map() self.moves = 0 self.score = 0 if self.view_gui: self.gui.draw_map(self.map) self.gui.draw_moves(moves=self.moves) self.gui.draw_score(score=self.score) def action_turn_left(self): self.map.robot_direction -= 1 if self.map.robot_direction < DIR_UP: self.map.robot_direction += 4 if self.view_gui: self.gui.draw_robot(self.map) def action_turn_right(self): self.map.robot_direction += 1 if self.map.robot_direction > DIR_LEFT: self.map.robot_direction -= 4 if self.view_gui: self.gui.draw_robot(self.map) def action_move_forward(self): next_field_1, next_field_2 = self.get_next_two_fields() if not self.is_action_move_valid(next_field_1, next_field_2): return if self.map.robot_direction == DIR_UP: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1]] = FIELD_EMPTY self.map.map[self.map.robot_pos[0] - 1][self.map.robot_pos[1]] = FIELD_ROBOT if next_field_1 == FIELD_BOX: self.map.map[self.map.robot_pos[0] - 2][self.map.robot_pos[1]] = FIELD_BOX self.map.robot_pos = [self.map.robot_pos[0] - 1, self.map.robot_pos[1]] elif self.map.robot_direction == DIR_RIGHT: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1]] = FIELD_EMPTY self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] + 1] = FIELD_ROBOT if next_field_1 == FIELD_BOX: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] + 2] = FIELD_BOX self.map.robot_pos = [self.map.robot_pos[0], self.map.robot_pos[1] + 1] elif self.map.robot_direction == DIR_DOWN: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1]] = FIELD_EMPTY self.map.map[self.map.robot_pos[0] + 1][self.map.robot_pos[1]] = FIELD_ROBOT if next_field_1 == FIELD_BOX: self.map.map[self.map.robot_pos[0] + 2][self.map.robot_pos[1]] = FIELD_BOX self.map.robot_pos = [self.map.robot_pos[0] + 1, self.map.robot_pos[1]] elif self.map.robot_direction == DIR_LEFT: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1]] = FIELD_EMPTY self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] - 1] = FIELD_ROBOT if next_field_1 == FIELD_BOX: self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] - 2] = FIELD_BOX self.map.robot_pos = [self.map.robot_pos[0], self.map.robot_pos[1] - 1] def is_action_move_valid(self, next_1=None, next_2=None): if not next_1: next_field_1, next_field_2 = self.get_next_two_fields() else: next_field_1 = next_1 next_field_2 = next_2 if (next_field_1 == FIELD_WALL) or \ (next_field_1 == FIELD_BOX and (next_field_2 == FIELD_BOX or next_field_2 == FIELD_WALL)): return False return True def get_next_two_fields(self): next_field_1 = None next_field_2 = None if self.map.robot_direction == DIR_UP: next_field_1 = self.map.map[self.map.robot_pos[0] - 1][self.map.robot_pos[1]] if not next_field_1 == FIELD_WALL: next_field_2 = self.map.map[self.map.robot_pos[0] - 2][self.map.robot_pos[1]] elif self.map.robot_direction == DIR_RIGHT: next_field_1 = self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] + 1] if not next_field_1 == FIELD_WALL: next_field_2 = self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] + 2] elif self.map.robot_direction == DIR_DOWN: next_field_1 = self.map.map[self.map.robot_pos[0] + 1][self.map.robot_pos[1]] if not next_field_1 == FIELD_WALL: next_field_2 = self.map.map[self.map.robot_pos[0] + 2][self.map.robot_pos[1]] elif self.map.robot_direction == DIR_LEFT: next_field_1 = self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] - 1] if not next_field_1 == FIELD_WALL: next_field_2 = self.map.map[self.map.robot_pos[0]][self.map.robot_pos[1] - 2] return next_field_1, next_field_2 def update_score(self, ai_score_map=False): score_map = [[2, 1, 1, 1, 1, 2], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [2, 1, 1, 1, 1, 2]] if ai_score_map: score_map = [[200, 150, 100, 100, 150, 200], [150, 10, 5, 5, 10, 150], [100, 5, 0, 0, 5, 100], [100, 5, 0, 0, 5, 100], [150, 10, 5, 5, 10, 150], [200, 150, 100, 100, 150, 200]] temp_score = 0 for y in range(0, MAP_SIZE): for x in range(0, MAP_SIZE): if self.map.map[y + 1][x + 1] == FIELD_BOX: temp_score += score_map[y][x] if self.score != temp_score: self.score = temp_score def run(self, autorun=False, random_movement=True, action=None): while True: if self.new_game: self.create_new_game() self.new_game = False self.running = True if autorun: self.run_auto(random_movement=random_movement, action=action) else: self.run_manual() if self.moves >= MAX_MOVES: self.running = False def run_manual(self): if self.view_gui: move = self.gui.run() self.perform_action(action=move) def run_auto(self, random_movement=True, action=None, ai_score_map=False): move = action if self.view_gui: move = self.gui.run_auto(random_actions=random_movement, action=action) self.perform_action(action=move, ai_score_map=ai_score_map) if self.view_gui: self.gui.update_display() def perform_action(self, action, ai_score_map=False): if action == ACTION_RESET: self.new_game = True return if self.running: if action == ACTION_MOVE_FORWARD: self.action_move_forward() elif action == ACTION_TURN_LEFT: self.action_turn_left() elif action == ACTION_TURN_RIGHT: self.action_turn_right() self.moves += 1 self.update_score(ai_score_map=ai_score_map) if self.view_gui: self.gui.draw_map(self.map) self.gui.draw_moves(self.moves) self.gui.draw_score(self.score)