You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hnu202109060118 157d12d24f
Update README.md
2 years ago
README.md Update README.md 2 years ago

README.md

import pygame

pygame.init() screen = pygame.display.set_mode((600, 670)) # 设置窗口长宽 pygame.display.set_caption('五子棋-EduCoder') # 设置 pygame 窗口名 space = 20 cell_size = 40 # 每个格子大小 cell_num = 15 # 设置线条数 chess_arr = [] # 创建列表记录落子位置 flag = 1

def lianzi(lx, ly, dx, dy, m): tx, ty, s = lx, ly, 0 while True: tx += dx ty += dy if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s s += 1

def win(chess_arr, flag): lx = chess_arr[-1][0] ly = chess_arr[-1][1] m = [[0] * cell_num for i in range(cell_num)] for x, y, c in chess_arr: if c == flag: m[y][x] = 1 dire_arr = (-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)], [(-1, 1), (1, -1)

for dire1, dire2 in dire_arr:
    dx, dy = dire1
    num1 = lianzi(lx, ly, dx, dy, m)
    dx, dy = dire2
    num2 = lianzi(lx, ly, dx, dy, m)
    if num1 + num2 + 1 >= 5: return True
return False

game_state = 1 while True: pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit()

    if event.type == pygame.MOUSEBUTTONUP:
        x, y = pygame.mouse.get_pos()
        xi, yi = int((x - space) / cell_size + 0.5), int((y - space) / cell_size + 0.5)
        if game_state == 1 and 0 <= xi < cell_num and 0 <= yi < cell_num and (xi, yi, 1) not in chess_arr and (
                xi, yi, 2) not in chess_arr:
            chess_arr.append((xi, yi, flag))
            if win(chess_arr, flag):
                game_state = 2 if flag == 1 else 3
            else:
                flag = 2 if flag == 1 else 1
        if 0 <= x <= 600 <= y <= 660:
            if 20 <= x <= 140 and 600 <= y <= 660:
                if chess_arr == []:
                    print('当前没有棋子')
                else:
                    del chess_arr[-1]
                    flag = 2 if flag == 1 else 1
            if 160 <= x <= 280 and 600 <= y <= 660:
                chess_arr = []
                game_state = 1
            if 300 <= x <= 420 and 600 <= y <= 660:
                if flag == 1:
                    game_state = 3
                if flag == 2:
                    game_state = 2

pygame.draw.rect(screen, (100, 100, 0), ((0, 600), (80, 40)), 0)

screen.fill((204, 153, 102))
# pygame.draw.line(窗口, 线条颜色, 开始坐标, 结束坐标, 线条大小 )
for x in range(space, 601 - space, cell_size):
    pygame.draw.line(screen, (200, 200, 200), (x, 0 + space), (x, 600 - space), 1)
for y in range(space, 601 - space, cell_size):
    pygame.draw.line(screen, (200, 200, 200), (space, y), (600 - space, y), 1)

pygame.draw.rect(screen, (180, 120, 80), ((20, 600), (120, 60)), 0)
pygame.draw.rect(screen, (150, 100, 75), ((20, 600), (120, 60)), 1)
myfont1 = pygame.font.SysFont('microsoftjhengheiui', 30)
win_text = '撤 回'
textImage = myfont1.render(win_text, True, (0, 0, 0))
screen.blit(textImage, (45, 610))

pygame.draw.rect(screen, (180, 120, 80), ((160, 600), (120, 60)), 0)
pygame.draw.rect(screen, (150, 100, 75), ((160, 600), (120, 60)), 1)
win_text = '重 开'
textImage = myfont1.render(win_text, True, (0, 0, 0))
screen.blit(textImage, (185, 610))

pygame.draw.rect(screen, (180, 120, 80), ((300, 600), (120, 60)), 0)
pygame.draw.rect(screen, (150, 100, 75), ((300, 600), (120, 60)), 1)
win_text = '认 输'
textImage = myfont1.render(win_text, True, (0, 0, 0))
screen.blit(textImage, (325, 610))

pygame.draw.circle(screen, (0, 0, 0), (space + 4 * cell_size, space + 4 * cell_size), 2, 3)
pygame.draw.circle(screen, (0, 0, 0), (space + 4 * cell_size, space + 10 * cell_size), 2, 3)
pygame.draw.circle(screen, (0, 0, 0), (space + 10 * cell_size, space + 4 * cell_size), 2, 3)
pygame.draw.circle(screen, (0, 0, 0), (space + 10 * cell_size, space + 10 * cell_size), 2, 3)
pygame.draw.circle(screen, (0, 0, 0), (space + 7 * cell_size, space + 7 * cell_size), 2, 3)

for x, y, c in chess_arr:
    chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
    pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
for x, y, c in chess_arr:
    m = [[0] * cell_num for i in range(cell_num)]
    if c == 1:
        m[y][x] = 1
    elif c == 2:
        m[y][x] = 2
if game_state != 1:
    myfont = pygame.font.SysFont('microsoftjhengheiui', 60)
    white = (255, 255, 255) if game_state == 2 else (0, 0, 0)

    win_text = "%s 方 胜 利" % ('黑' if game_state == 2 else '白')
    textImage = myfont.render(win_text, True, white)
    screen.blit(textImage, (150, 240))
pygame.display.update()