3

i am a complete beginner to coding and at school we started learning python. My little cousin discovered that, and asked me to make a game... since i am not an expert i decided to do a simple game, a maze. I coded some functions to generate the maze from 2 lists made up by 1 and 0. Then i tried to make the movement section for a turtle that has to cross the maze to escape.

The fact is that it is too easy to cheat since you can pass trough the walls. I need your help to solve that problem. i was thinnking to undo the wrong moves. but i am opened to any solutions

My cousin palyed the game today and it is very enthusiast to play the final version. i'll post my code with some comments so you can understand better.

P.S. i am sorry for my bad english :) thank you so much

"""
                                     .---.
                                     |   |      __.....__      __  __   ___                             __.....__
                                     |   |  .-''         '.   |  |/  `.'   `.                       .-''         '.
     .|             .-,.--.      .|  |   | /     .-''"'-.  `. |   .-.  .-.   '                     /     .-''"'-.  `.
   .' |_            |  .-. |   .' |_ |   |/     /________\   \|  |  |  |  |  |    __              /     /________\   \
 .'     |   _    _  | |  | | .'     ||   ||                  ||  |  |  |  |  | .:--.'.  .--------.|                  |
'--.  .-'  | '  / | | |  | |'--.  .-'|   |\    .-------------'|  |  |  |  |  |/ |   \ | |____    |\    .-------------'
   |  |   .' | .' | | |  '-    |  |  |   | \    '-.____...---.|  |  |  |  |  |`" __ | |     /   /  \    '-.____...---.
   |  |   /  | /  | | |        |  |  |   |  `.             .' |__|  |__|  |__| .'.''| |   .'   /    `.             .'
   |  '.'|   `'.  | | |        |  '.''---'    `''-...... -'                   / /   | |_ /    /___    `''-...... -'
   |   / '   .'|  '/|_|        |   /                                          \ \._,\ '/|         |
   `'-'   `-'  `--'            `'-'                                            `--'  `" |_________|

to my little cousin
"""



import turtle

turtle.title("TurtleMaze")





#here i define some funcions that mark the walls of the maze by the usage of some lists.

def mark_horizontal_line(lo, x, y):
    line_horizontal = turtle.Turtle()
    line_horizontal.speed(0)
    line_horizontal.hideturtle()
    line_horizontal.up()
    line_horizontal.setposition(x, y)
    for n in lo:
        if n == 1:
            line_horizontal.down()
            line_horizontal.forward(21)
            line_horizontal.up()
        if n == 0:
            line_horizontal.forward(21)


def mark_horizontal_lines(lo, x, y):
    step_y = y
    for n in lo:
        mark_horizontal_line(n, x, step_y)
        step_y -= 21



def mark_line_vertical(lv, x, y):
    line_v = turtle.Turtle()
    line_v.hideturtle()
    line_v.speed(0)
    line_v.up()
    line_v.setposition(x, y)
    line_v.right(90)
    for n in lv:
        if n == 1:
            line_v.down()
            line_v.forward(21)
            line_v.up()
        if n == 0:
            line_v.forward(21)


def create_lines_vertical(lv, x, y):
    step_dx = x
    for n in lv:
        mark_line_vertical(n, step_dx, y)
        step_dx += 21


# this function marcks the entire maze by attaching all the others above

def mark_maze(lo, lv, x, y):
    mark_horizontal_lines(lo, x, y)
    create_lines_vertical(lv, x, y)

# i create a finestra = window

finestra = turtle.Screen()
finestra.screensize(600, 400)
finestra.title("TurtleMaze")

# these are the lists that contain the info to mark the lines

lo = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],
      [0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0],
      [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
      [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
      [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
      [1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
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0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1],
      [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0],
      [0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1],
      [1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
      [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
      [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0],
      [1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1],
      [0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0],
      [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
      [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]




# i call the function to generate the maze

mark_maze(lo, lv, -200, 300)



# now the movement section

starting_pos = (63, -162)

red_line = turtle.Turtle()
red_line.hideturtle()
red_line.speed(0)
red_line.up()
red_line.setposition(starting_pos)
red_line.color("red")
red_line.up()
red_line.down()


def dx():
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(-180)



def down():

    red_line.right(90)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(-90)



def up():

    red_line.right(-90)
    red_line.speed(5)
    red_line.forward(11)
    red_line.speed(0)
    red_line.right(90)

# to make the turtle go back i made some separated commands to keep it as easy as possible

def undo_sx():
    red_line.undo()
    red_line.undo()
    red_line.undo()



def undo_dx():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    


def undo_down():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def undo_up():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def close():
    finestra.bye()


finestra.onkey(dx, "Right")
finestra.onkey(sx, "Left")
finestra.onkey(up, "Up")
finestra.onkey(down, "Down")
finestra.onkey(undo_dx, "d")
finestra.onkey(undo_sx, "a")
finestra.onkey(undo_down, "s")
finestra.onkey(undo_up, "w")
finestra.onkey(close, "q")

finestra.listen()
finestra.mainloop()
MarcoMac01
  • 31
  • 1
  • Hello, I'm currently working on something that could help you, I'll maybe post it tonight or tomorrow. – ƒkS124 Jan 23 '21 at 20:52
  • So after considering it about an hour, I arrived at the conclusion that with the way you draw your maze, you can't check if there is a wall arround you. If you want to, you would have to define a list that tells if at the index of the list your player is, he can move down, left, right or up. Obviously, with two lists with differents size you can't. – ƒkS124 Jan 23 '21 at 21:38
  • mh, i sincerely cannot figure it out because i do not have the knowledge... could you please make me an example maybe in code? – MarcoMac01 Jan 24 '21 at 18:27

3 Answers3

0

Concept:

This is possible if you replace the pixels of the drawn lines with small turtle objects aligned, each small turtle object making a pixel of the lines.

So for example, if each small turtle object is stored in a list called lines:

def dx():
    red_line.speed(5)
    red_line.forward(11)
    for line in lines:
        if red_line.distance(line) < 5: # If the player came within 5 units of any pixel in the lines list...
            red_line.undo() # then undo the previous move
            break
    red_line.speed(0)

Implementation:

Okay, so here are the changes I made to your code.

  1. I defined a pixels list to store each turtle that makes up the walls:
pixels = list()
  1. In your mark_horizontal_line and mark_line_vertical functions, instead of drawing the walls, each line will be made up of 2 turtle objects of shape 'square' and size 1 pixel by 10.5 pixels (half the size of a wall).

So in the mark_horizontal_line function, from

            line_horizontal.down()
            line_horizontal.forward(21)
            line_horizontal.up()

to

            for i in range(2):
                line_horizontal.forward(21 / 4)
                pixel = line_horizontal.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_horizontal.forward(21 / 4)

And in the mark_line_vertical function, from:

            line_v.down()
            line_v.forward(21)
            line_v.up()

to

            for i in range(2):
                line_v.forward(21 / 4)
                pixel = line_v.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_v.forward(21 / 4)
  1. I defined a forward() function that will make the red_line turtle move forward, but automatically undo its moves if it comes within 5 pixels of any of the pixels of the pixels list:
def forward(turt):
    for i in range(11):
        turt.forward(1)
        if any(pixel.distance(turt) < 5 for pixel in pixels):
            for i in range(i):
                turt.undo()
            break
  1. Finally, replace the red_line.forward(11) in each of the , , and functions with forward(red_line) to call the function defined:
def dx():
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(-180)

def down():
    red_line.right(90)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(-90)

def up():
    red_line.right(-90)
    red_line.speed(5)
    forward(red_line) # Function call
    red_line.speed(0)
    red_line.right(90)

Note: If you don't want to wait minute for the maze to be drawn, you can turn off tracer() for the mark_maze() call:

turtle.tracer(0)
mark_maze(lo, lv, -200, 300)
turtle.update()
turtle.tracer(1)

Altogether:

import turtle

turtle.title("TurtleMaze")





#here i define some funcions that mark the walls of the maze by the usage of some lists.
pixels = list()

def mark_horizontal_line(lo, x, y):
    line_horizontal = turtle.Turtle()
    line_horizontal.speed(0)
    line_horizontal.hideturtle()
    line_horizontal.up()
    line_horizontal.setposition(x, y)
    for n in lo:
        if n == 1:
            for i in range(2):
                line_horizontal.forward(21 / 4)
                pixel = line_horizontal.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_horizontal.forward(21 / 4)
                
        if n == 0:
            line_horizontal.forward(21)


def mark_horizontal_lines(lo, x, y):
    step_y = y
    for n in lo:
        mark_horizontal_line(n, x, step_y)
        step_y -= 21



def mark_line_vertical(lv, x, y):
    line_v = turtle.Turtle()
    line_v.hideturtle()
    line_v.speed(0)
    line_v.up()
    line_v.setposition(x, y)
    line_v.right(90)
    for n in lv:
        if n == 1:
            for i in range(2):
                line_v.forward(21 / 4)
                pixel = line_v.clone()
                pixel.showturtle()
                pixel.shape("square")
                pixel.shapesize(1 / 20, 10.5 / 20)
                pixels.append(pixel)
                line_v.forward(21 / 4)
        if n == 0:
            line_v.forward(21)


def create_lines_vertical(lv, x, y):
    step_dx = x
    for n in lv:
        mark_line_vertical(n, step_dx, y)
        step_dx += 21


# this function marcks the entire maze by attaching all the others above

def mark_maze(lo, lv, x, y):
    mark_horizontal_lines(lo, x, y)
    create_lines_vertical(lv, x, y)

# i create a finestra = window

finestra = turtle.Screen()
finestra.screensize(600, 400)
finestra.title("TurtleMaze")

# these are the lists that contain the info to mark the lines

lo = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],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 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
      [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
      [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]




# i call the function to generate the maze

turtle.tracer(0)
mark_maze(lo, lv, -200, 300)
turtle.update()
turtle.tracer(1)

# now the movement section

starting_pos = (63, -162)

red_line = turtle.Turtle()
red_line.hideturtle()
red_line.speed(0)
red_line.up()
red_line.setposition(starting_pos)
red_line.color("red")
red_line.up()
red_line.down()

def forward(turt):
    for i in range(11):
        turt.forward(1)
        if any(pixel.distance(turt) < 5 for pixel in pixels):
            for i in range(i):
                turt.undo()
            break
def dx():
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)

def sx():

    red_line.right(180)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(-180)

def down():
    red_line.right(90)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(-90)

def up():
    red_line.right(-90)
    red_line.speed(5)
    forward(red_line)
    red_line.speed(0)
    red_line.right(90)

# to make the turtle go back i made some separated commands to keep it as easy as possible

def undo_sx():
    red_line.undo()
    red_line.undo()
    red_line.undo()



def undo_dx():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    


def undo_down():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def undo_up():
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()
    red_line.undo()


def close():
    finestra.bye()


finestra.onkey(dx, "Right")
finestra.onkey(sx, "Left")
finestra.onkey(up, "Up")
finestra.onkey(down, "Down")
finestra.onkey(undo_dx, "d")
finestra.onkey(undo_sx, "a")
finestra.onkey(undo_down, "s")
finestra.onkey(undo_up, "w")
finestra.onkey(close, "q")

finestra.listen()
finestra.mainloop()

Output:

enter image description here

Observe how I keep attempting to "cheat", but the walls won't let me.

Ann Zen
  • 17,892
  • 6
  • 20
  • 39
  • So, if i am understanding... I should create a turtle for each line and apply somehow the function you posted... am I right? Thank you so much by the way – MarcoMac01 Jan 24 '21 at 00:21
  • @MarcoMac01 yeah, maybe multiple turtles per line – Ann Zen Jan 24 '21 at 00:22
  • It would be too much to put in a function I think. Too difficult for me maybe. I'll try to do some test tomorrow. To apply this reckoning I should create a single turtle per pixel then work with the spacing between each turtle. I am just thinking, for sure I am wrong. – MarcoMac01 Jan 24 '21 at 00:28
  • 1
    @MarcoMac01 Keep in mind that doing `turtle.shape("square")` and `turtle.shapesize(21 / 20, 1 / 20)` makes a wall. – Ann Zen Jan 24 '21 at 00:35
  • i should do lots of single lines... too much time lost i think. – MarcoMac01 Jan 24 '21 at 18:28
  • @MarcoMac01 Okay, I implemented my solution into your code. – Ann Zen Jan 24 '21 at 20:50
  • OMG, YOU ARE AMAZING... but i need to study in depth the code and functions to understand better what happens there. i do not know why the undo moves do not work... but you have done a pretty good job so far. congrats – MarcoMac01 Jan 25 '21 at 00:18
  • @MarcoMac01 I'm glad I could help. But can't you elaborate on how the `undo` function doesn't work? It seems to work fine on my end. – Ann Zen Jan 25 '21 at 00:48
  • i don't know... i run the code on pycharm community edition. what do you use to run the code? – MarcoMac01 Jan 26 '21 at 21:36
  • i am happy to hear that... i always knew that the coding community was full of helpful people like you. – MarcoMac01 Jan 26 '21 at 21:37
  • @MarcoMac01 I used IDLE. When you say "do not work", do you mean it malfunctions, or nothing happens when you press the asdw keys? – Ann Zen Jan 26 '21 at 21:54
  • @MarcoMac01 oh, by the way, an upvote on my post would be a great "thank you" :) – Ann Zen Jan 26 '21 at 21:56
0

I like it. The ASCII art is cool. Yea it's do'able, but you have to divide the turtle position by whatever spacing is used, to achieve position in your level data, then compare for collision.

I've gotten somewhere with it, but one of your wall lists is rotated by 90 degrees, so it's wrecking my brain. I'll post what I have so far, so you can look at it. Tired, and it would take another day for me to get it running. Gonna grab some tea, watch a couple youtubes, then pass out. Might glance at it again tomorrow.


turtle.cfg

Tried to resize window with this. There's a way to do it, but for whatever reason it isn't.

width = 0.5
height = 0.75
leftright = None
topbottom = None
canvwidth = 700
canvheight = 500
mode = standard
colormode = 1.0
delay = 0
undobuffersize = 1000
shape = classic
pencolor = black
fillcolor = black
resizemode = noresize
visible = True
language = english
exampleturtle = turtle
examplescreen = screen
title = TurtleMaze
using_IDLE = False

#! /usr/bin/python3

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##  docs.python.org/3/library/t.html

import turtle as t
import math

##  create a finestra = window
finestra = t .Screen()

width = 700
height = 500

half_width = math .floor( width /2 )
half_height = math .floor( height /2 )

finestra .screensize( width, height )
finestra .title('TurtleMaze')

##  t.setheading() or t.seth()
East = 0
North = 90
West = 180
South = 270

red_line = t .Turtle()
red_line .hideturtle()
red_line .up()
red_line .speed('fastest')
red_line .color('red')

line_h = t .Turtle()
line_h .hideturtle()
line_h .seth( East )
line_h .up()
line_h .speed('fastest')

line_v = t .Turtle()
line_v .hideturtle()
line_v .seth( South )
line_v .up()
line_v .speed('fastest')

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##  draw walls by referencing lists.

def mark_horizontal_line( row, x, y ):
    line_h .up()
    line_h .setposition( x, y )
    for r in row:
        if r == 0:  line_h .up()
        else:  line_h .down()
        line_h .forward( horiz_spacing )


def create_horiz_lines( lo, x, y ):
    step_y = y
    for n in lo:
        mark_horizontal_line( n, x, step_y )
        step_y -= vert_spacing  ##  go down


def mark_line_vertical( column, x, y ):
    line_v .up()
    line_v .setposition( x, y )
    for c in column:
        if c == 0:  line_v .up()
        else:  line_v .down()
        line_v .forward( vert_spacing )


def create_vert_lines( lv, x, y ):
    step_dx = x
    for n in lv:
        mark_line_vertical( n, step_dx, y )
        step_dx += horiz_spacing  ##  go right

##  generate entire maze by attaching all the others above

def mark_maze( lo, lv, x, y ):
    create_horiz_lines( lo, x, y )
    create_vert_lines( lv, x, y )


##  horizontal segment data:  23 vert x 25 horiz

lo = [  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
        [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],
        [0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0],
        [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
        [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
        [1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0],
        [0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0],
        [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0],
        [1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0],
        [0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0],
        [0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0],
        [0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
        [1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1],
        [0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0],
        [1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ]

##  vertical segment data:  26 vert x 22 horiz   rotated by 90*

lv = [  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1],
        [0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
        [1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0],
        [1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0],
        [0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0],
        [1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
        [0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0],
        [1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
        [1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0],
        [0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1],
        [1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0],
        [1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1],
        [0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0],
        [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
        [0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
        [0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ]

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

horiz_spacing = width /len( lo[0] )
vert_spacing = height /len( lv[0] )

horiz_walk = horiz_spacing /3
vert_walk = vert_spacing /3

##  generate maze
mark_maze( lo, lv, -half_width, half_height )

##  now the movement section
starting_pos = ( 0, -half_height )

red_line .setposition( starting_pos )
red_line .seth( North )
red_line .showturtle()
red_line .down()

def up():
    xx = round( (half_width +red_line .xcor()) /horiz_spacing )
    yy = round( (half_height +red_line .ycor() -vert_walk) /vert_spacing )
    red_line .seth( North )
    print( xx, yy )
    if lo[-yy][xx] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def down():
    xx = round( (half_width +red_line .xcor()) /horiz_spacing )
    yy = round( (half_height +red_line .ycor() +vert_walk) /vert_spacing )
    red_line .seth( South )
    print( xx, yy )
    if lo[-yy][xx] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def left():
    xx = round( (half_width +red_line .xcor() -horiz_walk) /horiz_spacing )
    yy = round( (half_height +red_line .ycor()) /vert_spacing )
    red_line .seth( West )
    print( xx, yy )
    if lv[xx][yy] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )


def right():
    xx = round( (half_width +red_line .xcor() +vert_walk) /horiz_spacing )
    yy = round( (half_height +red_line .ycor()) /vert_spacing )
    red_line .seth( East )
    print( xx, yy )
    if lv[xx][yy] == 0:
        red_line .forward( vert_walk )
    else:
        finestra .bgcolor( 'orange' )
        red_line .speed( 'slow' )
        red_line .forward( vert_walk )
        red_line .undo()
        finestra .bgcolor( 'white' )

def undo():
    red_line .undo()

def quit():
    finestra .bye()

finestra .onkey( up, 'Up' )
finestra .onkey( down, 'Down' )
finestra .onkey( left, 'Left' )
finestra .onkey( right, 'Right' )

finestra .onkey( undo, 'space' )
finestra .onkey( quit, 'q' )

finestra .listen()
finestra .mainloop()
Doyousketch2
  • 796
  • 4
  • 11
  • Yea, thanks. I am so excited to see what could solve the problem. Thank you so much for everything – MarcoMac01 Jan 24 '21 at 00:22
  • The turtle git stuck in a wall, and I managed to wriggle it out... the turtle ending up on the other side of the wall. – Ann Zen Jan 24 '21 at 00:34
-1

Actually sorry to disappoint you, but i don't think this is possible using turtle. If you want the turtle to collide with the walls, you need to implement collision detection and for that you need the co-ordinates of the walls, which you don't have. Maybe you can make a snake game using pygame module.