0

so currently i'm trying to plot out a block maze by reading from a .txt file and then displaying it in Python's Turtle Library. Currently, my code is only able to draw out the boxes, but not the grid lines surrounding the boxes. Is there anyway to deal with this problem, cuze i tried to see the docs and they only suggested turtle.Turtle.fillcolor, which doesnt really seems right.

Here's my current code:

# import the necessary library
import turtle

# read the .txt file here!
with open("map01.txt") as f:
    content = f.readlines()
content = [x.strip() for x in content] 

# create the map here!
window = turtle.Screen()
window.bgcolor("white") # set the background as white(check if this is default)
window.title("PIZZA RUNNERS") # create the titlebar
window.setup(700,700)

# create pen
class Pen(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("square")
        self.color('grey')
        self.penup()
        self.speed(0)

# create maze_list
maze = []

# add the maze to maze
maze.append(content)

# create conversion from the list to the map in turtle
def setup_maze(level):
    for y in range(len(level)):
        for x in range(len(level[y])):
            # get the character at each x,y coordinate
            character = level[y][x]
            # calculate the screen x, y coordinates
            screen_x = -288 + (x * 24)
            screen_y = 288 - (y * 24)
            
            # check if it is a wall
            if character == "X":
                pen.goto(screen_x, screen_y)
                pen.stamp()

# create class instances     
pen = Pen()

# set up the maze
setup_maze(maze[0])

# main game loop
while True:
    pass

And this is how the current text file i am reading from looks like:

XXXXXXXXXXXX
X.........eX
X.XXX.XXX..X
X.XsX.X.XX.X
X.X......X.X
X.XXXXXXXX.X
X..........X
XXXXXXXXXXXX

The 's' and 'e' are supposed to represent the starting point and the ending point, which is not implemented yet, so it can be ignored. The dots represent the path, and the X represents walls. The current map(or txt) dimension is 8 rows by 12 columns.

Right now my output looks like this: enter image description here

I wanted it to look something like this(referring to the addition of grids, not the same pattern maze):

enter image description here

2
  • do you want to draw lines in-between the squares, from top to bottom and from left to right Commented Oct 30, 2020 at 4:37
  • @rioV8 Err ye, i'll try to draw lines in between now that you have voiced out. Thanks! Will update the answer if i managed to solve it! Commented Oct 30, 2020 at 4:41

1 Answer 1

1

Assuming what you desire is:

enter image description here

Then we need to resize the square cursor from its default size of 20 to your tile size of 24:

from turtle import Screen, Turtle

TILE_SIZE = 24
CURSOR_SIZE = 20

class Pen(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('square')
        self.shapesize(TILE_SIZE / CURSOR_SIZE)
        self.color('grey')
        self.penup()
        self.speed('fastest')

def setup_maze(level):
    ''' Conversion from the list to the map in turtle. '''

    maze_height, maze_width = len(level), len(level[0])

    for y in range(maze_height):
        for x in range(maze_width):
            # get the character at each x,y coordinate
            character = level[y][x]

            # check if it is a wall
            if character == 'X':
                # calculate the screen x, y coordinates
                screen_x = (x - maze_width) * TILE_SIZE
                screen_y = (maze_width - y) * TILE_SIZE

                pen.goto(screen_x, screen_y)
                pen.stamp()

screen = Screen()
screen.setup(700, 700)
screen.title("PIZZA RUNNERS")

maze = []

with open("map01.txt") as file:
    for line in file:
        maze.append(line.strip())

pen = Pen()

setup_maze(maze)

screen.mainloop()

If you're looking for something more like:

enter image description here

Then change the line:

self.color('grey')

in the code above to:

self.color('black', 'grey')

Finally, if you desire:

enter image description here

Then we need to make a couple of small changes to the code above:

class Pen(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('square')
        self.shapesize(TILE_SIZE / CURSOR_SIZE)
        self.pencolor('black')
        self.penup()
        self.speed('fastest')

def setup_maze(level):
    ''' Conversion from the list to the map in turtle. '''

    maze_height, maze_width = len(level), len(level[0])

    for y in range(maze_height):
        for x in range(maze_width):
            # get the character at each x,y coordinate
            character = level[y][x]

            # check if it is a wall or a path
            pen.fillcolor(['white', 'grey'][character == 'X'])

            # calculate the screen x, y coordinates
            screen_x = (x - maze_width) * TILE_SIZE
            screen_y = (maze_width - y) * TILE_SIZE

            pen.goto(screen_x, screen_y)
            pen.stamp()
Sign up to request clarification or add additional context in comments.

3 Comments

@prodoggy4life, I've added two gridded alternatives based on the example image you added.
Hi thank you for the detailed answer! Appreciate it! But i have a question here. How do you derive with the values for tile size the cursor size, and the screen_size?
@prodoggy4life, the tile size comes from your code, screen_y = 288 - (y * 24), the cursor size is based on the turtle source code and I didn't have a screen_size but you can get that from the screen_width() and screen_height() methods of the screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.