I am pretty new to Python, and I just need some help with a simple algorithm for a minesweeper game.
I am trying to generate the numbers around the mines that show how many are in a 3x3 area, and so far it has been successful except that the numbers are overlapping when generated instead of replacing each other.
One solution I have tried is using the EraseTile function to erase the previous number on that tile and replacing it with a higher number, but I am unsure where I would apply this function if it is even needed at all.
I am using a combination of the turtle and tkinter modules.
Below is the (unfinished) code for the game, any help with my problem as well as any feedback on anything else in the code would be appreciated!
# import stuff
from turtle import *
from tkinter import *
from random import *
# set up window and turtle
root = Tk()
root.geometry('250x250')
root.title('Minesweeper')
pa = Canvas(root)
pa.config(width=245, height=245)
pa.pack()
scrn = TurtleScreen(pa)
scrn.tracer(0)
scrn.addshape('assets/mine.gif')
t = RawTurtle(scrn, shape='assets/mine.gif')
t.speed(0)
t.turtlesize(0.9)
t.penup()
t.goto(-120, 100)
# place mines and log their coordinates
mines = 0
tiles = 144
xlines = 0
ylines = 0
x = -120
y = 100
minex = []
miney = []
poslogs = []
# create horizontal grid lines
while xlines < 19:
t.goto(x, y)
t.pendown()
t.forward(240)
y -= 20
xlines += 1
t.penup()
x = -100
y = 120
t.setheading(270)
# create vertical grid lines
while ylines < 19:
t.goto(x, y)
t.pendown()
t.forward(240)
x += 20
ylines += 1
t.penup()
# generate mines in random locations
while mines < 15:
xpos = randint(-11, 11)
ypos = randint(-11, 11)
if minex.count(xpos) > 0:
if miney.count(ypos) > 0:
mines -= 1
continue
else:
while xpos % 2 != 1:
xpos = randint(-11, 11)
while ypos % 2 != 1:
ypos = randint(-11, 11)
else:
while xpos % 2 != 1:
xpos = randint(-11, 11)
while ypos % 2 != 1:
ypos = randint(-11, 11)
t.goto((xpos * 10), (ypos * 10))
t.stamp()
minex.append(xpos * 10)
miney.append(ypos * 10)
numxvalues = [-2.5, 17.5, 17.5, 17.5, -2.5, -22.5, -22.5, -22.5]
numyvalues = [12.5, 12.5, -7.5, -27.5, -27.5, -27.5, -7.5, 12.5]
index = 0
# generate numbers that indicate mines in a 3x3 area
for num in numxvalues:
newpos = xpos * 10 + num, ypos * 10 + numyvalues[index]
def EraseTile():
t.goto(xpos * 10, ypos * 10 + 20)
t.shape('square')
t.color('white')
t.stamp()
t.color('black')
t.shape('assets/mine.gif')
t.goto(newpos)
if poslogs.count(newpos) == 0:
t.write('1', font=30)
elif poslogs.count(newpos) == 1:
t.write('2', font=30)
elif poslogs.count(newpos) == 2:
t.write('3', font=30)
elif poslogs.count(newpos) == 3:
t.write('4', font=30)
elif poslogs.count(newpos) == 4:
t.write('5', font=30)
poslogs.append(newpos)
index += 1
mines += 1
root.mainloop()
Aucun commentaire:
Enregistrer un commentaire