猫史档案馆


【Python作品分享】扫雷(tk)【作品秀】

用户:B站小铭铭B站小铭铭查看:0 回复:1 评论:0 创建时间:2021-06-04T20:56:12


【作品展示】

center_image

 

【作品介绍】

一个简单的扫雷作品

用了tkinter模块

画面不大

但乐趣无穷!

 

【作品源代码】

#This is the whole program for dodge the 喵s.
#Some blank lines have been added to make the program easier to read.


import tkinter
import random

gameOver = False
score = 0
喵sToClear = 0


def play_喵dodger():
    create_喵field(喵field)
    window = tkinter.Tk()
    layout_window(window)
    window.mainloop()


喵field = []


def create_喵field(喵field):
    global 喵sToClear

    for row in range(0, 10):
        rowList = []
        for column in range(0, 10):
            if random.randint(1, 100) < 20:
                rowList.append(1)
            else:
                rowList.append(0)
                喵sToClear = 喵sToClear + 1
        喵field.append(rowList)

    #printfield(喵field)


def printfield(喵field):
    for rowList in 喵field:
        print(rowList)


def layout_window(window):
    for rowNumber, rowList in enumerate(喵field):
        for columnNumber, columnEntry in enumerate(rowList):
            if random.randint(1, 100) < 25:
                喵 = tkinter.Label(window, text="    ", bg="darkgreen")
            elif random.randint(1, 100) > 75:
                喵 = tkinter.Label(window, text="    ", bg="seagreen")
            else:
                喵 = tkinter.Label(window, text="    ", bg="green")
            喵.grid(row=rowNumber, column=columnNumber)
            喵.bind("", on_click)


def on_click(event):
    global score
    global gameOver
    global 喵sToClear
    喵 = event.widget
    row = int(喵.grid_info()["row"])
    column = int(喵.grid_info()["column"])
    currentText = 喵.cget("text")
    if gameOver == False:
        if 喵field[row][column] == 1:
            gameOver = True
            喵.config(bg="red")
            print("Game Over! You hit a 喵!")
            print("Your score was: ", score)
        elif currentText == "    ":
            喵.config(bg="brown")
            totalBombs = 0

            if row < 9:
                if 喵field[row+1][column] == 1:
                    totalBombs = totalBombs + 1

            if row > 0:
                if 喵field[row-1][column] == 1:
                    totalBombs = totalBombs + 1

            if column > 0:
                if 喵field[row][column-1] == 1:
                    totalBombs = totalBombs + 1

            if column < 9:
                if 喵field[row][column+1] == 1:
                    totalBombs = totalBombs + 1

            if row > 0 and column > 0:
                if 喵field[row-1][column-1] == 1:
                    totalBombs = totalBombs + 1

            if row < 9 and column > 0:
                if 喵field[row+1][column-1] == 1:
                    totalBombs = totalBombs + 1

            if row > 0 and column < 9:
                if 喵field[row-1][column+1] == 1:
                    totalBombs = totalBombs + 1

            if row < 9 and column < 9:
                if 喵field[row+1][column+1] == 1:
                    totalBombs = totalBombs + 1

            喵.config(text=" " + str(totalBombs) + " ")
            喵sToClear = 喵sToClear - 1
            score = score + 1
            if 喵sToClear == 0:
                gameOver = True
                print("Well done! You found all the safe 喵s!")
                print("Your score was:", score)


play_喵dodger()                

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
B站小铭铭B站小铭铭

英文版的哈!应该都看得懂吧

点赞0


评论