コード例 #1
0
ファイル: main.py プロジェクト: poplock1/Connect_four
class Game():
    def __init__(self):
        pygame.init()
        self.game_display = pygame.display.set_mode(
            (config.display_w, config.display_h))
        pygame.display.set_caption(config.caption)
        self.running = True
        self.clock = pygame.time.Clock()
        self.click = False
        self.player_win = False
        self.difficulty = False
        self.choose = None

    def new_game(self):
        while self.running:
            self.grid = Grid(config.rows, config.columns, self)
            self.agent = Agent(self.grid, self)
            self.run()
            if self.restart():
                self.running = True
            else:
                self.running = False

    def run(self):
        self.turn = config.AI_turn
        self.playing = True
        while self.playing:
            self.check_filled()
            self.clock.tick(60)
            self.events()
            self.update()
            self.draw()
            if self.winning(self.grid.grid, config.player_mark):
                self.player_win = True
                self.playing = False
            elif self.winning(self.grid.grid, config.AI_mark):
                self.playing = False

    def events(self):
        self.click = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.playing = False
                self.running = False
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.click = True

        self.mouse_pos = pygame.mouse.get_pos()
        self.keys = pygame.key.get_pressed()

        if self.keys[pygame.K_ESCAPE]:
            self.playing = False
            self.running = False
            pygame.quit()
            sys.exit()

    def update(self):
        if self.turn == config.AI_turn:
            self.grid.update(config.AI_mark)
            self.turn += 1
            self.turn = self.turn % 2
        elif self.turn == config.player_turn:
            if self.click:
                self.grid.update(config.player_mark)
                self.turn += 1
                self.turn = self.turn % 2

    def draw(self):
        self.game_display.fill(config.bg_color)
        self.grid.draw_grid(self.game_display)
        pygame.display.update()

    def check_filled(self):
        self.filled_spots = 0
        for columns in range(self.grid.cols):
            if self.grid.open_spot(columns) == "XD":
                self.filled_spots += 1

        if self.filled_spots == self.grid.cols:
            self.playing = False

    def winning(self, grid, piece):

        # Horizontal
        for row in range(self.grid.row):
            for column in range(self.grid.cols - (config.inarow - 1)):
                self.window = list(grid[row, column:column + config.inarow])
                if self.window.count(piece) == config.inarow:
                    return True

        # Vertical
        for row in range(self.grid.row - (config.inarow - 1)):
            for column in range(self.grid.cols):
                self.window = list(grid[row:row + config.inarow, column])
                if self.window.count(piece) == config.inarow:
                    return True

        # Positive diagonal
        for row in range(self.grid.row - (config.inarow - 1)):
            for column in range(self.grid.cols - (config.inarow - 1)):
                self.window = list(grid[range(row, row + config.inarow),
                                        range(column, column + config.inarow)])
                if self.window.count(piece) == config.inarow:
                    return True

        # Negative diagonal
        for row in range((config.inarow - 1), self.grid.row):
            for column in range(self.grid.cols - (config.inarow - 1)):
                self.window = list(grid[range(row, row - config.inarow, -1),
                                        range(column, column + config.inarow)])
                if self.window.count(piece) == config.inarow:
                    return True

        return False

    def start_screen(self):
        self.start = False
        while not self.start:
            self.events()
            self.start = gui.start_screen(self.mouse_pos, self.click,
                                          self.game_display)

    def restart(self):
        self.reset = False
        if self.player_win:
            self.winner = config.player_mark
        else:
            self.winner = config.AI_mark
        while not self.reset:
            self.events()
            self.choose = gui.restart_screen(self.mouse_pos, self.click,
                                             self.game_display, self.winner)
            if self.choose:
                return self.choose
            elif self.choose == False:
                return self.choose
            else:
                pass

    def game_over(self):
        pass

    def difficulty_screen(self):
        while not self.difficulty:
            self.events()
            self.difficulty = gui.difficulty_screen(self.mouse_pos, self.click,
                                                    self.game_display)
コード例 #2
0
ファイル: main.py プロジェクト: poplock1/Number_guesser
class Game():
    def __init__(self):
        pygame.init()
        self.game_display = pygame.display.set_mode(
            (stg.display_x, stg.display_y))
        pygame.display.set_caption(stg.display_title)
        self.tf_model = tf.keras.models.load_model('num_reader.model')
        self.running = True
        self.guessing = False
        self.drawing = True
        self.clock = pygame.time.Clock()
        self.click = False
        self.prediction_text = None


    def new_board(self):
        if self.running:
            self.guessing = False
            self.drawing = True
            self.board = Grid()

    def new_guess(self):
        self.info_text1 = gui.TextWindow(
            stg.text3_x, stg.text3_y, stg.text3_w, stg.text3_h, stg.text3_text_color, stg.text3_text, stg.text3_font)
        self.info_text2 = gui.TextWindow(
            stg.text4_x, stg.text4_y, stg.text4_w, stg.text4_h, stg.text4_text_color, stg.text4_text, stg.text4_font)
        while self.running:
            self.new_board()
            self.run()

    def run(self):
        self.playing = True
        while self.playing:
            self.clock.tick(60)
            self.events()
            self.update()
            self.draw()

    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.playing = False
                self.running = False
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.click = True
            if event.type == pygame.MOUSEBUTTONUP:
                self.click = False

        self.mouse_pos = pygame.mouse.get_pos()
        self.keys = pygame.key.get_pressed()

        if self.keys[pygame.K_ESCAPE]:
            self.playing = False
            self.running = False
            pygame.quit()
            sys.exit()
        if self.keys[pygame.K_SPACE]:
            self.drawing = False
            self.guessing = True
        if self.keys[pygame.K_c]:
            self.new_board()

    def update(self):
        if self.drawing:
            if self.click:
                self.board.update()
        elif self.guessing:
            self.guess()

    def draw(self):
        self.game_display.fill(stg.BG_COLOR)
        self.board.draw(self.game_display)
        if self.drawing:
            self.info_text1.draw(self.game_display)
        elif self.prediction_text:
            self.prediction_text.draw(self.game_display)
            self.info_text2.draw(self.game_display)
        pygame.display.update()

    def guess(self):
        # self.data = self.overwriting_data()
        self.data = np.reshape(self.board.grid, (-1, 28, 28))
        self.predictions = self.tf_model.predict(self.data)
        self.prediction = (np.argmax(self.predictions[0]))
        self.prediction_text = gui.TextWindow(stg.text2_x, stg.text2_y, stg.text2_w, stg.text2_h,
                                              stg.text2_text_color, (f'{stg.text2_text}{self.prediction}'), stg.text2_font)
        self.guessing = False