class GameInterface(Tk): def __init__(self, choose_point_callback, concede_callback=None): super().__init__() self.config(bg=Config.GUI.COLOR) #self.withdraw() #self.start_menu = StartMenu(self) n = Config.Field.CELL_COUNT l = Config.Field.CELL_SIZE canvas_size = (n + 1) * l + 2 * Config.Field.CANV_INDENTS self.title('POP-Go') root_size = ((Config.Field.CELL_COUNT + 1) * l + Config.GameMenu.SIZE + 3 * Config.Field.CANV_INDENTS) self.geometry('{}x{}'.format(root_size, canvas_size)) self.resizable(height=False, width=False) self.locked = False self.field = Field(self, choose_point_callback) self.field.place(x=Config.Field.CANV_INDENTS, y=Config.Field.CANV_INDENTS) self.game_menu = GameMenu(self) self.game_menu.place(x=2 * Config.Field.CANV_INDENTS + (n + 1) * l, y=Config.Field.CANV_INDENTS) def display(self, hull, score1, score2, points): self.field.display(hull, points) self.game_menu.score(score1, score2) def lock(self): self.locked = True self.field.lock() self.game_menu.lock() def unlock(self): self.locked = False self.field.unlock() self.game_menu.unlock() def report(self, s): self.start_menu.report(s)
class Game(object): def __init__(self): self.bRunning = False self.field = Field() self.field.setPitch(pitchtype="standard") self.teams = [] self.length = 10 fieldCenter = self.field.getCenter() self.ball = Ball() self.ball.setPos(fieldCenter[0], fieldCenter[1], 0) def start(self): tmp = datetime.now() startTime = tmp.minute * 60 + tmp.second timenow = datetime.now() endTime = timenow.minute * 60 + timenow.second self.bRunning = True while (self.bRunning and endTime - startTime < 10): self.loop() self.display() for event in pygame.event.get(): if event.type == pygame.QUIT: self.bRunning = False timenow = datetime.now() endTime = timenow.minute * 60 + timenow.second def loop(self): pass def display(self): (width, height) = (int( (self.field.boundary.length + 2 * self.field.margin) * 10), int((self.field.boundary.width + 2 * self.field.margin) * 10)) screen = pygame.display.set_mode((width, height)) self.field.display(screen) self.ball.display(screen) pygame.display.update()
button = input("Button: ").strip().lower() if button == 'w': button = 1 move.y += 1 elif button == 's': button = 2 move.y -= 1 elif button == 'a': button = 3 move.x -= 1 elif button == 'd': button = 4 move.x += 1 elif button == 'q': halls.display(mapping) continue elif button == 'e': halls.display() print(f"Center: {halls.center}") continue else: print(f"ERROR. Unrecognized button {str(button)}") outs = comp.execute_tape(button) print(outs) status = outs[0] if outs[1][0] == 0: halls.set_val(-1, move) elif outs[1][0] == 1:
for y in range(len(COMPUTER_FIELD)): COMPUTER_FIELD[x][y] = ' ' BATTLE_FIELD = [[0] * m for i in range(n)] for x in range(len(BATTLE_FIELD)): for y in range(len(BATTLE_FIELD)): BATTLE_FIELD[x][y] = ' ' user = Man(USER_FIELD, COMPUTER_FIELD) #пользователь Игрок computer = Computer(COMPUTER_FIELD, USER_FIELD) #пользователь Компьютер COMPUTER_FIELD = computer.replace_computers_ships( COMPUTER_FIELD) #Разместить корабли компьютера USER_FIELD = user.replace_ships(USER_FIELD) #пользователь размещает компьютер print("Поле игрока") Field.display(USER_FIELD) #Поле игрока flag = True while True: COMPUTER_FIELD = user.shoot(COMPUTER_FIELD) flag = Field.check_to_victory(COMPUTER_FIELD) if flag == True: print("Ты поДЕБил") break USER_FIELD = computer.computer_shoot(USER_FIELD) flag = Field.check_to_victory(USER_FIELD) if flag == True: print("Компьютер поДЕБил") break print('Поле игрока') Field.display(USER_FIELD) #Поле игрока
def start_game(self): while True: print() print("Chose the game mode: ") print("[1] 1 Player vs Bot") print("[2] 2 Players") print() command = input("Enter command: ") if command.isdigit() and command == "1": while True: print("Enter size of grid (between 3 to 10): ") print() size = input("Enter size: ") if size.isdigit() and 3 <= int(size) <= 10: break else: print("Please enter correct size!") field = Field(int(size)) player = Player("X") bot = Bot("O") checker = Checker(field.grid) first_player = randint(0, 1) if first_player == 0: field.display() not_finished = True while not_finished: y, x = player.make_move(field.grid) field.fill_cell(player.symbol, x, y) field.display() flag = checker.do_win_the_game(player) if flag: print("You won the game!") not_finished = False no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break if not flag: y, x = bot.make_move(field.grid) field.fill_cell(bot.symbol, x, y) field.display() flag = checker.do_win_the_game(bot) if flag: print("Bot won the game!") break no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break else: field.display() not_finished = True while not_finished: y, x = bot.make_move(field.grid) field.fill_cell(bot.symbol, x, y) field.display() flag = checker.do_win_the_game(bot) if flag: print("Bot won the game!") not_finished = False no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break if not flag: y, x = player.make_move(field.grid) field.fill_cell(player.symbol, x, y) field.display() flag = checker.do_win_the_game(player) if flag: print("You won the game!") break no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break elif command.isdigit() and command == "2": while True: print("Enter size of grid (between 3 to 10): ") print() size = input("Enter size: ") if size.isdigit() and 3 <= int(size) <= 10: break else: print("Please enter correct size!") field = Field(int(size)) player_1 = Player("X") player_2 = Player("O") checker = Checker(field.grid) first_player = randint(0, 1) if first_player == 0: field.display() not_finished = True while not_finished: print("Player #1 turn:") y, x = player_1.make_move(field.grid) field.fill_cell(player_1.symbol, x, y) flag = checker.do_win_the_game(player_1) if flag: print("Player #1 won the game!") not_finished = False no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break if not flag: print("Player #2 turn:") y, x = player_2.make_move(field.grid) field.fill_cell(player_2.symbol, x, y) field.display() flag = checker.do_win_the_game(player_2) if flag: print("Player #2 won the game!") break no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break else: field.display() not_finished = True while not_finished: print("Player #2 turn:") y, x = player_2.make_move(field.grid) field.fill_cell(player_2.symbol, x, y) field.display() flag = checker.do_win_the_game(player_2) if flag: print("Player #2 won the game!") not_finished = False no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break if not flag: print("Player #1 turn:") y, x = player_1.make_move(field.grid) field.fill_cell(player_1.symbol, x, y) field.display() flag = checker.do_win_the_game(player_1) if flag: print("Player #1 won the game!") break no_empty = checker.no_empty_cell() if no_empty: print("It's a draw!") break else: print("Please enter correct command!") while True: print() print("Do you want to play again?") print("[1] YES") print("[2] EXIT") command = input("Enter command: ") if command.isdigit() and command == "1": break elif command == "2": exit(1) else: print("Please enter correct command!")