Update 五子棋代码.py

master
hnu202109060119 2 years ago
parent 71ccc5d770
commit ab63b6a149

@ -1,96 +1,83 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Created on Fri Dec 9 18:27:50 2022 Created on Fri Dec 9 18:27:50 2022
@author: lsy @author: lsy
""" """
import pygame import pygame
pygame.init()
pygame.init() screen = pygame.display.set_mode((600, 600))
screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption('五子棋-EduCoder')
pygame.display.set_caption('五子棋-EduCoder') space = 20
space = 20 cell_size = 40
cell_size = 40 cell_num = 15
cell_num = 15 chess_arr = []
chess_arr = [] def get_one_dire_num(lx, ly, dx, dy, m):
def get_one_dire_num(lx, ly, dx, dy, m): tx = lx
ty = ly
tx = lx s = 0
ty = ly while True:
s = 0 tx += dx
while True: ty += dy
tx += dx if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
ty += dy s+=1
if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s def check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)]
s+=1 for x, y, c in chess_arr:
def check_win(chess_arr, flag): if c == flag:
m = [[0] * cell_num for i in range(cell_num)] m[y][x] = 1
for x, y, c in chess_arr: lx = chess_arr[-1][0]
if c == flag: ly = chess_arr[-1][1]
m[y][x] = 1 dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
lx = chess_arr[-1][0] [(-1, 1), (1, -1)]]
ly = chess_arr[-1][1] for dire1, dire2 in dire_arr:
dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)], dx, dy = dire1
[(-1, 1), (1, -1)]] num1 = get_one_dire_num(lx, ly, dx, dy, m)
for dire1, dire2 in dire_arr: dx, dy = dire2
num2 = get_one_dire_num(lx, ly, dx, dy, m)
dx, dy = dire1 if num1 + num2 + 1 >= 5: return True
num1 = get_one_dire_num(lx, ly, dx, dy, m) return False
flag = 1
dx, dy = dire2 game_state = 1
num2 = get_one_dire_num(lx, ly, dx, dy, m) while True:
for event in pygame.event.get():
if num1 + num2 + 1 >= 5: return True if event.type == pygame.QUIT:
return False pygame.quit()
flag = 1 exit()
game_state = 1 if game_state == 1 and event.type == pygame.MOUSEBUTTONUP:
while True: x, y = pygame.mouse.get_pos()
for event in pygame.event.get(): xi = int(round((x - space) * 1.0 / cell_size))
if event.type == pygame.QUIT: yi = int(round((y - space) * 1.0 / cell_size))
pygame.quit() if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi,1) not in chess_arr and (xi, yi,2) not in chess_arr:
exit() chess_arr.append((xi, yi,flag))
if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: if check_win(chess_arr, flag):
x, y = pygame.mouse.get_pos() game_state = 2 if flag == 1 else 3
xi = int(round((x - space) * 1.0 / cell_size)) else:
yi = int(round((y - space) * 1.0 / cell_size)) flag = 2 if flag == 1 else 1
if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi,1) not in chess_arr and (xi, yi,2) not in chess_arr: screen.fill((204, 153, 102))
chess_arr.append((xi, yi,flag)) for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space),
if check_win(chess_arr, flag): (x + space, cell_size * (cell_num - 1) + space), 1)
for y in range(0, cell_size * cell_num, cell_size):
game_state = 2 if flag == 1 else 3 pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space),
else: (cell_size * (cell_num - 1) + space, y + space), 1)
for x, y,c in chess_arr:
flag = 2 if flag == 1 else 1 chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
screen.fill((204, 153, 102)) pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
if (4, 4,1) not in chess_arr and (4, 4,2) not in chess_arr:
for x in range(0, cell_size * cell_num, cell_size): pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1)
pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space), if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr:
(x + space, cell_size * (cell_num - 1) + space), 1) pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1)
if (10, 4, 1) not in chess_arr and (10, 4, 2) not in chess_arr:
for y in range(0, cell_size * cell_num, cell_size): pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1)
pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space), if (4, 10, 1) not in chess_arr and (4 ,10, 2) not in chess_arr:
(cell_size * (cell_num - 1) + space, y + space), 1) pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1)
if (7, 7, 1) not in chess_arr and (7, 7, 2) not in chess_arr:
for x, y,c in chess_arr: pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1)
chess_color = (30, 30, 30) if c == 1 else (225, 225, 225) if game_state != 1:
pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16) myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
if (4, 4,1) not in chess_arr and (4, 4,2) not in chess_arr: win_text = "%s win" % ('black' if game_state == 2 else 'white')
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1) textImage = myfont.render(win_text, True, white)
if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr: screen.blit(textImage, (260, 320))
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1) pygame.display.update()
if (10, 4, 1) not in chess_arr and (10, 4, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1)
if (4, 10, 1) not in chess_arr and (4 ,10, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1)
if (7, 7, 1) not in chess_arr and (7, 7, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1)
if game_state != 1:
myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
win_text = "%s win" % ('black' if game_state == 2 else 'white')
textImage = myfont.render(win_text, True, white)
screen.blit(textImage, (260, 320))
pygame.display.update()

Loading…
Cancel
Save