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],
[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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 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()