#๐ Snake not moving in python game
10 messages ยท Page 1 of 1 (latest)
@faint mulch
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
import turtle
#set up the screen
wn = turtle.Screen()
wn.title("snake game by @karabo")
wn.bgcolor("green")
wn.setup(width = 600, height = 600)
wn.tracer(0)
#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.penup()
head.goto(0,0)
head.direction = "stop"
#functions
def go_up():
head.direction ="up"
def go_down():
head.direction = "down"
def go_left():
head.direction = "left"
def go_right():
head.direction = "right"
def move():
if head.direction =="up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycore()
head.sety(y-20)
if head.direction == "down":
y = head.ycore()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.stx(x + 20)
# keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")
#game loop
while True :
wn.update()
move()
time.sleep(delay)
wn.mainloop()
Pls help guys
!indent it seems from go_right you just increase the indentation - and the keyboard listener ends up in multiple-nested function, meaning it doesn't get run
Also the game loop doesn't contain the move and sleep, just update
Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.
Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.
Example
def foo():
bar = 'baz' # indented one level
if bar == 'baz':
print('ham') # indented two levels
return bar # indented one level
The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the if statement, and will only run if the if statement evaluates to True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the if statement above as no more lines can be inside the if statement below that line.
Indentation is used after:
1. Compound statements (eg. if, while, for, try, with, def, class, and their counterparts)
2. Continuation lines
More Info
1. Indentation style guide
2. Tabs or Spaces?
3. Official docs on indentation
Please read what I said and the embed. You sent the exact same code, with exact same errors I addressed
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.