# Simple labyrinth

I'm making a a simple labyrinth. I want to make it so that I could hit a wall, and if I did hit the wall, I wouldn't cross it.

from turtle import *
from time import *

speed(0)

for a in range(0,401,20):
sety(a);
forward(400);
#write(a);
forward(-400);

home()
left(90)

for a in range(0,401,20):
setx(a)
forward(400)
backward(400)
home()

map=[
'####################',
'#       #          #',
'# ####  ### # #### #',
'# #       # # #  # #',
'# #### #### #  # # #',
'#      #    # #    #',
'###### # #### ## # #',
'#    # #         # #',
'# #### ####### ### #',
'#            # #   #',
'# #  ##### ### # # #',
'# #  #   #         #',
'# #  #   # #### ## #',
'# ## ##### #  # ## #',
'#             #    #',
'# ########## ### # #',
'#   #      #     # #',
'### #### # ####### #',
'#S       #       # #',
'####################'

]

delay(0)
home(); sety(400)

for row in map:
for cell in row:
if cell == ' ': color('black', 'black')
if cell == '~': color('black', 'red')
if cell == '#':
begin_fill()
for x in range(4):
forward(20)
right(90)
end_fill()
if cell=='S' or cell=='F':
if cell=='S':
color('green')
else:
color('yellow')
right(90)
forward(20)
left(90)
write(cell, font=("Arial", 14))
left(90)
forward(20)
right(90)
forward(20)
forward(-400)
right(90); forward(20); left(90)

goto(20,20)
color('black', 'green')
begin_fill()
for i in range(4):
forward(20); left(90)
end_fill()
write('S', font=("Arial", 14))
hideturtle()

setx(160)
color('black', 'yellow')
begin_fill()
for i in range(4):
forward(20); left(90)
end_fill()
write('F', font=("Arial", 14))
hideturtle()

hero=Turtle()
hero.color('pink')
hero.penup()
hero.speed(0)
hero.goto(50,30)
screen=getscreen()
screen.listen()

hero.row=8
hero.col=2

def W():
hero.clear()
hero.forward(20)
hero.row-=1
react_to_cell()
cell = map[hero.row][hero.col]
if cell == '#':
hero.forward(-20)
hero.row+=1

def S():
hero.clear()
hero.forward(20)
hero.row+=1
react_to_cell()
cell = map[hero.row][hero.col]
if cell == '#':
hero.forward(-20)
hero.row-=1

def A():
hero.clear()
hero.forward(20)
hero.col-=1
react_to_cell()
cell = map[hero.row][hero.col]
if cell == '#':
hero.forward(-20)
hero.col+=1

def D():
hero.clear()
hero.forward(20)
hero.row+=1
react_to_cell()
cell = map[hero.row][hero.col]
if cell == '#':
hero.forward(-20)
hero.row-=1

def react_to_cell():
cell=map[hero.row][hero.col]
if cell==' ':
hero.write('čia negalima eiti', font=("Arial", 14))
hero.goto(50,30)
hero.row=18
hero.col=1
elif cell=='F':
hero.color('green')
color('red')
goto(-120,0)
write(u'Finišas', font=("Arial", 20, 'bold'))

screen.onkey(W,'Up')
screen.onkey(S,'Down')
screen.onkey(A,'Left')
screen.onkey(D,'Right')

• Welcome to Code Review! I'm not sure why you tried to turn your code into a JavaScript snippet, but that doesn't do much for Python. We fixed it up for you, I hope you get some great answers.
– Mast
Apr 2 '16 at 12:46
• @Mast i did no try to run it on jacascript, i just pasted it in it, because if i would have pasted it in just the field of the text, it would have been all messed up. Apr 2 '16 at 13:35
• To make something a code block, highlight it and hit Ctrl-k. Apr 2 '16 at 14:14

I would highly recommend that you read PEP 8, the Python style guide.

There are a couple rules you are breaking.

From the Imports section:

Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

You should instead use import turtle.

From the Indentation section:

Use 4 spaces per indentation level.

From the Function Names section:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

From the Other Recommendations section:

Compound statements (multiple statements on the same line) are generally discouraged.

Yes:

if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()


Rather not:

if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()


Also from that section:

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -=, etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).

I just had to get that over with. Now for my comments:

You have some foreign characters in your file. You should probably put # -*- encoding: UTF-8 -*- at the beginning of the file so that Python knows how to interpret them.

In Python2, you can use either a new line or a semicolon to mark the end of a command. Using both is redundant.

for a in range(0,401,20):


Why 20? Why 401? You should define a constant at the beginning of the file:

CELL_SIZE = 20


Then, draw the grid after you have defined the grid:

grid = [
...
]

for i, row in enumerate(grid):
turtle.sety(i * CELL_SIZE)
turtle.forward(len(row) * CELL_SIZE)
turtle.backward(len(row) * CELL_SIZE)

for i, column in enumerate(zip(*grid)):
turtle.setx(i * CELL_SIZE)
turtle.forward(len(column) * CELL_SIZE)
turtle.backward(len(column) * CELL_SIZE)


The reason for all this: you can easily change the size of the grid or the size of the cells. The way I have it written above, you shouldn't even need to have a rectangular grid, let alone a square one. You would also need to change forward(20) to forward(CELL_SIZE) and goto(20, 20) to goto(CELL_SIZE, CELL_SIZE). I also changed forward(-400) to backward(400). That makes a little more sense, doesn't it? One more thing that I changed: map is a bad variable name because it shadows the built-in function. If you were to try to use that function ... you'd get list object is not callable. That might be confusing. It's better to stick with names that have not already been used.

#write(a)


If you aren't using that code any more, take it out. If it was there for debugging purposes, you should have put #DEBUG at the end of the line. That way, you can search for DEBUG once you think you are done, and remove all of those lines.

for x in range(4):
forward(20)
right(90)


It is generally a good idea to use _ as a variable name if you aren't using the variable. It just makes it a little more obvious. One more thing: how did you decide on 4? You shouldn't use magic numbers like that.

if cell=='S' or cell=='F':
if cell=='S':
color('green')
else:
color('yellow')


Instead of using an if statement to check two conditions and then re-check the conditions, use elif:

if cell == 'S':
turtle.color('green')
elif cell == 'F':
turtle.color('yellow')

goto(20, 20)


Any occurrences of goto() should be something like goto(HOME[0] + ..., HOME[1] + ...). You would, of course, define HOME higher up. As it is, it's difficult for me to place your grid where I want it. It's up in the corner for me.

hero.row = 8
hero.col = 2


I can see why the column would be two, but why is the row eight? Shouldn't it be eighteen to be the second to last row? If that is what you meant, do hero.row = len(grid) - 2.




You never started the main loop. You should have turtle.mainloop() at the end.