Mình có viết chơi 1 trò dò bomb mà sai rồi, các cậu giúp mình với

import tkinter
import random

gameOver= False
score=0
squaresToClear=0

def play_bombodger():
create_bombfield(bombfield)
window=tkinter.Tk()
layout_window(window)
window.mainloop()

bombfield=[]
def create_bombfield(bombfield):
global squaresToClear
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)
squaresToClear= squaresToClear + 1
bombfield.append(rowList)

printfield(bombfield)

def printfield(bombfield):
for rowList in bombfield:
print(rowList)

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

def on_click(event):
global score
global gameOver
global squaresToClear
square = event.widget
row=int(square.gird_info()[“row”])
column=int(square.gird_info()[“column”])
currentText =square.cget(“text”)
if gameOver== False:
if bombfield[row][column]==1:
gameOver=True
square.config(bg=“red”)
print(“game over!!!bạn đã đạp nhầm bom.”)
print(“số điểm của bạn là”,score)
elif currentText == " “:
square.config(bg=“brown”)
totalBombs = 0
if row<9:
if bombfield[row+1][column]==1:
totalBombs=totalBombs+1
if row>0:
if bombfield[row-1][column]==1:
totalBombs=totalBombs+1
if column>0:
if bombfield[row][column-1]==1:
totalBombs=totalBombs +1
if column <9:
if bombfield[row][column+1]==1:
totalBombs=totalBombs +1
if row>0 and column>0:
if bombfield[row-1][column-1]==1:
totalBombs=totalBombs+1
if row<9 and column>0:
if bombfield[row+1][column-1]==1:
totalBombs=totalBomb+1
if row>0 and colum <9:
if bombfield[row-1][column+1]:
totalBombs=totalBombs+1
if row<9 and colum <9:
if bombfield[row+1][column+1]:
totalBombs=totalBombs+1
square.config(text=” "+ str(totalBombs)+ " ")
squasquaresToClear =squaresToClear-1
score = score + 1
if squaresToClear==0:
gameOver =True
print(“xin chúc mừng bạn đã chiến thắng”)
print(“tổng số điểm là:”,score)
play_bombodger()

Bạn hãy làm theo hướng dẫn sau để code dễ nhìn

1 Like

Thanks bạn để mình gửi lại

import tkinter
import random

gameOver= False 
score=0 
squaresToClear=0 

def play_bombodger():
    create_bombfield(bombfield) 
    window=tkinter.Tk() 
    layout_window(window) 
    window.mainloop() 

bombfield=[] 
def  create_bombfield(bombfield): 
    global squaresToClear 
    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)
                squaresToClear= squaresToClear + 1
        bombfield.append(rowList)

   # printfield(bombfield) 
def printfield(bombfield): 
    for rowList in bombfield: 
        print(rowList)

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

def on_click(event):
    global score
    global gameOver
    global squaresToClear
    square = event.widget
    row=int(square.gird_info()["row"])
    column=int(square.gird_info()["column"])
    currentText =square.cget("text")
    if gameOver== False:
        if bombfield[row][column]==1:
            gameOver=True
            square.config(bg="red")
            print("game over!!!bạn đã đạp nhầm bom.")
            print("số điểm của bạn là",score)
    elif currentText == "    ":
        square.config(bg="brown")
        totalBombs = 0
        if row<9:
            if bombfield[row+1][column]==1:
                totalBombs=totalBombs+1
        if row>0:
            if bombfield[row-1][column]==1:
                totalBombs=totalBombs+1
        if column>0:
            if bombfield[row][column-1]==1:
                totalBombs=totalBombs +1
        if column <9:
            if bombfield[row][column+1]==1:
                totalBombs=totalBombs +1
        if row>0 and column>0:
            if bombfield[row-1][column-1]==1:
                totalBombs=totalBombs+1
        if row<9 and column>0:
            if bombfield[row+1][column-1]==1:
                totalBombs=totalBomb+1
        if row>0 and colum <9:
            if bombfield[row-1][column+1]:
                totalBombs=totalBombs+1
        if row<9 and colum <9:
            if bombfield[row+1][column+1]:
                totalBombs=totalBombs+1
        square.config(text=" "+ str(totalBombs)+ " ")
        squasquaresToClear =squaresToClear-1
        score = score + 1
        if squaresToClear==0:
            gameOver =True
            print("xin chúc mừng bạn đã chiến thắng")
            print("tổng số điểm là:",score)
play_bombodger()


`

Lỗi của bạn hiển thị là như thế nào?