python GUI编程实现扫雷游戏

作者:ZJF010101 时间:2023-10-27 20:41:29 

前言

1992年扫雷被加入到windows3.1,成为早期windows的经典游戏。近来接触python的GUI(图形化)编程,于是通过编写扫雷来实践学习。有关程序的问题和想法欢迎大家指出。

一、基本思路

(1)程序的核心数据是二维列表control_list[16][16],值-1代表雷,0和其他数字代表四周雷的数目。函数randomization()随机40个雷的 位置
(2)生成16x16个按钮控件,根据control_list列表确定点击相应的按钮时执行的代码。如按照游戏规则:点击对应control_list列表值为0的按钮时,递归执行函数re()来扫雷,当点击对应的control_list列表值为-1时直接结束游戏,对应大于0的值则只需隐藏当前的按钮控件。
其他:本程序用到的GUI编程库为tkinter, 控件的布局统一采用place()方法

二、源代码

1.运行效果

运行效果如下(示例):

python GUI编程实现扫雷游戏

2.上源码

"""
Created on Tuesday, April 5,2022
@author:I
"""
from tkinter import *
from tkinter import messagebox
import random

#定义五个二维数组,充当整个程序的数据
control_list=[[0 for i in range(16)] for j in range(16)]#二维列表,呈现雷和数字的分布。
show_list=[[0 for i in range(16)] for j in range(16)]#二维列表,控制遮住或显示雷和数字。(0--遮住,1--显示)
button_list=[[0 for i in range(16)] for j in range(16)]#二维的按钮列表(显示在上层)
label_list=[[0 for i in range(16)] for j in range(16)]#二维的标签列表(显示在下层)
mark_list=[[0 for i in range(16)] for j in range(16)]#二维标记列表
num_mine=40#控制游戏结束
counter=0#计时
T ,t= 1,0#游戏结束的判断
def randomization(c_list):#随机初始化雷的分布即初始化列表control_list
? ? num=0
? ? while num<40:
? ? ? ? x=random.randint(0,15)
? ? ? ? y=random.randint(0,15)
? ? ? ? if(c_list[x][y]==0):
? ? ? ? ? ? num+=1
? ? ? ? ? ? c_list[x][y]=-1
? ? for i in range(16):
? ? ? ? for j in range(16):
? ? ? ? ? ? if(c_list[i][j]>-1):
? ? ? ? ? ? ? ? if (i>0 and c_list[i-1][j]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (i<15 and c_list[i+1][j]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (j>0 and c_list[i][j-1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (j<15 and c_list[i][j+1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (i>0 and j>0 and c_list[i-1][j-1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (i<15 and j<15 and c_list[i+1][j+1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (i>0 and j<15 and c_list[i-1][j+1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
? ? ? ? ? ? ? ? if (i<15 and j>0 and c_list[i+1][j-1]==-1):
? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1
def game_core():
? ? randomization(control_list)
? ? for row in range(16):
? ? ? ? for col in range(16):
? ? ? ? ? ? if(control_list[row][col]==-1):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="?",font=('arial', 15, 'bold'),fg="black",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==0):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==1):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="1",font=('arial', 15, 'bold'),fg="red",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==2):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="2",font=('arial', 15, 'bold'),fg="blue",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==3):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="3",font=('arial', 15, 'bold'),fg="green",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==4):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="4",font=('arial', 15, 'bold'),fg="white",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==5):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="5",font=('arial', 15, 'bold'),fg="red",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==6):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="6",font=('arial', 15, 'bold'),fg="blue",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==7):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="7",font=('arial', 15, 'bold'),fg="green",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? ? ? ? ? elif(control_list[row][col]==8):
? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="8",font=('arial', 15, 'bold'),fg="white",bg="#AAAAAA",relief=RIDGE)
? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20)
? ? for r in range(16):
? ? ? ? for c in range(16):
? ? ? ? ? ? s = str((r)*16+c)
? ? ? ? ? ? button_list[r][c]=Button(root,text=s,activeforeground="#AAAAAA",bg="#AAAAAA",fg="#AAAAAA")
? ? ? ? ? ? button_list[r][c].place(x=17+c*20,y=46+r*20,height=20,width=20)
? ? ? ? ? ? button_list[r][c].bind("<Button-1>",button_control_l)#鼠标左击绑定函数
? ? ? ? ? ? button_list[r][c].bind("<Button-3>",button_control_r)
def button_control_l(event):#扫雷控制函数.(开始函数直接用参数r和c,但是会产生问题)
? ? r = int(event.widget["text"])//16
? ? c = int(event.widget["text"])%16
? ? global t
? ? global T
? ? if(control_list[r][c]>=1):
? ? ? ? button_list[r][c].place_forget()
? ? ? ? show_list[r][c]=1
? ? ? ? t+=1
? ? elif(control_list[r][c]==0):
? ? ? ? rec(r,c)
? ? elif(control_list[r][c]==-1 and T):
? ? ? ? button_list[r][c].place_forget()
? ? ? ? show_list[r][c]=1
? ? ? ? T=0
? ? ? ? for i in range(16):
? ? ? ? ? ? for j in range(16):
? ? ? ? ? ? ? ? if(control_list[i][j]==-1):
? ? ? ? ? ? ? ? ? ? button_list[i][j].place_forget()
? ? ? ? ? ? ? ? ? ? show_list[r][c]=1
? ? ? ? button_restart["text"]="?"
? ? ? ? messagebox.showwarning("失败","你已经被炸死了!")
? ? if t==216:
? ? ? ? ? ? T=0
? ? ? ? ? ? messagebox.showwarning("成功","恭喜你扫雷完成!")
def button_control_r(event):
? ? r = int(event.widget["text"])//16
? ? c = int(event.widget["text"])%16
? ? mark_list[r][c]=Button(root,text="?",font=('楷体', 14),activeforeground="#AAAAAA",bg="#AAAAAA",fg="yellow")
? ? mark_list[r][c].place(x=17+c*20,y=46+r*20,height=20,width=20)
? ? mark_list[r][c].bind("<Button-3>",button_control_r_change)
def button_control_r_change(event):
? ? global num_mine
? ? if (event.widget["text"]=="?" and num_mine>0):
? ? ? ? num_mine-=1
? ? ? ? event.widget["text"]="▲"
? ? ? ? cout_label["text"]=str(num_mine)
? ? elif(event.widget["text"]=="▲"):
? ? ? ? num_mine+=1
? ? ? ? cout_label["text"]=str(num_mine)
? ? ? ? event.widget.place_forget()
? ? elif (event.widget["text"]=="?" and num_mine==0):
? ? ? ? event.widget.place_forget()
def rec(r,c):#递归探测
? ? global t
? ? if control_list[r][c]>0 and show_list[r][c]==0:
? ? ? ? button_list[r][c].place_forget()
? ? ? ? show_list[r][c]=1
? ? ? ? t+=1
? ? ? ? return 0
? ? elif control_list[r][c] ==0 and show_list[r][c]==0:
? ? ? ? button_list[r][c].place_forget()
? ? ? ? show_list[r][c]=1
? ? ? ? t+=1
? ? ? ? if r>0 and c>0:
? ? ? ? ? ? rec(r-1,c-1)
? ? ? ? if r>0:
? ? ? ? ? ? rec(r-1,c)
? ? ? ? if r>0 and c<15:
? ? ? ? ? ? rec(r-1,c+1)
? ? ? ? if c<15:
? ? ? ? ? ? rec(r,c+1)
? ? ? ? if r<15 and c<15:
? ? ? ? ? ? rec(r+1,c+1)
? ? ? ? if r<15:
? ? ? ? ? ? rec(r+1,c)
? ? ? ? if r<15 and c>0:
? ? ? ? ? ? rec(r+1,c-1)
? ? ? ? if c>0:
? ? ? ? ? ? rec(r,c-1)
def time_counter(la): ?# la是标签,计时函数
? ? def counting():
? ? ? ? global counter
? ? ? ? if T:
? ? ? ? ? ? counter += 1
? ? ? ? la["text"]=str(counter)
? ? ? ? la.after(1000,counting) ?# 在1000毫秒后执行counting()函数,即循环执行counting
? ? counting()
def restart():#重新开始函数
? ? button_restart["text"]="?"
? ? cout_label["text"]="40"
? ? #数据重置
? ? for i in range(16):
? ? ? ? for j in range(16):
? ? ? ? ? ? control_list[i][j]=0
? ? ? ? ? ? show_list[i][j]=0
? ? ? ? ? ? button_list[i][j].place_forget()
? ? ? ? ? ? button_list[i][j]=0
? ? ? ? ? ? label_list[i][j].place_forget()
? ? ? ? ? ? label_list[i][j]=0
? ? ? ? ? ? if (mark_list[i][j]!=0):
? ? ? ? ? ? ? ? mark_list[i][j].place_forget()
? ? ? ? ? ? mark_list[i][j]=0
? ? global num_mine
? ? global counter
? ? global T ,t
? ? num_mine=40
? ? counter=0
? ? T ,t= 1,0
? ? game_core()
if __name__ =="__main__":
? ??
? ? root = Tk()#根窗体
? ? root.title("扫雷小游戏")
? ? root.geometry("360x410")#根窗体大小
? ? cv1 = Canvas(root,bd=15,bg="#FFFFFF",relief=RIDGE,cursor="cross",width=321,height=350)
? ? cv1.create_line(15,45,337,45)
? ? cv1.place(x=0,y=0)
? ? w=Label(root,text="你所作的选择,决定你的命运!",font=("楷体",12))
? ? w.place(x=60,y=385)
? ? button_restart=Button(root,text="?",font=('楷体', 15),bg="#AAAAAA",fg="yellow",command=restart)
? ? button_restart.place(x=150,y=17,height=27,width=27)
? ? time_label = Label(root,bg="black",fg="red",text=str(counter),font=("LcdD",15))#计时标签
? ? time_label.place(x=285,y=17,height=27,width=50)
? ? cout_label = Label(root,bg="black",fg="red",text="40",font=("LcdD",20))#计数标签
? ? cout_label.place(x=18,y=17,height=27,width=27)
? ? game_core()
? ? time_counter(time_label)
? ? root.mainloop()#监控组件,组件发生变化或触发事件时,更新窗口

来源:https://blog.csdn.net/ZJF010101/article/details/124076084

标签:python,GUI,扫雷
0
投稿

猜你喜欢

  • PHP中类静态调用和范围解析操作符的区别解析

    2023-11-15 07:38:39
  • Python+OpenCV人脸检测原理及示例详解

    2021-07-31 19:31:51
  • 如何将python代码生成API接口

    2022-10-27 14:37:39
  • Windows10下 python3.7 安装 facenet的教程

    2022-06-21 11:17:01
  • python pytorch图像识别基础介绍

    2021-02-02 03:13:13
  • python之DataFrame实现excel合并单元格

    2021-02-22 04:45:00
  • JavaScript,5种调用函数的方法[译]

    2009-02-24 16:26:00
  • python实现生成Word、docx文件的方法分析

    2021-03-01 07:49:53
  • Python的函数使用介绍

    2022-07-02 12:05:42
  • Python3爬虫学习之将爬取的信息保存到本地的方法详解

    2023-01-07 14:20:26
  • MySQL鲜为人知的几个特殊技巧

    2009-03-09 13:22:00
  • Python中的True,False条件判断实例分析

    2023-09-30 07:34:24
  • GOLang单元测试用法详解

    2024-05-05 09:27:33
  • 详解Mysql中的JSON系列操作函数

    2024-01-20 02:08:08
  • MYSQL的主从复制知识点整理

    2024-01-26 02:54:29
  • asp 正则实现清除html文本格式的函数代码

    2011-03-09 11:21:00
  • 用Python实现给Word文档盖章

    2021-07-08 21:18:00
  • Python开发桌面小程序功能

    2023-07-01 14:46:59
  • python 中的@property的用法详解

    2023-03-03 16:57:57
  • python并发爬虫实用工具tomorrow实用解析

    2023-03-18 02:29:07
  • asp之家 网络编程 m.aspxhome.com