Apr-25-2018, 07:45 PM
Hello i'm working on a schoolwork assignment, the code below is for a snake game. I am running this in a PyCharm. When I run the code in PyCharm I get the following error on line 56.
elif (dir == "Down"):
^
SyntaxError: invalid syntax.
If anyone could help or offer tips with the error, that would be great. Also if you see any other errors feel free to tell me about those too.
elif (dir == "Down"):
^
SyntaxError: invalid syntax.
If anyone could help or offer tips with the error, that would be great. Also if you see any other errors feel free to tell me about those too.
# Global Settings import turtle import random screen = turtle.Screen() screen.bgcolor("lightgreen") sprite = turtle.Turtle() sprite.penup() sprite.speed(0) sprite.shape("square") sprite.goto(-1000 ,1000) dir = "Right" food = None snake = [] def u(): global dir if(not dir == "Down"): dir = "Up" def d(): global dir if(not dir == "Up"): dir = "Down" def l(): global dir if(not dir == "Right"): dir = "Left" def r(): global dir if(not dir == "Left"): dir = "Right" def createBody(x,y): body = sprite.clone() body.goto(x,y) snake.append(body) def move(): last = snake[len(snake)-1] first = snake[0] x = first.xcor() y = first.ycor() size = 22 if (dir == "Right"): last.goto((x + (size)), y) elif (dir == "Left"): last.goto((x - (size)), y) elif (dir == "Up"): last.goto((x,y + (size)) elif (dir == "Down"): last.goto((x,y - (size)) else:last.goto(x, y - (size)) snake.insert(0, last) snake.pop() def create_Food(): global food food = sprite.clone() # create the food object food.color("red") # change it to red randX = random.randint(-8,8) * 22 # find a random location randY = random.randint(-8,8) * 22 food.goto(randX, randY) # go to the location running = False def update(): if running: move() first = snake[0] x = first.xcor() y = first.ycor() if(x == food.xcor() and y == food.ycor()): createBody(x, y) food.hideturtle() createFood() screen.ontimer(update, 350) def startGame(): global running running = True createBody(0,0) createFood() update() screen.onkey(u, "Up") screen.onkey(d, "Down") screen.onkey(l, "Left") screen.onkey(r, "Right") screen.listen() startGame()