首页 > 解决方案 > 为扫雷游戏创建用户界面

问题描述

我想在 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)

标签: python

解决方案


您可以使用 tkinter 创建类似的界面。

https://www.tutorialspoint.com/python/python_gui_programming.htm


推荐阅读