Python Forum
School Work Assignment: Snake Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
School Work Assignment: Snake Game
#1
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.


# 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()
Reply
#2
elif is a syntax error there, because you have a few parentheses opened on the previous line, and not all of them were closed. elif is a statement, and statements are not allowed inside expressions, so having an elif inside parentheses is a syntax error.

...and once you fix that, the next few lines will have very similar errors.
Reply
#3
moved thread back into homework section so the user can see it
Recommended Tutorials:
Reply
#4
hello nilamo thank you for the help, I made corrections based on the things you said,the updated code is below. But when I hit the run button it opened and closed immediately if you could offer any help that would be wonderful.

# 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)

running = False
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


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()
            create_food()
    screen.ontimer(update, 350)


def startgame():
    global running
    running = True
    createbody(0, 0)
    create_food()
    update()


screen.onkey(u, "Up")
screen.onkey(d, "Down")
screen.onkey(l, "Left")
screen.onkey(r, "Right")
screen.listen()

startgame()
Reply
#5
https://docs.python.org/3/library/turtle...e.mainloop

I don't know very much about turtle, but I assume at some point you need to tell it to start.
Reply
#6
Actually it seems, you just need to add
turtle.done()
at the end of your code and it runs OK!(at least for me)
Ups just noticed that I wrote basically the same thing as @nilamo! sorry for that :(
Reply
#7
Thank you @nilamo and @mlieqofor all the help, I have the snake game running well now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame Snake Game Help Please Beginnerprogrammer6 0 1,375 Jan-15-2023, 09:45 PM
Last Post: Beginnerprogrammer6
  Need help with my assignment please - game of Solitaire Muzz 6 6,637 Sep-28-2021, 07:36 PM
Last Post: Underscore
  Need some help with Dice Game for daughter in school OptX 2 1,941 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  Assignment to make a multiple choice game blacklight 1 2,167 Aug-14-2020, 02:37 AM
Last Post: Larz60+
  Need help with a tough school assignment datson79 5 3,423 Feb-18-2019, 06:01 PM
Last Post: ichabod801
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,576 May-17-2018, 05:39 PM
Last Post: j.crater
  Array to get profit (school assignment) harryvandervelden 2 2,834 Nov-28-2017, 05:48 PM
Last Post: Larz60+
  Small game (school project) Ganesh 7 5,667 Nov-08-2017, 09:04 PM
Last Post: Ganesh
  School assignment Ronaldo 5 9,109 Jan-08-2017, 11:57 AM
Last Post: Ofnuts
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,859 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020