Trying to make a simple maze game in Python Turtle. Using the stamp method I have made the outline of the first level, and it loads perfectly. I want to add a second level and access it, but I don't know how. Any help will be appreciated.
P.S - I am quite new to stack overflow so idk how much code I should put here, but expecting help, I am posting the full code. Thank in advance
import turtle
import math
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("A maze game")
wn.setup(700,700)
class Pen(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("white")
self.penup()
self.speed(0)
class Treasure(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("square")
self.color(color_1)
self.penup()
self.speed(0)
self.gold = 100
self.goto(x, y)
def destroy(self):
self.goto(2000, 2000)
self.hideturtle()
def change_color(self):
self.color(color_2)
color_1 = ("white")
color_2 = ("gold")
class Player(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("circle")
self.color("red")
self.penup()
self.speed(0)
self.gold = 0
def go_up(self):
move_to_x = player.xcor()
move_to_y = player.ycor() + 24
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_down(self):
move_to_x = player.xcor()
move_to_y = player.ycor() - 24
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_left(self):
move_to_x = player.xcor() - 24
move_to_y = player.ycor()
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_right(self):
move_to_x = player.xcor() + 24
move_to_y = player.ycor()
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def is_close_to(self, other):
a = self.xcor()-other.xcor()
b = self.ycor()-other.ycor()
distance = math.sqrt((a ** 2) + (b ** 2))
if distance < 50:
return True
else:
return False
levels = [""]
level_1 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X P T X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]
treasures = []
levels.append(level_1)
def setup_maze(level):
for y in range(len(level)):
for x in range(len(level[y])):
character = level[y][x]
screen_x = -288 + (x * 24)
screen_y = 288 - (y * 24)
if character == "X":
pen.goto(screen_x, screen_y)
pen.stamp()
walls.append((screen_x, screen_y))
if character == "P":
player.goto(screen_x, screen_y)
if character == "T":
treasures.append(Treasure(screen_x, screen_y))
pen = Pen()
player = Player()
walls = []
setup_maze(levels[1])
turtle.listen()
turtle.onkey(player.go_left,"Left")
turtle.onkey(player.go_right,"Right")
turtle.onkey(player.go_up,"Up")
turtle.onkey(player.go_down,"Down")
wn.tracer(0)
while True:
for treasure in treasures:
if player.is_close_to(treasure):
treasure.change_color()
wn.update()