我想在python上对minesweeper进行编码,并创建一个典型的用户界面,就像您在游戏的Windows版本中会看到的那样。我目前有一种可以在控制台上播放的算法,但是不知道如何创建可以与之交互的界面。
当前状态下的程序显示一个9矩阵,后面隐藏着另一个矩阵,其中包含炸弹(以1s表示)和其他正方形(以0s表示)。我知道您可以将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)
答案 0 :(得分:0)
您可以使用tkinter创建类似的界面。
https://www.tutorialspoint.com/python/python_gui_programming.htm