1

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

To load a new level, you need to reset the current state of the world to a known base state, then setup the new level. You seem to have figured out the latter, and the former involves clearing the screen, re-initializing its properties, and clearing the game state.

First, you need to clear the window. As this answer tells us, this can be done by calling turtle.Screen().reset(), or in your case wm.reset().

After that's done, you need to reinitialize all the properties of the screen, like setting the bgcolor and title and other things like this. Right now these happen at the beginning of the file, but you'll need to do them multiple times through your program's lifecycle, so you should place them in a separate function, like init_screen, so that you can call it once you've reset it.

Finally, you need to reset the game state. In your case, this would be your treasures and walls lists, but as you add more features this may include any other data that does not need to persist between one level and the next. This data actually becomes invalid as soon as you call setup_maze, so it would make sense to first clear them and then setup the new level in the inner loops.

Danya02
  • 1,006
  • 1
  • 9
  • 24