python - 为扫雷游戏创建用户界面
问题描述
我想在 python 上编写扫雷程序并创建一个典型的用户界面,就像你在游戏的 windows 版本中找到的那样。我目前有一个可以在控制台上播放的有效算法,但不知道如何创建一个可以与之交互的界面。
当前状态下的程序显示一个由 9 组成的矩阵,其后面隐藏着另一个矩阵,其中包含炸弹(用 1 表示)和其他方块(用 0 表示)。我知道您可以将 matplotlib 用于用户界面,但无法弄清楚如何在我的代码中正确使用它,您能帮忙吗?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mping
from os import getcwd,chdir
import random as rd
import sys #closes the algorithm
def gridcreation(a):
k=0 #for the ghost grid
j=0
z=0 #for the infinite iteration
choix=[]
sequence='true'
if a=='easy':
fangrid=np.array([[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]])
while k<=10:
fangrid[rd.randrange(0,9)][rd.randrange(0,9)]=1
k=k+1
# print(fangrid)
playergrid=np.array(([[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9], [9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9],[9,9,9,9,9,9,9,9,9]]))
print(playergrid)
while sequence=='true': #allows for infinite iteration
print ('What is the line number of the square you want to reveal ?') #to show a square
xu=input()
ab=int(xu)
print('What is the column number of the square you want to reveal ?')
yu=input()
ord=int(yu)
if fangrid[ab][ord]==1: #game over
print(fangrid)
print('BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM, you lost, would you like to play again ?')
rep=input()
if rep=='no':
sys.exit(0) #closes the progamm
#find a way to restart the entire game
if fangrid[ab][ord]==0 and ab!=0 and ab!=9 and ord!=9 and ord!=0: #find a way when the square is on the edge
j=j+fangrid[ab-1][ord-1]+fangrid[ab-1][ord]+fangrid[ab-1][ord+1]+fangrid[ab][ord+1]+fangrid[ab][ord-1]+fangrid[ab+1][ord-1]+fangrid[ab+1][ord]+fangrid[ab+1][ord+1]
if fangrid[ab][ord]==0 and ab==0 and ord!=0 and ord!=0:
j=j+fangrid[ab][ord+1]+fangrid[ab][ord-1]+fangrid[ab+1][ord]+fangrid[ab+1] [ord-1]+fangrid[ab+1][ord+1]
if fangrid[ab][ord]==0 and ab!=0 and ord!=9 and ab==9 and ord!=0:
j=j+fangrid[ab-1][ord-1]+fangrid[ab-1][ord]+fangrid[ab-1][ord+1]+fangrid[ab][ord+1]+fangrid[ab][ord-1]
if fangrid[ab][ord]==0 and ab!=0 and ord==0 and ab!=9 and ord!=9:
j=j+fangrid[ab-1][ord]+fangrid[ab-1][ord+1]+fangrid[ab][ord+1]+fangrid[ab+1][ord]+fangrid[ab+1][ord+1]
if fangrid[ab][ord]==0 and ab!=0 and ord!=0 and ab!=9 and ord==9:
j=j+fangrid[ab-1][ord-1]+fangrid[ab-1][ord]+fangrid[ab][ord-1]+fangrid[ab+1][ord-1]+fangrid[ab+1][ord]
if fangrid[ab][ord]==0 and ab==0 and ab!=9 and ord!=9 and ord==0:
j=j+fangrid[ab][ord+1]+fangrid[ab+1][ord]+fangrid[ab+1][ord+1]
if fangrid[ab][ord]==0 and ab==9 and ab!=0 and ord!=0 and ord==9:
j=j+fangrid[ab-1][ord-1]+fangrid[ab-1][ord]+fangrid[ab][ord-1]
if fangrid[ab][ord]==0 and ab==0 and ab!=9 and ord!=0 and ord==9:
j=j+fangrid[ab][ord-1]+fangrid[ab+1][ord-1]+fangrid[ab+1][ord]
if fangrid[ab][ord]==0 and ab!=0 and ab==9 and ord==0 and ord!=9:
j=j+fangrid[ab-1][ord]+fangrid[ab-1][ord+1]+fangrid[ab][ord+1]
if 9 not in playergrid: #to end the game if victory
print("GG wp, would you like to play again ?")
choix=input()
if choix=='yes':
sys.exit(0)
#so that the player can see the result
playergrid[ab][ord]=j
j=j-playergrid[ab][ord]
if 9 in playergrid:
sequence=sequence
if 9 not in playergrid:
sequence=sequence+'False'
print(playergrid)
解决方案
您可以使用 tkinter 创建类似的界面。
https://www.tutorialspoint.com/python/python_gui_programming.htm
推荐阅读
- android - 如何在 Android Studio 中从 Fragment 打开 Activity
- database - Ionic React 我的组件在使用 map() 首次查看时未呈现
- c++ - Memeber 变量作为 C++ 中的类变量是如何发生的?
- eleventy - 如何让我的 Eleventy 模板不编码 HTML 标签?
- css - CSS隐藏元素以防止换行
- ios - Connect to an already remembered SSID in iOS through Xamarin
- laravel - Laravel我无法在我的主页中显示图像
- sql - 如何在 WHERE 子句中引用列别名
- c# - 尝试使用 foreach 循环打印类型属性(C#)
- r - 匹配值时选择 R 中的前 n 个变量