20231319 2023-2024-2 《Python程序设计》实验4报告
@
- 20231319 2023-2024-2 《Python程序设计》实验4报告
- 一、实验内容
- 二、实验过程及结果
- 1. 初始化 Pygame 画布界面
- 2. 绘制棋盘
- 3. 获取鼠标信息并显示落子位置
- 4. 实现落子操作
- 5. 判断胜利条件
- 6.使用tkinter中的messagebox组件成功实现胜方显示。
- 7. 完整代码
- 三、 实验过程中遇到的问题和解决过程
- 四、课程感悟与体会
- 五、实验总结
20231319 2023-2024-2 《Python程序设计》实验4报告
课程:《Python程序设计》
班级: 2313
姓名: 邓传山
学号:20231319
实验教师:王志强
实验日期:2020年5月15日
必修/选修: 公选课
一、实验内容
使用pygame组件,实现双人五子棋游戏;
二、实验过程及结果
1. 初始化 Pygame 画布界面
首先,初始化 Pygame,并创建一个窗口界面。设置画布颜色为棕黄色。
import pygame
import sys
from pygame.locals import QUIT, KEYDOWN
# 初始化 Pygame
pygame.init()
# 创建窗口
screen = pygame.display.set_mode((670, 670))
screen_color = [238, 154, 73] # 设置画布颜色
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(screen_color) # 清屏
pygame.display.update() # 刷新显示
2. 绘制棋盘
在画布上绘制五子棋的棋盘,包含 15x15 的网格线和中心点。
line_color = [0, 0, 0] # 设置线条颜色
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(screen_color) # 清屏
for i in range(27, 670, 44):
# 画竖线
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 4)
else:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 2)
# 画横线
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 4)
else:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 2)
# 在棋盘中心画一个不透明的黑色小圆
pygame.draw.circle(screen, line_color, [27 + 44 * 7, 27 + 44 * 7], 8, 0)
pygame.display.update() # 刷新显示
3. 获取鼠标信息并显示落子位置
获取鼠标位置,确定可以落子的具体位置,并实时显示在界面上。
def find_pos(x, y):
for i in range(27, 670, 44):
for j in range(27, 670, 44):
if abs(x - i) < 22 and abs(y - j) < 22:
return i, j
return x, y
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(screen_color) # 清屏
for i in range(27, 670, 44):
# 画竖线
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 4)
else:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 2)
# 画横线
if i == 27或i == 670 - 27:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 4)
else:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 2)
# 在棋盘中心画一个小圆
pygame.draw.circle(screen, line_color, [27 + 44 * 7, 27 + 44 * 7], 8, 0)
# 获取鼠标坐标信息
x, y = pygame.mouse.get_pos()
x, y = find_pos(x, y)
pygame.draw.rect(screen, [0, 229, 238], [x - 22, y - 22, 44, 44], 2, 1)
pygame.display.update() # 刷新显示
4. 实现落子操作
实现棋子的落子功能,包含判断当前位置是否已经落子和记录已落子的位置。
def check_over_pos(x, y, over_pos):
for val in over_pos:
if val[0] == [x, y]:
return False
return True
over_pos = [] # 已经落子的位置
white_color = [255, 255, 255]
black_color = [0, 0, 0]
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(screen_color) # 清屏
for i in range(27, 670, 44):
# 画竖线
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 4)
else:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 2)
# 画横线
if i == 27或i == 670 - 27:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 4)
else:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 2)
# 在棋盘中心画一个小圆
pygame.draw.circle(screen, line_color, [27 + 44 * 7, 27 + 44 * 7], 8, 0)
for val in over_pos:
pygame.draw.circle(screen, val[1], val[0], 20, 0)
# 获取鼠标坐标信息
x, y = pygame.mouse.get_pos()
x, y = find_pos(x, y)
if check_over_pos(x, y, over_pos):
pygame.draw.rect(screen, [0, 229, 238], [x - 22, y - 22, 44, 44], 2, 1)
keys_pressed = pygame.mouse.get_pressed()
if keys_pressed[0]:
if check_over_pos(x, y, over_pos):
if len(over_pos) % 2 == 0:
over_pos.append([[x, y], black_color])
else:
over_pos.append([[x, y], white_color])
for val in over_pos:
pygame.draw.circle(screen, val[1], val[0], 20, 0)
pygame.display.update() # 刷新显示
5. 判断胜利条件
实现五子连心的胜利判断逻辑。
def check_win(board):
for color in [1, 2]:
for x in range(15):
for y in range(15):
if check_line(board, x, y, color, 1, 0) or \
check_line(board, x, y, color, 0, 1) or \
check_line(board, x, y, color, 1, 1) or \
check_line(board, x, y, color, 1, -1):
return color
return 0
def check_line(board, x, y, color, dx, dy):
count = 0
for i in range(5):
nx, ny = x + i * dx, y + i * dy
if 0 <= nx < 15 and 0 <= ny < 15 and board[nx][ny] == color:
count += 1
else:
break
return count == 5
6.使用tkinter中的messagebox组件成功实现胜方显示。
7. 完整代码
import pygame
import sys
from pygame.locals import QUIT, KEYDOWN
import numpy as np
import tkinter as tk
from tkinter import messagebox
# 初始化 Pygame
pygame.init()
# 初始化 Tkinter
root = tk.Tk()
root.withdraw() # 隐藏主窗口
# 获取对显示系统的访问,并创建一个窗口 screen
# 窗口大小为 670x670
screen = pygame.display.set_mode((670, 670))
screen_color = [238, 154, 73] # 设置画布颜色
line_color = [0, 0, 0] # 设置线条颜色
def check_win(board):
for color in [1, 2]:
for x in range(15):
for y in range(15):
if check_line(board, x, y, color, 1, 0) or \
check_line(board, x, y, color, 0, 1) or
\
check_line(board, x, y, color, 1, 1) or \
check_line(board, x, y, color, 1, -1):
return color
return 0
def check_line(board, x, y, color, dx, dy):
count = 0
for i in range(5):
nx, ny = x + i * dx, y + i * dy
if 0 <= nx < 15 and 0 <= ny < 15 and board[nx][ny] == color:
count += 1
else:
break
return count == 5
def find_pos(x, y):
for i in range(27, 670, 44):
for j in range(27, 670, 44):
if abs(x - i) < 22 and abs(y - j) < 22:
return i, j
return x, y
def check_over_pos(x, y, over_pos):
for val in over_pos:
if val[0] == [x, y]:
return False
return True
def draw_board(screen):
screen.fill(screen_color)
for i in range(27, 670, 44):
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 4)
else:
pygame.draw.line(screen, line_color, [i, 27], [i, 670 - 27], 2)
if i == 27 or i == 670 - 27:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 4)
else:
pygame.draw.line(screen, line_color, [27, i], [670 - 27, i], 2)
pygame.draw.circle(screen, line_color, [27 + 44 * 7, 27 + 44 * 7], 8, 0)
board = [[0]*15 for _ in range(15)]
over_pos = []
white_color = [255, 255, 255]
black_color = [0, 0, 0]
flag = False
tim = 0
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
pygame.quit()
sys.exit()
draw_board(screen)
for val in over_pos:
pygame.draw.circle(screen, val[1], val[0], 20, 0)
res = check_win(board)
if res != 0:
winner_color = "Black" if res == 1 else "White"
messagebox.showinfo("Game Over", f"{winner_color} wins!")
pygame.quit()
sys.exit()
x, y = pygame.mouse.get_pos()
x, y = find_pos(x, y)
if check_over_pos(x, y, over_pos):
pygame.draw.rect(screen, [0, 229, 238], [x - 22, y - 22, 44, 44], 2, 1)
keys_pressed = pygame.mouse.get_pressed()
if keys_pressed[0] and tim == 0:
flag = True
if check_over_pos(x, y, over_pos):
pos = ((x - 27) // 44, (y - 27) // 44)
if len(over_pos) % 2 == 0:
over_pos.append([[x, y], black_color])
board[pos[0]][pos[1]] = 1
else:
over_pos.append([[x, y], white_color])
board[pos[0]][pos[1]] = 2
if flag:
tim += 1
if tim % 50 == 0:
flag = False
tim = 0
pygame.display.update()
三、 实验过程中遇到的问题和解决过程
问题1:Tkinter消息框导入问题
具体描述:程序打包为 .exe 文件后报错
tkinter 没有 attribute 'messagebox'。问题1解决方案:在尝试多种导入方式后,正确导入
tkinter模块,并使用root.withdraw()隐藏主窗口。问题2:落子判断逻辑错误
具体描述:无法正确判断五子连心。
问题2解决方案:检查并改进
check_win和check_line函数,确保能够正确检测到所有可能的五子连线。...
四、课程感悟与体会
- 人生苦短,我用 Python
- 学习 Python 通过编写小型项目或解决实际问题,可以帮助我更好地理解 Python 的各种概念和用法,同时提升编程技能。
- 老师风趣幽默的授课方式非常吸引人,希望老师明年面对新生时,也能保持时不时抽查提问的风格。
五、实验总结
通过这次实验,我进一步熟悉了 Pygame 和 Tkinter 的使用,理解了如何在 Python 中实现一个简单的图形用户界面和交互逻辑。实现五子棋游戏不仅提高了我的编程能力,也增强了我解决问题的能力。整个过程中,我学到了很多宝贵的经验,对 Python 语言的认识也更加深入。
相关推荐
政府科技管理者如何提升科技成果转化项目进场交易的备案效率?.docx
政府科技管理者如何提升科技成果转化项目进场交易的备案效率?
山区道路边坡落石隐患检测数据集VOC+YOLO格式241张1类别.md
【重要提示】本页面仅为资源介绍,不含外部下载地址 亲爱的 CSDN 用户: 感谢你浏览本资源页面,在这里提前向大家说明相关情况,避免后续产生误解。本资源我设置的初衷是 0 积分免费分享文档介绍,希望可以无偿给到有需要的朋友。但 CSDN 平台存在自动调控机制,系统会根据资源下载热度、文件体积、平台规则等因素,自动修改资源所需下载积分,页面显示为 1 积分、2 积分或更高积分都属于平台自动调整结果,并非我手动修改设置,我没有权限自主锁定积分数值。 如果你打开页面后,发现下载所需积分不为 0,请务必谨慎考虑是否下载。一旦你支付积分完成下载,后续遇到资源内容和描述不符、文件损坏等问题,我无法承担对应的积分损失,也没有渠道为你办理积分退还。在此强烈建议,只有页面明确显示 0 积分的时候,再进行下载操作。 同时特别说明:这份资源介绍文案里,没有附带任何外部下载地址。资源本身是通过 CSDN 官方上传通道上传的附件,正常情况下直接点击页面自带的下载按钮即可获取文件。页面找不到外部下载链接属于正常情况,并非资源缺失。 如果因为平台积分显示异常,导致你被扣取积分,积分退款、积分异常相关问题,请直接联系 CSDN 官方客服咨询处理。平台规则由官方制定,我无法干预积分变动,望各位理解。
高空作业栏杆检测数据集VOC+YOLO格式230张3类别.md
【重要提示】本页面仅为资源介绍,不含外部下载地址 亲爱的 CSDN 用户: 感谢你浏览本资源页面,在这里提前向大家说明相关情况,避免后续产生误解。本资源我设置的初衷是 0 积分免费分享文档介绍,希望可以无偿给到有需要的朋友。但 CSDN 平台存在自动调控机制,系统会根据资源下载热度、文件体积、平台规则等因素,自动修改资源所需下载积分,页面显示为 1 积分、2 积分或更高积分都属于平台自动调整结果,并非我手动修改设置,我没有权限自主锁定积分数值。 如果你打开页面后,发现下载所需积分不为 0,请务必谨慎考虑是否下载。一旦你支付积分完成下载,后续遇到资源内容和描述不符、文件损坏等问题,我无法承担对应的积分损失,也没有渠道为你办理积分退还。在此强烈建议,只有页面明确显示 0 积分的时候,再进行下载操作。 同时特别说明:这份资源介绍文案里,没有附带任何外部下载地址。资源本身是通过 CSDN 官方上传通道上传的附件,正常情况下直接点击页面自带的下载按钮即可获取文件。页面找不到外部下载链接属于正常情况,并非资源缺失。 如果因为平台积分显示异常,导致你被扣取积分,积分退款、积分异常相关问题,请直接联系 CSDN 官方客服咨询处理。平台规则由官方制定,我无法干预积分变动,望各位理解。
ti-cgt-c2000-22.6.0.LTS-windows-x64-installer
ti-cgt-c2000-22.6.0.LTS-windows-x64-installer
光伏板太阳能板尘土灰尘脏污检测数据集VOC+YOLO格式1006张3类别.md
【重要提示】本页面仅为资源介绍,不含外部下载地址 亲爱的 CSDN 用户: 感谢你浏览本资源页面,在这里提前向大家说明相关情况,避免后续产生误解。本资源我设置的初衷是 0 积分免费分享文档介绍,希望可以无偿给到有需要的朋友。但 CSDN 平台存在自动调控机制,系统会根据资源下载热度、文件体积、平台规则等因素,自动修改资源所需下载积分,页面显示为 1 积分、2 积分或更高积分都属于平台自动调整结果,并非我手动修改设置,我没有权限自主锁定积分数值。 如果你打开页面后,发现下载所需积分不为 0,请务必谨慎考虑是否下载。一旦你支付积分完成下载,后续遇到资源内容和描述不符、文件损坏等问题,我无法承担对应的积分损失,也没有渠道为你办理积分退还。在此强烈建议,只有页面明确显示 0 积分的时候,再进行下载操作。 同时特别说明:这份资源介绍文案里,没有附带任何外部下载地址。资源本身是通过 CSDN 官方上传通道上传的附件,正常情况下直接点击页面自带的下载按钮即可获取文件。页面找不到外部下载链接属于正常情况,并非资源缺失。 如果因为平台积分显示异常,导致你被扣取积分,积分退款、积分异常相关问题,请直接联系 CSDN 官方客服咨询处理。平台规则由官方制定,我无法干预积分变动,望各位理解。



