Trying to force turtle back to beginning of maze after collides with wall

B

baujacob

Hi everyone, I'm trying to create a simple maze program. When the user finishes the maze, I want to print in big letters "You Win!" and when the user hits a wall, I want the user to go back to the beginning of the maze. The problem is "collision detection" which is an advanced topic and I'm only in a beginning programming class. Is there anyway I can force the user back tothe starting point when the turtle hits the wall? Thank you in advance.

I've tried writing:
if userTurtle.xcor(-30) and userTurtle.ycor(60):
userTurtle.goto(-30,180)
userTurtle.setheading(-90)

and

if userTurtle.pos(-30,60):
userTurtle.goto(-30,180)
userTurtle.setheading(-90)

So basically I have one turtle that draws the maze at the beginning and then another turtle(userTurtle) that completes the maze. When the userTurtle hits any point on the wall, I want to force userTurtle back to the start of the maze.
Here it is:

import turtle
userTurtle = turtle.Turtle()
draw = turtle.Turtle()
scr = turtle.Screen()

def drawMaze():
draw.pencolor("gold")
draw.pensize(3)
draw.penup()
draw.goto(0,-180)
draw.pendown()
draw.speed(10)
draw.setheading(180)
draw.fd(180)
draw.setheading(90)
draw.fd(60)
draw.setheading(0)
draw.fd(120)
draw.backward(120)
draw.setheading(90)
draw.fd(300)
draw.setheading(0)
draw.fd(120)
draw.setheading(-90)
draw.fd(120)
draw.setheading(180)
draw.fd(60)
draw.setheading(90)
draw.fd(60)
draw.setheading(-90)
draw.fd(120)
draw.setheading(90)
draw.fd(60)
draw.setheading(0)
draw.fd(120)
draw.setheading(-90)
draw.fd(60)
draw.setheading(0)
draw.fd(60)
draw.setheading(-90)
draw.fd(60)
draw.backward(60)
draw.setheading(0)
draw.fd(60)
draw.setheading(90)
draw.fd(60)
draw.penup()
draw.setheading(180)
draw.fd(60)
draw.pendown()
draw.setheading(90)
draw.fd(60)
draw.setheading(180)
draw.fd(60)
draw.setheading(90)
draw.fd(60)
draw.setheading(0)
draw.fd(120)
draw.setheading(-90)
draw.fd(60)
draw.backward(60)
draw.setheading(0)
draw.fd(60)
draw.setheading(-90)
draw.fd(240)
draw.setheading(180)
draw.fd(60)
draw.setheading(-90)
draw.fd(60)
draw.setheading(180)
draw.fd(120)
draw.setheading(90)
draw.fd(60)
draw.setheading(180)
draw.fd(60)
draw.setheading(90)
draw.fd(60)
draw.backward(60)
draw.setheading(180)
draw.fd(60)
draw.penup()
draw.setheading(0)
draw.fd(300)
draw.pendown()
draw.setheading(-90)
draw.fd(120)
draw.setheading(180)
draw.fd(120)
draw.ht()

userTurtle.penup()
userTurtle.goto(-30,180)
userTurtle.setheading(-90)

def mazeGame():
scr.bgcolor("#0070ff")

def m1():
userTurtle.setheading(90)
userTurtle.fd(30)
userTurtle.pos()
print(userTurtle.pos())

def m2():
userTurtle.setheading(180)
userTurtle.fd(30)
userTurtle.pos()
print(userTurtle.pos())

def m3():
userTurtle.setheading(360)
userTurtle.fd(30)
userTurtle.pos()
print(userTurtle.pos())

def m4():
userTurtle.setheading(-90)
userTurtle.fd(30)
userTurtle.pos()
print(userTurtle.pos())

scr.onkeypress(m1, "Up")
scr.onkeypress(m2, "Left")
scr.onkeypress(m3, "Right")
scr.onkeypress(m4, "Down")

scr.listen()

drawMaze()
mazeGame()
 
D

Denis McMahon

Hi everyone, I'm trying to create a simple maze program. When the user
finishes the maze, I want to print in big letters "You Win!" and when
the user hits a wall, I want the user to go back to the beginning of the
maze. The problem is "collision detection" which is an advanced topic
and I'm only in a beginning programming class. Is there anyway I can
force the user back to the starting point when the turtle hits the wall?

OK

My first observation is that "maze" could be an array of "rooms" each of
which starts life with 4 exits.

Generating the maze would involve turning exits into walls.

You could create a set maze, or you could randomly generate a maze at the
start of each "game".

Once you have created a maze, define your start and end points.

If you want to apply your logic, then if the "solver" turtle enters a
"room" with only one exit, it is in a dead end.

The "solver" turtle would probably need to store which paths led to dead
ends to ensure it did not follow them again. This extends back to any
exit that only leads to dead ends.

Let's consider a simple maze, 2x2 so 4 rooms. Room 0 is the top left (nw)
room, and rooms are numbered across, down. Each room number has 4 exit
directions, nesw. 'x' is an external exit, a number is an internal room,
None is no exit (ie a wall).

maze = { 0: { 'n': 'x', 'e': 1, 's': None, w: None },
1: { 'n': None, 'e': None, 's': 3, w: 0 },
2: { 'n': None, 'e': 3, 's': 'x', w: None },
3: { 'n': 1, 'e': None, 's': None, w: 2 } }

Room 0 (nw) has an exit (or entrance) to the north and an internal path
to the east (rm 1).

Room 1 (ne) has internal paths to the west (rm 0) and south (rm 3).

Room 2 (sw) has an entrance (or exit) to the south and an internal path
to the east (rm 3).

Room 3 (se) has internal links to the north (rm 1) and west (rm 2).

The data structure shown (a dictionary of dictionaries) is just one way
of holding the maze data, and perhaps not the most elegant. The maze
could be stored as a dictionary of tuples thus:

maze = { 0: ( 'x', 1, None, None ),
1: ( None, None, 3, 0 ),
2: ( None, 3, 'x', None ),
3: ( 1, None, None, 2 ) }

Drawing such a maze graphically would require allocating co-ordinates in
some drawing space to the vertices of each room, and drawing lines along
the edges that represent walls.

Navigating such a maze graphically would require allocating co-ordinates
in the same drawing space to the centres of the rooms, and moving between
adjacent room centres provided the exit was "open".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top