猫史档案馆


双人围棋游戏

用户:治愈绾兮治愈绾兮查看:2 回复:3 评论:2 创建时间:2021-01-24T18:39:52


import sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("双人围棋游戏")
font1 = pygame.font.SysFont("SimHei", 20)
font2 = pygame.font.SysFont("arial", 35)
background_color = 150, 180, 150
black = 0, 0, 0
white = 255, 255, 255
circle_color = 0, 0, 0
lines_color = 100, 0, 0
text_color = 50, 0, 0


def print_text(font, x, y, text, color=(0, 0, 0)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x, y))


mouse_x = 0
mouse_y = 0
mouse_up = 0
mouse_up_x = 0
mouse_up_y = 0


class Go:
    def __init__(self):
        self.__checkerboard_data = [[0 for i in range(9)] for j in range(9)]
        self.__player_flag = -1
        self.__game_over = 0
        self.__mouse_x = 0
        self.__mouse_y = 0
        self.__suicide_flag = 0
        self.__stack = []
        self.__repeat_flag = 0
        self.__isalive_flag = 0

    def __draw_checker(self):
        color = lines_color
        line_width = 1
        rect_pos = 100, 100, 400, 400
        pygame.draw.rect(screen, color, rect_pos, line_width * 2)
        for i in range(7):
            pygame.draw.line(screen, color, (100, 100 + i * 50 + 50),
                             (500, 100 + i * 50 + 50), line_width)
        for i in range(7):
            pygame.draw.line(screen, color, (100 + i * 50 + 50, 100),
                             (100 + i * 50 + 50, 500), line_width)
        positions = [[300, 300], [200, 200], [
            200, 400], [400, 200], [400, 400]]
        for pos in positions:
            pygame.draw.circle(screen, color, pos, 5, 0)

    def get_mouse_current_position(self, x, y):
        self.__mouse_x = x
        self.__mouse_y = y

    def __chess_follow(self):
        pos = self.__mouse_x, self.__mouse_y
        if self.__player_flag == -1:
            color = black
        elif self.__player_flag == 1:
            color = white
        pygame.draw.circle(screen, color, pos, 19, 0)
        pygame.draw.circle(screen, circle_color, pos, 20, 2)

    def switch_player(self):
        self.__player_flag = - self.__player_flag

    def make_a_move(self, x, y):
        self.__suicide_flag = 0
        self.__repeat_flag == 0
        if x >= 75 and x <= 525 and y >= 75 and y <= 525:
            row = (y - 75) // 50
            col = (x - 75) // 50
            if self.__checkerboard_data[row][col] == 0:
                self.__push()
                self.__checkerboard_data[row][col] = self.__player_flag
                self.__player_flag = -self.__player_flag
                self.__take_out()
                self.__if_suicide(row, col)
                self.__if_repeat()

    def __if_suicide(self, row, col):
        if self.__suicide_flag == 1:
            self.__checkerboard_data[row][col] = 0
            self.__player_flag = -self.__player_flag

    def __push(self):
        self.__stack.append(self.__checkerboard_data)
        if len(self.__stack) > 5:
            self.__stack.pop(0)

    def take_back_a_move(self):
        if len(self.__stack) != 0:
            self.__checkerboard_data = self.__stack.pop()

    def __if_repeat(self):
        if self.__repeat_flag == 1:
            pass

    def play(self):
        self.__draw_checker()
        self.__if_game_over()
        self.__print_texts()
        self.__draw_chesses()
        self.__chess_follow()

    def __print_texts(self):
        if self.__suicide_flag == 1:
            print_text(font1, 165, 535, "禁止喵!", text_color)

    def __draw_chesses(self):
        for i in range(9):
            for j in range(9):
                if self.__checkerboard_data[i][j] == 0:
                    continue
                elif self.__checkerboard_data[i][j] == -1:
                    color = black
                elif self.__checkerboard_data[i][j] == 1:
                    color = white
                pos = j * 50 + 100, i * 50 + 100
                pygame.draw.circle(screen, color, pos, 19, 0)
                pygame.draw.circle(screen, circle_color, pos, 20, 2)

    def restart_game(self):
        self.__game_over = 1

    def __clear_checkerboard(self):
        self.__checkerboard_data = [[0 for i in range(9)] for j in range(9)]

    def __if_game_over(self):
        if self.__game_over == 1:
            self.__clear_checkerboard()
            self.__game_over = 0
            self.__player_flag = -1
            self.__stack = []

    def __DFS(self, x, y):
        self.__visit[x][y] = 1
        directions = [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]
        for dx, dy in directions:
            if dx < 0 or dx > 8 or dy < 0 or dy > 8:
                continue
            elif self.__visit[dx][dy] == 0:
                if self.__checkerboard_data[dx][dy] == 0:
                    self.__isalive_flag = 1
                    return
                elif self.__checkerboard_data[dx][dy] == - self.__checkerboard_data[x][y]:
                    continue
                elif self.__checkerboard_data[dx][dy] == self.__checkerboard_data[x][y]:
                    self.__DFS(dx, dy)
        return

    def __clear_visit(self):
        self.__visit = [[0 for i in range(9)] for j in range(9)]
        self.__isalive_flag = 0

    def __is_alive(self, x, y):
        isalive = 1
        self.__clear_visit()
        self.__DFS(x, y)
        if self.__isalive_flag == 0:
            isalive = 0
        self.__clear_visit()
        return isalive

    def __take_out(self):
        token_list = []
        for i in range(9):
            for j in range(9):
                if self.__checkerboard_data[i][j] == 0:
                    continue
                elif self.__checkerboard_data[i][j] == self.__player_flag and self.__is_alive(i, j) == 0:
                    token_list.append([i, j])
        if len(token_list) != 0:
            for i, j in token_list:
                self.__checkerboard_data[i][j] = 0
        for i in range(9):
            for j in range(9):
                if self.__checkerboard_data[i][j] == - self.__player_flag:
                    if self.__is_alive(i, j) == 0:
                        self.__suicide_flag = 1
                        break


go = Go()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x, mouse_y = event.pos
            go.get_mouse_current_position(mouse_x, mouse_y)
        elif event.type == MOUSEBUTTONUP:
            mouse_up = event.button
            mouse_up_x, mouse_up_y = event.pos
            go.make_a_move(mouse_up_x, mouse_up_y)
    screen.fill(background_color)
    go.play()
    pygame.display.update()


回复

上一页1 页 / 共 1下一页
该账号已被封禁该账号已被封禁

az
再发一遍

你是抄的还是自己写的?

点赞0


评论


该账号已被封禁该账号已被封禁

这篇文章一点也不㵘,可燚了

点赞0


评论


yh12i31yh12i31

厉害!center_image

点赞0


评论