Hello everybody, my name is Ludvig, I am a swedish student who educates myself to become an engineer. For my programming course I have made a minesweepergame, but to get the highest grade availible, I have to make it in graphics. I have succeded in doing a version without the graphics, but when it comes to the graphics, I am hopelessly lost.
This is how my nonworking attempt at graphical minesweeper looks. The biggest problem I have is that I actually don't rightly know the best way to build a graphical grid, and when I try, python tells me that the graphical list simply does not exist, I can't manipulate the values in the code. If someone has some practical advice and desirably some codeexamples in their replies I would be eternally gratefull.
A quick rundown on my code is that I first create the boxes which will store the actual values. Before I create the graphical grid, I create a digital grid that stores all the squares and the position of the mines. Based on this digital grid, I create a graphical grid named li, but when I click the squares in the graphical grid, all I get is errormessages.
import random from Tkinter import * yMax = 10 xMax = 10 mines = 5 class box: def __init__(self, clicked, mine, varde): self.clicked= clicked self.mine = mine self.varde = varde def arm(self): self.mine = True def click(y, x): matris[y][x].clicked= True if matris[y][x].varde == 0: li.bttn["text"] = " " for r in range(-1,2,1): for k in range(-1,2,1): if xMax > matris[y][x].x + x and matris[y][x].x+x >= 0 and yMax > matris[y][x].y+y and matris[y][x].y+y >= 0: if not li[li[y][x].y+y][li[y][x].x+x].clicked: click(li[y][x].y+y, li[y][x].x+x) elif matris[y][x].varde != 0 and matris[y][x].mine == False: return y return x vardeKlick(y, x) matris = [None]*xMax for y in range(yMax): matris[y] = [None]*xMax for x in range(xMax): matris[y][x] = box(False, False, 0) placedMines = 0 r = random.randint(0, yMax-1) k = random.randint(0, xMax-1) if not matris[r][k].mine: matris[r][k].arm() placedMines += 1 for y in range(yMax): for x in range(xMax): if matris[y][x].mine: for yp in xrange(-1,2,1): for xp in xrange(-1,2,1): if yMax > x+xp and x+xp >= 0 and xMax > y+yp and y+yp >= 0: matris[y+yp][x+xp].varde += 1 class rojGraf(Frame): def __init(self, master): Frame.__init__(self, master) self.grid() class square: def __init__(self, y, x): self.y = y self.x = x def poke(self): click(self.y, self.x) for y in range (yMax): li = list() for x in range (xMax): bttn = square(y, x) bttn.bttn = Button(text = " ", height = 2, width = 3, command = bttn.poke) bttn.bttn.grid(row = y, column = x) li.append(bttn) #roj = Tk() roj.mainloop
Editor: added the code tags