首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 3扫雷机tkinter游戏

Python 3扫雷机tkinter游戏
EN

Code Review用户
提问于 2019-02-17 12:59:20
回答 1查看 790关注 0票数 3

我刚刚完成了我的扫雷游戏使用tkinter,并想知道我如何可以改进我的程序。

代码语言:javascript
复制
from tkinter import *
from tkinter import messagebox
from random import randint

class setupwindow():
    def __init__(window): #window is the master object of the setup window
        window.root = Tk()
        window.root.title("Setup")
        window.root.grid()

        window.finish = "N"

        labels = ["Height:", "Width:", "Mines:"]
        window.label = ["","",""]
        window.entry = ["","",""]

        for i in range(3):
            window.label[i] = Label(text = labels[i])
            window.label[i].grid(row = i, column = 1)
            window.entry[i] = Entry()
            window.entry[i].grid(row = i, column = 2)

        window.startbutton = Button(text = "Start", command = lambda: setupwindow.onclick(window))
        window.startbutton.grid(column = 2)
        window.root.mainloop()

    def onclick(window):
        setupwindow.verification(window)
        if window.verf == "Y":
            window.finish = "Y"
            window.root.destroy()
            return window

    def verification(window):
        height = window.entry[0].get()
        width = window.entry[1].get()
        mines = window.entry[2].get()

        window.verf = "N"
        if height.isdigit() and width.isdigit() and mines.isdigit():
            height = int(height)
            width = int(width)
            mines = int(mines)

            if height > 0 and height <= 24:
                totalsquares = height * width

                if width > 0 and width <= 48:

                    if mines > 0:
                        if mines < totalsquares:
                            window.verf = "Y"
                            window.height = height
                            window.width = width
                            window.mines = mines

                        else:
                            messagebox.showerror("Invalid", "You cannot have more mines than squares!")
                    else:
                        messagebox.showerror("Invalid", "You can't play minesweeper without mines!")
                else:
                    messagebox.showerror("Invalid", "Width must be between 1 and 48 inclusive")
            else:
                messagebox.showerror("Invalid", "Height must be between 1 and 24 inclusive")
        else:
            messagebox.showerror("Invalid", "All values must be integers")


class gamewindow():
    def __init__(s, setup):  #s is the master object of the main game
        s.height = setup.height
        s.width = setup.width
        s.mines = setup.mines

        s.root = Tk()
        s.root.title("Minesweeper")
        s.root.grid()

        s.finish = "N"
        s.maingrid = list()
        for i in range(s.height):
            s.maingrid.append([])
            for x in range(s.width):
                s.maingrid[i].append(" ")
                s.maingrid[i][x] = Button(height = 0, width = 3, font = "Calibri 15 bold", text = "", bg = "gray90", command = lambda i=i, x=x: gamewindow.onclick(s, i, x))

                s.maingrid[i][x].bind("<Button-3>", lambda event="<Button-3>", i=i, x=x: gamewindow.rightclick(event, s, i, x))
                s.maingrid[i][x].grid(row = i, column = x)
                s.maingrid[i][x].mine = "False"

        totalsquares = s.height * s.width
        s.scoreneeded = totalsquares - s.mines
        s.score = 0

        indexlist = list()
        for i in range(totalsquares):
            indexlist.append(i)

        spaceschosen = list() #where the mines are going to be
        for i in range(s.mines):
            chosenspace = randint(0, len(indexlist) - 1)
            spaceschosen.append(indexlist[chosenspace])
            del indexlist[chosenspace]

        for i in range(len(spaceschosen)):
            xvalue = int(spaceschosen[i] % s.width)
            ivalue = int(spaceschosen[i] / s.width)

            s.maingrid[ivalue][xvalue].mine = "True"

        s.root.mainloop()


    def onclick(s, i, x):
        colourlist = ["PlaceHolder", "Blue", "Green", "Red", "Purple", "Black", "Maroon", "Gray", "Turquoise"]

        if s.maingrid[i][x]["text"] != "F" and s.maingrid[i][x]["relief"] != "sunken":
            if s.maingrid[i][x].mine == "False":
                s.score += 1

                combinationsi = [1, -1, 0, 0, 1, 1, -1, -1]
                combinationsx = [0, 0, 1, -1, 1, -1, 1, -1] #All the surrounding spaces

                minecount = 0
                for combinations in range(len(combinationsi)):
                    tempi = i + combinationsi[combinations]
                    tempx = x + combinationsx[combinations]

                    if tempi < s.height and tempx < s.width and tempi >= 0 and tempx >= 0:
                        if s.maingrid[tempi][tempx].mine == "True":
                            minecount = minecount + 1

                if minecount == 0:
                    minecount = ""

                s.maingrid[i][x].configure(text = minecount, relief = "sunken", bg = "gray85")

                if str(minecount).isdigit():
                    s.maingrid[i][x].configure(fg = colourlist[minecount])

                if minecount == "":
                    for z in range(len(combinationsi)):
                        if s.finish == "N":
                            ivalue = i + int(combinationsi[z])
                            xvalue = x + int(combinationsx[z])

                            if ivalue >= 0 and ivalue < s.height and xvalue >=0 and xvalue < s.width:
                                if s.maingrid[ivalue][xvalue]["relief"] != "sunken":
                                    gamewindow.onclick(s, ivalue, xvalue)

                if s.score == s.scoreneeded and s.finish == "N":
                    messagebox.showinfo("Congratulations", "A winner is you!")
                    s.finish = "Y"
                    s.root.destroy()


            else:
                s.maingrid[i][x].configure(bg = "Red", text = "*")
                for a in range(len(s.maingrid)):
                    for b in range(len(s.maingrid[a])):
                        if s.maingrid[a][b].mine == "True":
                            if s.maingrid[a][b]["text"] == "F":
                                s.maingrid[a][b].configure(bg = "Green")
                            elif s.maingrid[a][b]["bg"] != "Red":
                                s.maingrid[a][b].configure(bg = "Pink", text = "*")

                        elif s.maingrid[a][b]["text"] == "F":
                            s.maingrid[a][b].configure(bg = "Yellow")

                messagebox.showinfo("GAME OVER", "You have lost")
                s.root.destroy()

    def rightclick(event, s, i, x):
        if s.maingrid[i][x]["relief"] != "sunken":
            if s.maingrid[i][x]["text"] == "":
                s.maingrid[i][x].config(text = "F")
            elif s.maingrid[i][x]["text"] == "F":
                s.maingrid[i][x].config(text = "?")
            else:
                s.maingrid[i][x].config(text = "")


if __name__ == "__main__":
    setup = setupwindow()
    if setup.finish == "Y":
        game = gamewindow(setup)
    quit()
EN

回答 1

Code Review用户

回答已采纳

发布于 2019-02-17 14:14:11

  1. 佩普-8
    • 类名通常应该使用CapWords约定。#类名
    • 当用于指示关键字参数时,或用于指示未注释函数参数的默认值时,不要在=符号周围使用空格。#白空间
    • 始终在实例方法的第一个参数中使用self。#函数和方法-参数
    • 还有其他PEP-8违规事件。阅读完整文件

  2. 保护条款 什么是警卫条款以及如何使用它们?
  3. 在需要之前不要使用for i in range(len(list))。即使您需要它,也可以使用enumerate代替。

  4. range转换为list
代码语言:javascript
复制
        indexlist = list()
        for i in range(totalsquares):
            indexlist.append(i)

等于indexlist = list(range(totalsquares))

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/213648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档