20223210 2022-2023-2 《Python程序设计》实验四报告
课程:《Python程序设计》 班级: 2232 姓名: 黄湛 学号:20223210 实验教师:王志强 实验日期:2023年5月20日 必修/选修: 公选课
(一)实验内容
python的综合应用 利用tkinter编写扫雷小游戏
(二)实验过程及结果
扫雷小游戏 懂得都懂
1.初始化设置
import random
from tkinter import *
import tkinter as tk
from tkinter.messagebox import *
from functools import partial
game = Tk()
game.title("扫雷")
nx = [-1,-1,-1,0,0,1,1,1]
ny = [-1,0,1,-1,1,-1,0,1]
color = ["White","Blue","Green","Red","Brown","Gold","Pink","Purple","Black"] #不同数字的颜色
game_map = [] #隐藏的地图 ‘X’表示雷
state_map = [] #地图的状态 0未被点击 1已点击 2被标记 3被质疑 4雷被引爆
game_size = "" #地图的尺寸 (窗口大小用
size = 25 #格子大小
length = 0 #长
widthh = 0 #宽
tot = 1e9 #雷的个数
2.主界面
def home():
clear()
length = widthh = 0
tot = 1e9
game.geometry("300x280") # 窗口大小 字母x
words = tk.Label(game, text = "欢迎来到扫雷游戏!\n请选择您的游戏难度", font="五号")
words.place(y=10, relx=0.2)
b = tk.Button(game, text= "初级 9x9", font="五号", command=partial(create, 9, 9, 10))
b.place(y=60, relx=0.3)
b = tk.Button(game, text= "中级 16x16", font="五号", command=partial(create, 16, 16, 40))
b.place(y=100, relx=0.3)
b = tk.Button(game, text= "高级 16x30", font="五号", command=partial(create, 30, 16, 99))
b.place(y=140, relx=0.3)
global v1,v2,v3 #自定义地图
v1 = tk.StringVar()
e = tk.Entry(game, textvariable = v1, font="五号", bg = "White", justify = tk.CENTER)
e.place(y=220, height = 30, width=55,relx = 0.2)
v2 = tk.StringVar()
e = tk.Entry(game, textvariable = v2, font="五号", bg="White", justify = tk.CENTER)
e.place(y=220, height = 30, width=55,relx = 0.4)
v3 = tk.StringVar()
e = tk.Entry(game, textvariable = v3, font="五号", bg="White", justify = tk.CENTER)
e.place(y=220, height = 30, width=55,relx = 0.6)
b = tk.Button(game, text = " 自定义 ",font = "五号", command=DIY)
b.place(y=180, relx = 0.3)
3.自定义地图的合法性判断和弹窗
def DIY():
a = v1.get()
b = v2.get()
c = v3.get()
if a.isdigit() == 0 or b.isdigit() == 0 or c.isdigit() == 0:
showinfo(parent=game, title="请重试", message="抱歉,输入数据不合法 (>﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏ |