I made a minesweeper game with tkinter - python

I'm a total beginner who just started learning to code with the book "Head First Learn to Code". I've just finished the book and coded a minesweeper game with python3 on my mac. I hope to get some advice on the game I made. If you feel like the code is too long to read, here are some specific questions I'd like to ask:
What does this: flag = flag.resize((12, 12), Image.ANTIALIAS) actually do, why do I need to assign it a variable instead of just doing this: flag.resize((12, 12), Image.ANTIALIAS) as I'd do with other objects
Is there a better way to create a stopwatch? Mine is the function update_time.
Did I make any mistake regarding the conventions of python? Please point out some for me.
Any help would be much appreciated!
Here's my code:
from tkinter import *
import random as rd
from tkinter import messagebox
from PIL import ImageTk, Image
import sys
class GameGrid(Frame): #the game
def __init__(self, master, height, width, mines_count, player):
Frame.__init__(self, master)
self.grid(row=0)
self.master = master
if sys.platform == 'win32': #checking os
self.platform = 'windows'
else:
self.platform = 'macos'
self.height = height #storing height, width, mines_count, and player's name
self.width = width
self.mines_count = mines_count
self.player_name = player
self.play_time = 0 #initiating play_time and other values
self.lost = False
self.won = False
self.notmine = height * width - mines_count #calculate the number of tiles that are not mines
flag = Image.open('flag.png') #creating and storing flag and bomb images
flag = flag.resize((12, 12), Image.ANTIALIAS)
bomb = Image.open('bomb.png')
bomb = bomb.resize((12, 12), Image.ANTIALIAS)
self.flag = ImageTk.PhotoImage(flag)
self.bomb = ImageTk.PhotoImage(bomb)
grid_model = [[0]*width for item in [0]*height] #creating a list to hold 1's and 0's
while mines_count > 0: #1 is mine, 0 is normal
randi = rd.randint(0, height-1) #putting mines into the list by generating random coordinates
randj = rd.randint(0, width-1) #and storing mine in the corresponding place
if grid_model[randi][randj] == 0:
grid_model[randi][randj] = 1
mines_count -= 1
self.tiles = {} #creating Tiles and storing them using dictionary
for i in range(height):
for j in range(width):
if grid_model[i][j] == 1:
self.tiles[i, j] = Tile(self, i, j, True)
else:
mine_neighbors = 0 #counting nearby mines if Tile in creation is not a mine
if i - 1 >= 0:
if grid_model[i-1][j] == 1:
mine_neighbors += 1
if i - 1 >= 0 and j - 1 >= 0:
if grid_model[i-1][j-1] == 1:
mine_neighbors += 1
if i - 1 >= 0 and j + 1 < width:
if grid_model[i-1][j+1] == 1:
mine_neighbors += 1
if j - 1 >= 0:
if grid_model[i][j-1] == 1:
mine_neighbors += 1
if j + 1 < width:
if grid_model[i][j+1] == 1:
mine_neighbors += 1
if i + 1 < height:
if grid_model[i+1][j] == 1:
mine_neighbors += 1
if i + 1 < height and j - 1 >= 0:
if grid_model[i+1][j-1] == 1:
mine_neighbors += 1
if i + 1 < height and j + 1 < width:
if grid_model[i+1][j+1] == 1:
mine_neighbors += 1
self.tiles[i, j] = Tile(self, i, j, False, mine_neighbors)
def reveal_surroundings(self, i, j): #reveal nearby tiles
revealing = []
width = self.width
height = self.height
if i - 1 >= 0:
revealing.append(self.tiles[i-1, j])
if i - 1 >= 0 and j - 1 >= 0:
revealing.append(self.tiles[i-1, j-1])
if i - 1 >= 0 and j + 1 < width:
revealing.append(self.tiles[i-1, j+1])
if j - 1 >= 0:
revealing.append(self.tiles[i, j-1])
if j + 1 < width:
revealing.append(self.tiles[i, j+1])
if i + 1 < height:
revealing.append(self.tiles[i+1, j])
if i + 1 < height and j - 1 >= 0:
revealing.append(self.tiles[i+1, j-1])
if i + 1 < height and j + 1 < width:
revealing.append(self.tiles[i+1, j+1])
for tile in revealing:
tile.reveal()
def lose(self): #show if lost, stop the clock
global stp
stp = True
self.lost = True
if self.platform == 'windows':
for tile in self.tiles:
if self.tiles[tile].mine:
self.tiles[tile].config(bg='red')
else:
for tile in self.tiles:
if self.tiles[tile].mine:
self.tiles[tile].config(image=self.bomb, padx=9, pady=4, bg='red')
self.tiles[tile].unbind('<Button-1>')
self.tiles[tile].unbind('<Button-2>')
messagebox.showerror(message='Boom, Game Over!!')
self.score = ScoreBoard(self.master)
def win(self): #show if won, stop the clock, creating a window recording scores
global mn, sc, stp
stp = True
self.won = True
for tile in self.tiles:
if self.tiles[tile].mine:
self.tiles[tile].config(image=self.bomb, padx=9, pady=4, bg='red')
self.tiles[tile].unbind('<Button-1>')
self.tiles[tile].unbind('<Button-2>')
messagebox.showinfo(message='Congrats, You Survived ;)')
play_time = str(m) + ' mins, ' + str(s) + ' secs'
self.score = ScoreBoard(self.master, self.player_name, play_time)
class ScoreBoard(Toplevel): #for score recording
def __init__(self, master, name=None, time=None):
Toplevel.__init__(self, master)
self.title('Hall of Fame')
fin_text = ''
if name != None: #writing in the record if there is one
self.board = open('ScoreBoard.txt', 'r') #assigning the text inside ScoreBoard.txt to board_text
board_text = '' #and writing it into ScoreBoard.txt
for line in self.board:
board_text = board_text + line
self.board = open('ScoreBoard.txt', 'w')
self.record = name + ' ' + time
self.board.write(board_text + '\n' + self.record)
self.board = open('ScoreBoard.txt', 'r') #reading text in ScoreBoard and put it on the window
for line in self.board:
fin_text = fin_text + line
self.lbl = Label(self, text=fin_text)
self.lbl.pack()
self.geometry('300x300')
self.board.close()
class Tile(Label): #the Tile
def __init__(self, master, i, j, mine, mine_neighbors=None):
Label.__init__(self, master, width=2, relief=RAISED)
self.grid(row=i, column=j)
self.game = master #storing row, column, is mine or not, count of nearby mines
self.mine = mine
self.row = i
self.col = j
self.mine_neighbors = mine_neighbors
self.revealed = False
self.marked = False
self.bind('<Button-1>', self.reveal) #bind Tile: reveal(left click), mark(right click)
self.bind('<Button-2>', self.mark)
def reveal(self, event=None): #revealing tile
if self.mine:
self.game.lose()
return
else:
if not self.revealed:
self.revealed = True
self.mark()
self.unbind('<Button-1>')
self.unbind('<Button-2>')
if self.mine_neighbors == 0: #if no nearby mines, reveal nearby tiles
self.config(text='', relief=SUNKEN, bg='lightgrey', image='', padx=1, pady=1)
self.game.reveal_surroundings(self.row, self.col)
else:
self.config(text=self.mine_neighbors, relief=SUNKEN, bg='lightgrey', image='', padx=1, pady=1)
self.game.notmine -= 1
if self.game.notmine == 0:
self.game.win()
def mark(self,event=None): #marking tile
if self.game.platform == 'windows':
if not self.marked:
self.config(text='*')
self.marked = True
else:
self.config(text='')
self.marked = False
else:
if not self.marked:
self.config(image=self.game.flag, padx=9, pady=4)
self.marked = True
else:
self.config(image='', padx=1, pady=1)
self.marked = False
stp = False #used to stop the clock when lost or won
def update_time(): #a stopwatch
global m, s, timer, stp
if stp != True:
s = s + 1
if s == 60:
m = m + 1
s = 0
mn = str(m) #making the clock look better by adding a 0 when the number
sc = str(s) #of second or minute is just one digit, e.g. 01, 06, 09..
if len(sc) == 1 and len(mn) == 1:
sc = '0' + sc
mn = '0' + mn
timer.config(text=mn+':'+sc)
elif len(mn) == 1 and len(sc) != 1:
mn = '0' + str(m)
timer.config(text=mn+':'+str(s))
elif len(sc) == 1 and len(mn) != 1:
sc = '0' + sc
timer.config(text=mn+':'+sc)
timer.after(1000, update_time)
def play(height, width, mines_count, player): #initiating the game
global s, m, timer
m = 0
s = -1
time = str(m) + ':' + str(s)
root = Tk()
root.title('MineSweeper')
root.resizable(False, False)
timer = Label(root, text='%i:%i'%(m,s)) #creating stopwatch and update it every second
timer.grid(row=1)
update_time()
game = GameGrid(root, height, width, mines_count, player)
root.mainloop()
if __name__ == '__main__':
play(10, 10, 10, 'Harley')

Related

How to change a square's color with tkinter

Doing a minesweeper on python using tkinter. I'm trying to make a square white when you click on it but it only makes the first square white.
import random
import tkinter as tk
import time
def init_grille(m, n, n_mines): # Creates the grid with the mines and the numbers
grille = []
for i in range(m) :
ligne = []
for i in range(n) :
ligne.append(0)
grille.append(ligne)
for i in range(n_mines) :
rm = random.randint(0, m - 1)
rn = random.randint(0, n - 1)
while grille[rm][rn] == 'X' :
rm = random.randint(0, m - 1)
rn = random.randint(0, n - 1)
grille[rm][rn] = 'X'
for em in range(m) :
for en in range(n) :
if grille[em][en] == 0 :
m_proches = 0
if en > 0 and grille[em][en-1] == 'X' :
m_proches += 1
if en < n - 1 and grille[em][en+1] == 'X' :
m_proches += 1
if em < m - 1 and grille[em+1][en] == 'X' :
m_proches += 1
if em < m - 1 and en < n - 1 and grille[em+1][en+1] == 'X' :
m_proches += 1
if em < m - 1 and en > 0 and grille[em+1][en-1] == 'X' :
m_proches += 1
if em > 0 and grille[em-1][en] == 'X' :
m_proches += 1
if em > 0 and en < n - 1 and grille[em-1][en+1] == 'X' :
m_proches += 1
if em > 0 and en > 0 and grille[em-1][en-1] == 'X' :
m_proches += 1
grille[em][en] = m_proches
return grille
def init_grille_joueur(m, n): # Creates the grid the player interacts with (digging and flagging)
grille_j = []
for i in range(m) :
ligne = []
for i in range(n) :
ligne.append(0)
grille_j.append(ligne)
return grille_j
root = tk.Tk()
M = 10
N = 10
LARGEUR_CASE = 50
HAUTEUR_CASE = 50 # Just some values used for testing the program
def fenetre_principale(): # Configurates the window
label = tk.Label(root, text = "GO !", background = "white")
label.pack()
root.title("Démineur")
root.geometry(str(M * LARGEUR_CASE) + 'x' + str(N * HAUTEUR_CASE + 90))
def init_canvas(m, n): # Creates the visual grid
rect_id = []
canvas = tk.Canvas(root, width = M * LARGEUR_CASE + 25, height = N * HAUTEUR_CASE + 25)
for i in range(m):
for j in range(n):
rect_id.append(canvas.create_rectangle(j * LARGEUR_CASE, i * HAUTEUR_CASE, (j + 1) * LARGEUR_CASE, (i + 1) * HAUTEUR_CASE, outline = "lightgray", fill = "gray", width = 2))
canvas.bind('<Button-1>', clic_gauche)
canvas.bind('<Button-3>', clic_droit)
canvas.pack()
return rect_id, canvas
def get_case(x, y): # Locates a square on the visual grid
case = canvas.find_overlapping(x, y, x, y)
if len(case) == 0 :
return -1, -1
else :
x = (case[0] - 1) // M
y = (case[0] - 1) % N
return x, y
def creuser(i, j): # The digging mechanic
if not grille[i][j] == -1 :
grille_j[i][j] = -1
C = (j * LARGEUR_CASE + LARGEUR_CASE / 2, i * HAUTEUR_CASE + HAUTEUR_CASE / 2)
case_id = canvas.find_overlapping(j, i, j, i)
case = case_id[0]
canvas.itemconfig(case, fill = "white")
if grille[i][j] == 'X' :
print("Perdu!")
canvas.create_text(C, font = "Wingdings 20 bold", text = chr(77), fill = "purple")
elif grille[i][j] == 1 :
canvas.create_text(C, font = "Arial 20 bold", text = str(grille[i][j]), fill = "blue")
elif grille[i][j] == 2 :
canvas.create_text(C, font = "Arial 20 bold", text = str(grille[i][j]), fill = "green")
elif grille[i][j] > 2 :
canvas.create_text(C, font = "Arial 20 bold", text = str(grille[i][j]), fill = "red")
canvas.pack()
def clic_gauche(event): # Left click
x, y = get_case(event.x, event.y)
creuser(x, y)
def drapeau(i, j): # The flag mechanic, unfinished
C = (j * LARGEUR_CASE + LARGEUR_CASE / 2, i * HAUTEUR_CASE + HAUTEUR_CASE / 2)
if grille_j[i][j] == 0 :
grille_j[i][j] = 1
canvas.create_text(C, font = "Wingdings 20 bold", text = chr(80), fill = "orange")
elif grille_j[i][j] == 1 :
grille_j[i][j] = 0
canvas.delete()
def clic_droit(event): # Right click
x, y = get_case(event.x, event.y)
drapeau(x, y)
'''def maj_labels():''' # Not finished yet
grille = init_grille(M, N, 5)
grille_j = init_grille_joueur(M, N)
fenetre_principale()
rect_id, canvas = init_canvas(M, N)
'''root.after(500, maj_labels)''' # It's supposed to update the timer which isn't implemented yet
root.mainloop()
I tried using find_overlapping to get the square's id when I click on it and then change its color with itemconfig, I tried to change the lines' order to see if they contradicted eachother but it's still the same problem
You passed incorrect region (j, i, j, i) to canvas.find_overlapping() inside creuser(), region (*C, *C) should be used instead:
def creuser(i, j):
...
#case_id = canvas.find_overlapping(j, i, j, i) # incorrect region
case_id = canvas.find_overlapping(*C, *C) # correct region
...

Attributes not inherited via tkinter.button

I am writing the number of step-by-step participants within the framework of the classical one. In normal code in def move function I try to reset attributes from one button to unexpected but when I use get_info I get nothing. I've been sitting for 5 hours, I can not figure out how to fix it.
I tried to remake all classes into 1, but even after that it didn't inherit the attributes I needed.
import random
import tkinter as tk
from PIL import Image, ImageTk
import threading
class Color(object):
EMPTY = 0
BLACK = 1
WHITE = 2
class Move:
xstart = -1
ystart = 0
xdest = 0
ydest = 0
class Button(tk.Button, object):
IMG = None
PlayStatus = None
Getstatus = 0
MovementPointsStart = 0
MovementPoints = 0
Heatl = 0
def __init__(self, color, master, x, y, *args, **kwargs):
self.color = color
super(Button, self).__init__(master, width=6, height=3, *args, **kwargs)
self.x = x
self.y = y
self.color = color
def __str__(self):
return f'Юнит {self.x} {self.y} {self.PlayStatus}'
def __repr__(self):
return f'Юнит {self.x} {self.y} {self.PlayStatus}'
def nextturn(self):
if self.MovementPointsStart > self.MovementPoints:
print(self.MovementPointsStart)
self.MovementPoints = self.MovementPointsStart
print('sosnul')
else:
pass
def get_info(self, Button, x, y):
info = 0
if self.color == Color.BLACK:
info = (type(self), self.PlayStatus, self.MovementPointsStart, str('/'), self.MovementPoints, 'Black')
else:
info = (type(self), self.PlayStatus, self.MovementPointsStart, str('/'), self.MovementPoints,'White')
return info
def get_moves(self, Button, turn):
print(turn)
i = 1
while i==1:
print('zestko')
if turn % 2 == 0 and self.color == Color.WHITE:
print('sosnul')
if self.MovementPoints > 0:
print('huiza')
if Button.get_color(Move.xstart, Move.ystart) == Color.WHITE or Button.get_color(Move.xdest, Move.ydest) == Color.EMPTY:
if Move.xdest < Move.xstart:
xend = Move.xstart - Move.xdest
else:
xend = Move.xdest - Move.xstart
if Move.ydest < Move.ystart:
yend = Move.ystart - Move.ydest
else:
yend = Move.ydest - Move.ystart
self.MovementPoints = self.MovementPoints - (xend + yend)
break
break
else:
print("У вас нет очков перемещения")
break
else:
if turn % 2 == 0:
print("Сейчас не ваш ход")
if turn % 2 == 1 and self.color == Color.BLACK:
print('sosnul')
if self.MovementPoints > 0:
print('huiza')
if Button.get_color(Move.xstart, Move.ystart) == Color.BLACK or Button.get_color(Move.xdest, Move.ydest) == Color.EMPTY:
if Move.xdest < Move.xstart:
xend = Move.xstart - Move.xdest
else:
xend = Move.xdest - Move.xstart
if Move.ydest < Move.ystart:
yend = Move.ystart - Move.ydest
else:
yend = Move.ydest - Move.ystart
self.MovementPoints = self.MovementPoints - (xend + yend)
break
else:
print("У вас нет очков перемещения")
break
else:
print("Сейчас не ваш ход")
break
class Game:
turn = 0
ROW = 10
COLUMNS = 10
win = tk.Tk()
win.title('Eve Offline')
win.geometry("900x560")
x=0
y=0
def __init__(self):
self.Button = []
for x in range(Game.ROW):
temp = []
for y in range(Game.COLUMNS):
btn = Button(Color.EMPTY, Game.win, bg='green', x=x, y=y, text='bebra')
temp.append(btn)
self.Button.append(temp)
def set_Button(self):
self.Button[1][2] = Button(Color.BLACK, Game.win, x=1, y=2, text='Suck')
self.Button[1][2].PlayStatus = 1
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
def create_window(self):
for x in range(Game.ROW):
for y in range(Game.COLUMNS):
btn = self.Button[x][y]
btn.grid(row=x, column=y)
Muvprikaz = tk.Button(bg='green', width=20, height=4, text='MOVE', activebackground='blue')
Muvprikaz.config(command=lambda button=self.Muvprikaz: self.Muvprikaz())
Muvprikaz.place(x=550, y=300)
NextturnB = tk.Button(bg='gray', width=20, height=4, text='Next Turn')
NextturnB.config(command=lambda button=self.nextturn(Game.turn): self.nextturn(button))
NextturnB.place(x=700, y=300)
def start(self):
self.set_Button()
self.create_window()
self.print_button()
self.win.mainloop()
def print_button(self):
for row_btn in self.Button:
print(row_btn)
def get_color(self, x, y):
return self.Button[y][x].color
def get_info(self, x, y):
return self.Button[y][x].get_info(self, x, y)
def move(self):
print(Move.xdest, Move.ydest)
print('sukablat')
print([Move.xdest],[Move.ydest])
self.Button[Move.xdest][Move.ydest].config(background='gray', text='SS')
self.Button[Move.xdest][Move.ydest].config(command=lambda button=self.Button[Move.xdest][Move.ydest]: self.get_click(button))
self.Button[Move.xdest][Move.ydest] = self.Button[Move.xdest][Move.ydest]
self.Button[Move.xstart][Move.ystart] = Button(Color.EMPTY, Game.win, bg='green', x=Move.xstart, y=Move.ystart, text='bebra')
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
self.create_window()
self.Button[Move.xstart][Move.ystart].Getstatus = 0
print('blatsuka')
self.Button[Move.xstart][Move.ystart].config(bg='green', text='bebra')
self.Update()
def nextturn(self, turn):
self.turn +=1
print(self.turn)
for x in range(Game.ROW):
for y in range(Game.COLUMNS):
if self.Button[x][y] == self.Button[x][y]:
self.Button[x][y].nextturn()
print("hui")
return turn
def get_click(self, clicked_button):
if Button.Getstatus == 0:
print(Button.Getstatus)
x = (clicked_button.x)
y = (clicked_button.y)
Button.Getstatus = Button.Getstatus+1
clicked_button.config(bg='red')
self.print_button()
print(self.get_info(x, y))
print(Button.Getstatus)
Move.xstart = x
Move.ystart = y
Button.Getstatus == 0
elif Button.Getstatus == 1:
Button.Getstatus = 0
print(Button.Getstatus)
clicked_button.config(bg='green')
def dest_click(self, clicked_button):
x = (clicked_button.x)
y = (clicked_button.y)
clicked_button.config(bg='blue')
Move.xdest = x
Move.ydest = y
print(Move.ydest, Move.xdest)
print('eblan')
self.Move()
def Move(self):
self.Button[Move.xstart][Move.ystart].get_moves(self, self.turn)
self.move()
def Muvprikaz(self):
if Move.xstart>-1:
self.print_button()
print(Move.xstart, Move.ystart)
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.dest_click(button))
else:
pass
def Update(self):
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
Move.xstart = -1
Move.ystart = 0
Move.xdest = 0
Move.ydest = 0
g = 1
game = Game()
game.start()

How to correctly update the grid in falling sand simulation?

So, recently I started doing some python programming, and came across a video on Youtube in which guy showcases some of his simulations made in pygame (https://www.youtube.com/watch?v=M39R2A8kuh8).
I decided to do the easiest one, the Falling Sand Simulation. I implemented eveything correctly, but when it came to updating the grid I just couldn't do it right. In the end cells are positioned correctly at the bottom of screen, but they don't fall slowly, instead they just instantly teleport there. That's happening because when for loop comes across the cell it is being updated and falling down one row down, then loop comes across that same cell once more and same thing happens
I tried fixing it with second array which holds old grid and for some reason it didn't work.
Here's the code (please ignore my bad code, just a beginner xd):
import pygame
import random
from time import sleep
pygame.init()
WIDTH, HEIGHT = 800, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Falling Sand Simulation")
BLACK = (0, 0, 0)
ORANGE = (158, 103, 32)
class Grid:
def __init__(self, width, height):
self.rows = int(width / 2)
self.columns = int(width / 2)
self.PreviousGrid = [[0 for i in range(self.columns)] for j in range(self.rows)]
self.CurrentGrid = [[0 for i in range(self.columns)] for j in range(self.rows)]
def add_cell(self, xpos, ypos):
xcell = int(xpos / 2)
ycell = int(ypos / 2)
self.CurrentGrid[xcell][ycell] = 1
def update_grid(self):
self.PreviousGrid = self.CurrentGrid
for i in range(self.rows):
if (i+1) != self.rows:
for j in range(self.columns):
if (j+1) != self.columns:
if self.PreviousGrid[i][j] == 0:
pass
else:
if self.PreviousGrid[i][j+1] == 0:
self.CurrentGrid[i][j] = 0
self.CurrentGrid[i][j+1] = 1
elif self.PreviousGrid[i-1][j+1] == 0 and self.PreviousGrid[i+1][j+1] == 0:
self.CurrentGrid[i][j] = 0
choice = random.randint(0, 1)
if choice == 0:
self.CurrentGrid[i-1][j+1] = 1
else:
self.CurrentGrid[i+1][j+1] = 1
elif self.PreviousGrid[i-1][j+1] == 0:
self.CurrentGrid[i][j] = 0
self.CurrentGrid[i-1][j+1] = 1
elif self.PreviousGrid[i+1][j+1] == 0:
self.CurrentGrid[i][j] = 0
self.CurrentGrid[i+1][j+1] = 1
def draw_grid(self, win):
for i in range(self.rows):
for j in range(self.columns):
if self.CurrentGrid[i][j] == 0:
pass
elif self.CurrentGrid[i][j] == 1:
pygame.draw.rect(win, ORANGE, pygame.Rect(int(i*2), int(j*2), 4, 4))
def main():
run = True
clock = pygame.time.Clock()
grid = Grid(WIDTH, HEIGHT)
update_rate = 0.05
countdownMS = update_rate
paused = False
while run:
clock.tick(30)
WIN.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sec = clock.get_rawtime()/100;
countdownMS -= sec;
if countdownMS < 0.0:
grid.update_grid()
countdownMS = update_rate
grid.draw_grid(WIN)
if pygame.mouse.get_pressed()[0]:
xpos, ypos = event.pos
grid.add_cell(xpos, ypos)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
You have to create a new empty grid in update_grid. Copy the bottom line of the old grid and fill the rest of the new grid depending on the previous grid:
class Grid:
# [...]
def update_grid(self):
self.PreviousGrid = self.CurrentGrid
# create a new and empty grid
self.CurrentGrid = [[0 for i in range(self.columns)] for j in range(self.rows)]
for i in range(self.rows):
self.CurrentGrid[i][self.columns-1] = self.PreviousGrid[i][self.columns-1]
# fill the new grid depending on the previous grid
for i in range(self.rows):
if i+1 < self.rows:
for j in range(self.columns):
if j+1 < self.columns:
if self.PreviousGrid[i][j] == 1:
if self.PreviousGrid[i][j+1] == 0:
self.CurrentGrid[i][j+1] = 1
elif self.PreviousGrid[i-1][j+1] == 0 and self.PreviousGrid[i+1][j+1] == 0:
self.CurrentGrid[i+random.choice([-1, 1])][j+1] = 1
elif self.PreviousGrid[i-1][j+1] == 0:
self.CurrentGrid[i-1][j+1] = 1
elif self.PreviousGrid[i+1][j+1] == 0:
self.CurrentGrid[i+1][j+1] = 1
else:
self.CurrentGrid[i][j] = 1

Maximum recursion error depth causing crash?

I'm making Minesweeper with tkinter. I want to have it so that when a button with 0 mines surrounding it is clicked, all the buttons around that one are automatically clicked. However, when I run this and click on a button with 0, the program crashes. (originally it gave me the error of "maximum recursion depth exceeded"). How do I fix this? Here is the full code:
import tkinter as tk
import sys
import random
from tkinter import messagebox
from PIL import Image, ImageTk
from types import FunctionType
from copy import copy
app = tk.Tk()
app.geometry("432x468")
app.title("Minesweeper")
sys.setrecursionlimit(999999999)
colors = ['#FFFFFF', '#0000FF', '#008200', '#FF0000', '#000084', '#840000', '#008284', '#840084', '#000000']
# create lists
def createlists():
global buttonlist
global mylist
global numberlist
global panelist
global rightlist
global flaglist
global rowlist
global columnlist
global abomb
global adic
global secondlist
secondlist = []
abomb = []
adic = {}
buttonlist = []
mylist = [0]
numberlist = []
panelist = []
rightlist = []
flaglist = []
rowlist = []
columnlist = []
for a in range(1,18):
mylist.append(18*a)
for i in range(1,325):
button = "b" + str(i)
flag = 'flag' + str(i)
row = 'row' + str(i)
column = 'column' + str(i)
buttonlist.append(button)
numberlist.append(i)
secondlist.append(i)
flaglist.append(flag)
rowlist.append(row)
columnlist.append(column)
# randomly select bombs
def selectbombs():
for i in range(0,50):
x = "panel" + str(bomblist[i])
panelist.append(x)
for i in bomblist:
n = "a" + str(i)
abomb.append(n)
secondlist.remove(i)
# create function for when a bomb is clicked
def mine():
global bomblist
for i in range(0,50):
panelist[i] = tk.Label(app, image = bomb)
for x in mylist:
for i in range(x,x+18):
if i+1 in bomblist:
thing = bomblist.index(i+1)
panelist[thing].grid(row=mylist.index(x)+1, column=i-x+1, columnspan=1)
MsgBox = tk.messagebox.askquestion ('Game Over', 'You clicked on a bomb! Game Over. Would you like to play again?')
if MsgBox == 'no':
app.destroy()
else:
createlists()
bomblist = random.sample(numberlist, 50)
selectbombs()
buttons()
numbers()
flags()
# create grid of buttons
def buttons():
for i in range(0,324):
if i+1 in bomblist:
buttonlist[i] = tk.Button(app, text="", width=2, height=1, command=mine)
else:
buttonlist[i] = tk.Button(app, text="", width=2, height=1)
for x in mylist:
for i in range(x,x+18):
buttonlist[i].grid(row=mylist.index(x)+1, column=i-x+1, columnspan=1)
rowlist[i] = mylist.index(x)+1
columnlist[i] = i-x+1
# determine the number of bombs adjacent to each button
def numbers():
for i in range(1,325):
adic.update({"a"+str(i) : 0})
alist = list(adic)
for i in bomblist:
for x in numberlist:
if rowlist[x-1] in range(rowlist[numberlist.index(i)]-1, rowlist[numberlist.index(i)]+2) and columnlist[x-1] in range(columnlist[numberlist.index(i)]-1, columnlist[numberlist.index(i)]+2):
adic[alist[x-1]] = adic[alist[x-1]] + 1
for i in bomblist:
del adic[alist[numberlist.index(i)]]
alist = list(adic)
for i in adic:
buttonlist[secondlist[alist.index(i)]-1].bind("<Button-1>", lambda event, x=secondlist[alist.index(i)]-1, y=adic[i] : num(x, y))
# number functions
def num(x, y):
a = rowlist[x]
b = columnlist[x]
if y==0:
buttonlist[x].config(bg = '#FFFFFF')
buttonlist[x]["state"] = "disabled"
if a != 1 and b != 1:
num(x-19, adic["a"+str(x-18)])
if a != 1:
num(x-18, adic["a"+str(x-17)])
if a != 1 and b != 18:
num(x-17, adic["a"+str(x-16)])
if b != 1:
num(x-1, adic["a"+str(x)])
if b != 18:
num(x+1, adic["a"+str(x+2)])
if a != 18 and b != 1:
num(x+17, adic["a"+str(x+18)])
if a != 18:
num(x+18, adic["a"+str(x+19)])
if a != 18 and b != 18:
num(x+19, adic["a"+str(x+20)])
else:
buttonlist[x].config(text = y, disabledforeground = colors[y], bg = '#FFFFFF')
buttonlist[x]["state"] = "disabled"
# create function to place a flag
im = Image.open("flag.png")
im = im.resize((20,20), Image.ANTIALIAS)
flag = ImageTk.PhotoImage(im)
def flags():
for i in range(0,324):
buttonlist[i].bind("<Button-3>", lambda event, x=i : right(event, x))
def right(event, x):
if buttonlist[x]["state"] == "normal":
flaglist[x] = tk.Button(app, text = "", width=18, height=19, image = flag)
flaglist[x].grid(row=rowlist[x], column=columnlist[x], columnspan=1)
flaglist[x].bind("<Button-1>", lambda event: flaglist[x].destroy())
# check if the game has been won
def checkwin():
disnum = 0
for i in secondlist:
if buttonlist[i-1]["state"] == "disabled":
disnum = disnum + 1
if disnum == 274:
MsgBox = tk.messagebox.askquestion ('Game Won', 'You have won the game! Would you like to play again?')
if MsgBox == 'no':
app.destroy()
else:
createlists()
bomblist = random.sample(numberlist, 50)
selectbombs()
buttons()
numbers()
flags()
# open images
img = Image.open("bomb.png")
img = img.resize((20,20), Image.ANTIALIAS)
bomb = ImageTk.PhotoImage(img)
createlists()
bomblist = random.sample(numberlist, 50)
selectbombs()
buttons()
numbers()
flags()
app.mainloop()
the specific part which is causing the program to crash:
def num(x, y):
a = rowlist[x]
b = columnlist[x]
if y==0:
buttonlist[x].config(bg = '#FFFFFF')
buttonlist[x]["state"] = "disabled"
if a != 1 and b != 1:
num(x-19, adic["a"+str(x-18)])
if a != 1:
num(x-18, adic["a"+str(x-17)])
if a != 1 and b != 18:
num(x-17, adic["a"+str(x-16)])
if b != 1:
num(x-1, adic["a"+str(x)])
if b != 18:
num(x+1, adic["a"+str(x+2)])
if a != 18 and b != 1:
num(x+17, adic["a"+str(x+18)])
if a != 18:
num(x+18, adic["a"+str(x+19)])
if a != 18 and b != 18:
num(x+19, adic["a"+str(x+20)])
else:
buttonlist[x].config(text = y, disabledforeground = colors[y], bg = '#FFFFFF')
buttonlist[x]["state"] = "disabled"
The issue is that in the recursion process, the code is backtracking on previous squares. When checking a new square, be sure it has not already be checked.
In the num function, add a line of code to skip squares that have already been disabled:
def num(x, y):
if buttonlist[x]["state"] == "disabled": return # add this line
a = rowlist[x]
b = columnlist[x]

issue with filling the correct grid cell in tkinter

im trying to make a battleship game in python using tkinter,
the minimum reproducible example is:
from tkinter import *
from random import randint
import time
tk = Tk()
class player:
def __init__(self, master):
self.master = master
self.placedships = []
self.bombed = []
self.sunk = []
self.ship_sizes = [5, 4, 3]
self.player_canvas = Canvas(master, height=300, width=300, highlightbackground='black', highlightthickness=0.5)
self.ai_canvas = Canvas(master, height=300, width=300, highlightbackground='black', highlightthickness=0.5)
self.player_canvas.grid(row=1, column=0, padx=50)
self.ai_canvas.grid(row=1, column=1, padx=50)
self.direction = 'v'
self.shipChosen = 0
gridLabel_player = Label(master,text="Your grid \nA B C D E F G H I J ")
gridLabel_player.grid(row=0,column=0)
gridLabel_ai = Label(master,text="AI's grid \nA B C D E F G H I J ")
gridLabel_ai.grid(row=0,column=1)
for x in range(10):
for y in range(10):
self.player_canvas.create_rectangle(x * 30, y * 30, 300, 300, fill = 'white')
self.ai_canvas.create_rectangle(x * 30, y * 30, 300, 300, fill = 'white')
#variables to store data for cells on game grid
# # self.player_ocean = 10 * [10 * [0]]
# # self.ai_ocean = 10 * [10 * [0]]
self.player_ocean = []
self.ai_ocean = []
temp = []
for i in range(10):
for y in range(10):
temp += [0]
self.player_ocean += [temp]
self.ai_ocean += [temp]
temp = []
self.selectedCoord = [0,0] # [0] = x coord, [1] = y coord
def placeShip(self):
def moveShip(event):
if event.keysym == 'Down' and self.selectedCoord[1] != 9:
self.selectedCoord[1] += 1
elif event.keysym == 'Up' and self.selectedCoord[1] != 0:
self.selectedCoord[1] -= 1
elif event.keysym == 'Left' and self.selectedCoord[0] != 0:
self.selectedCoord[0] -= 1
elif event.keysym == 'Right' and self.selectedCoord[0] != 9:
self.selectedCoord[0] += 1
print('selected coord:',self.selectedCoord)
def selectPlacement(event):
col = self.selectedCoord[0]
row = self.selectedCoord[1]
if self.direction == 'v':
v_range_start = row - self.ship_sizes[self.shipChosen] + 1
for y in range(v_range_start, row+1):
'''insert validation to reject ship clashing'''
self.player_ocean[y][col] = 1
self.placedships += [y,col]
self.refresh_ocean()
self.master.bind("<Up>", moveShip)
self.master.bind("<Down>", moveShip)
self.master.bind("<Left>", moveShip)
self.master.bind("<Right>", moveShip)
self.master.bind("<Return>", selectPlacement)
def refresh_ocean(self): # 1s turns to green
for y in range(10):
for x in range(10):
if self.player_ocean[y][x] == 1:
self.player_canvas.itemconfig(self.player_canvas.create_rectangle( (x+1) * 30, (y-1) * 30,x * 30, y * 30, fill='green'))
player1 = player(tk)
player1.placeShip()
tk.mainloop()
the problem i have is that lets say if i press the down arrow until my selected coordinate is [0,9], the code should colour the 6th to 10th box from top down, in the first column, but it colours the 5th to 9th box.
i have tried debugging it by checking if the coordinates x and y used in the last function refresh_ocean were wrong, but they were as expected
So the short and simple fix is to change (y-1) to (y+1).
That said I made a few changes:
Rewrote some of your names to follow PEP8 style guide.
Converted your refresh_ocean method to handle the initial draw and all draws afterwords. As well as added a delete('all') to clear the board so we are not draw on top of the previous objects. This will prevent using up more memory as the game progresses.
Cleaned up imports and changed from tkinter import * to import tkinter as tk. This will help prevent overwriting other things in the namespace.
By writing your refresh_ocean to handle all the drawing we can specify what needs to be drawn with tags like 'player1' and 'ai'.
Changed += to .append() for your list generation as append() is twice as fast as '+=. See this post: Difference between “.append()” and “+= []”?
Changed you single label of headers into a loop to create each label evenly. This will allow for a more accurate header then trying to manually set spaces in a single string.
Lastly because you are already checking of the key is one of the 4 arrow keys we can use a single bind to '<Key>' and get the same results as all 4 binds.
Updated code:
import tkinter as tk
class Player(tk.Tk):
def __init__(self):
super().__init__()
self.placed_ships = []
self.player_ocean = []
self.ai_ocean = []
self.bombed = []
self.sunk = []
self.bord_size = list('ABCDEFGHIJ')
self.selected_coord = [0, 0]
self.ship_sizes = [5, 4, 3]
self.direction = 'v'
self.shipChosen = 0
wh = 300
p_label_frame = tk.Frame(self, width=wh, height=30)
a_label_frame = tk.Frame(self, width=wh, height=30)
p_label_frame.grid(row=1, column=0, padx=50)
a_label_frame.grid(row=1, column=1, padx=50)
p_label_frame.grid_propagate(False)
a_label_frame.grid_propagate(False)
for ndex, letter in enumerate(self.bord_size):
p_label_frame.columnconfigure(ndex, weight=1)
a_label_frame.columnconfigure(ndex, weight=1)
tk.Label(p_label_frame, text=letter).grid(row=0, column=ndex)
tk.Label(a_label_frame, text=letter).grid(row=0, column=ndex)
self.player_canvas = tk.Canvas(self, height=wh, width=wh, highlightbackground='black', highlightthickness=0.5)
self.ai_canvas = tk.Canvas(self, height=wh, width=wh, highlightbackground='black', highlightthickness=0.5)
self.player_canvas.grid(row=2, column=0, padx=50)
self.ai_canvas.grid(row=2, column=1, padx=50)
temp = []
for i in range(10):
for y in range(10):
temp.append(0)
self.player_ocean.append(temp)
self.ai_ocean.append(temp)
temp = []
self.refresh_ocean()
self.bind("<Key>", self.move_ship)
self.bind("<Return>", self.select_placement)
def move_ship(self, event):
if event.keysym == 'Down' and self.selected_coord[1] < 9:
self.selected_coord[1] += 1
elif event.keysym == 'Up' and self.selected_coord[1] > 0:
self.selected_coord[1] -= 1
elif event.keysym == 'Left' and self.selected_coord[0] > 0:
self.selected_coord[0] -= 1
elif event.keysym == 'Right' and self.selected_coord[0] < 9:
self.selected_coord[0] += 1
print('selected coord:', self.selected_coord)
def select_placement(self, _=None):
col = self.selected_coord[0]
row = self.selected_coord[1]
if self.direction == 'v':
v_range_start = row - self.ship_sizes[self.shipChosen] + 1
for y in range(v_range_start, row+1):
'''insert validation to reject ship clashing'''
self.player_ocean[y][col] = 1
self.placed_ships += [y, col]
self.refresh_ocean(side='player1')
def refresh_ocean(self, side=None):
self.player_canvas.delete('all')
self.ai_canvas.delete('all')
for y in range(len(self.bord_size)):
for x in range(len(self.bord_size)):
if self.player_ocean[y][x] == 1 and side == 'player1':
self.player_canvas.itemconfig(self.player_canvas.create_rectangle(
(x+1) * 30, (y+1) * 30, x * 30, y * 30, fill='green'))
else:
self.player_canvas.create_rectangle(x * 30, y * 30, 300, 300, fill='white')
if self.ai_ocean[y][x] == 1 and side == 'ai':
self.ai_canvas.itemconfig(self.ai_canvas.create_rectangle(
(x + 1) * 30, (y + 1) * 30, x * 30, y * 30, fill='green'))
else:
self.ai_canvas.create_rectangle(x * 30, y * 30, 300, 300, fill='white')
if __name__ == '__main__':
Player().mainloop()
Results:
your refresh_ocean had an off by 1 error
def refresh_ocean(self): # 1s turns to green
for y in range(10):
for x in range(10):
if self.player_ocean[y][x] == 1:
self.player_canvas.itemconfig(self.player_canvas.create_rectangle( x*30, y * 30, x*30+30, y*30+30, fill='green'))
and select placement was placing ships in with a negative index.
i changed your iteration from range(row-size, row) to range(row, row+size) and added bounds checking, but that should probably be moved to the movement handling.
def selectPlacement(event):
col = self.selectedCoord[0]
row = self.selectedCoord[1]
if self.direction == 'v':
if row + self.ship_sizes[self.shipChosen] > 9:
row = 10 - self.ship_sizes[self.shipChosen]
for y in range(row, row + self.ship_sizes[self.shipChosen]):
'''insert validation to reject ship clashing'''
self.player_ocean[y][col] = 1
self.placedships += [y,col]
self.refresh_ocean()
ps: your drawing new squares on top of the old squares. you might want to clear the ocean before drawing.

Categories