课程:《Python程序设计》
班级: 1923
姓名: 王梓全
学号:20192305
实验教师:王志强
实验日期:2021年6月14日
必修/选修:公选课
import random
import pygame
from pygame.locals import MOUSEBUTTONUP
pygame.init()
cell_size = 40
cell_num = 15
grid_size = cell_size * (cell_num - 1)
screencaption = pygame.display.set_caption(‘五子棋‘)
screen = pygame.display.set_mode((grid_size, grid_size))
state = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if state == 1 and event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
screen.fill((238, 232, 170))
xi = int(round((x) * 1.0 / cell_size))
yi = int(round((y) * 1.0 / cell_size))
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:
chess_arr.append((xi, yi, flag))
if check_win(chess_arr, flag):
state = 2 if flag == 1 else 3
else:
flag = 2 if flag == 1 else 1
for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (x, 0 ),
(x, cell_size * (cell_num - 1)), 1)
for y in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (0, y),
(cell_size * (cell_num - 1), y), 1)
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, y * cell_size], 16, 16)
def get_one_dire_num(lx, ly, dx, dy, m):
tx = lx
ty = ly
s = 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 check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)]
for x, y, c in chess_arr:
if c == flag:
m[y][x] = 1
lx = chess_arr[-1][0]
ly = chess_arr[-1][1]
dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
[(-1, 1), (1, -1)]]
screen.fill((238, 232, 170))
if state != 1:
myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
win_text = "IS %s" % (‘black‘ if state == 2 else ‘white‘)
textImage = myfont.render(win_text, True, white)
screen.blit(textImage, (260, 320))
pygame.display.update()
import random
import pygame
from pygame.locals import MOUSEBUTTONUP
pygame.init()
cell_size = 40
cell_num = 15
grid_size = cell_size * (cell_num - 1)
screencaption = pygame.display.set_caption(‘五子棋‘)
screen = pygame.display.set_mode((grid_size, grid_size))
chess_arr = []
flag = 1
state = 1
def get_one_dire_num(lx, ly, dx, dy, m):
tx = lx
ty = ly
s = 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 check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)]
for x, y, c in chess_arr:
if c == flag:
m[y][x] = 1
lx = chess_arr[-1][0]
ly = chess_arr[-1][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 = get_one_dire_num(lx, ly, dx, dy, m)
dx, dy = dire2
num2 = get_one_dire_num(lx, ly, dx, dy, m)
if num1 + num2 + 1 >= 5: return True
return False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if state == 1 and event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
xi = int(round((x) * 1.0 / cell_size))
yi = int(round((y) * 1.0 / cell_size))
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:
chess_arr.append((xi, yi, flag))
if check_win(chess_arr, flag):
state = 2 if flag == 1 else 3
else:
flag = 2 if flag == 1 else 1
screen.fill((238, 232, 170))
for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (x, 0 ),
(x, cell_size * (cell_num - 1)), 1)
for y in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (0, y),
(cell_size * (cell_num - 1), y), 1)
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, y * cell_size], 16, 16)
if state != 1:
myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
win_text = "IS %s" % (‘black‘ if state == 2 else ‘white‘)
textImage = myfont.render(win_text, True, white)
screen.blit(textImage, (260, 320))
pygame.display.update()
本次的实验完全超脱了平时所学的内容,大部分过程都参考了《python:从入门到实践》一书中对pygame的讲解,对五子棋的具体实现则参考了cnblog上的文章,总体而言,
是目前编写过的最复杂的程序,虽然许多内容非自己的积累,但在这个过程中切实的提升了我的编程能力以及自学能力,对他人代码的揣摩也让我对程序设计有了更加全面的认识。
原文:https://www.cnblogs.com/w1137146/p/14858354.html