Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- import random
- delay = 0.08
- speed = 10
- screen = turtle.Screen()
- screen.bgcolor("black")
- screen.setup(width=600, height=600)
- screen.tracer(0)
- def drawCharacter(colour, shape):
- character = turtle.Turtle()
- character.penup()
- character.speed(0)
- character.shape(shape)
- character.color(colour)
- return character
- head = drawCharacter("green", "square")
- head.direction = "stop"
- head.goto(0, 0)
- food = drawCharacter("yellow", "circle")
- food.goto(0, 100)
- snakeBody = []
- #-----------------------------------------Exercise 5.A Start--------------------------#
- #---------------------------------------- Exercise 5.A End----------------------------#
- #--------------Exercise 2 Start--------------------------------------------------------#
- #Pass is just a placeholder so the program can still run before you write your code
- def move():
- # A function to move the turtle
- if head.direction == "up":
- pass
- elif head.direction == "down":
- pass
- elif head.direction == "left":
- pass
- elif head.direction == "right":
- pass
- #--------------Exercise 2 End-----------------------------------------------------#
- #--------------Exercise 3 Start--------------------------------------------------------#
- def goUp():
- pass
- def goDown():
- pass
- def goLeft():
- pass
- def goRight():
- pass
- #--------------Exercise 3 End----------------------------------------------------------#
- def gameStop():
- time.sleep(1)
- head.goto(0, 0)
- head.direction = "stop"
- for part in snakeBody:
- part.goto(1000, 1000)
- # Send all the body parts off screen before clearing them
- snakeBody.clear()
- # This will clear all the turtles in the "snakeBody" list.
- # Resetting the score (Something might need to be put here :D)
- # When the player presses a button, this will run a particular function depending
- # on which button is pressed
- screen.listen()
- # Notice that this "screen" variable is the same one used at the start
- screen.onkeypress(goUp, 'w')
- screen.onkeypress(goDown, 's')
- screen.onkeypress(goLeft, 'a')
- screen.onkeypress(goRight, 'd')
- while True:
- screen.update()
- # Every iteration of this loop will update the screen. Since this loop is
- # always running, the screen will keep updating
- time.sleep(delay)
- # This will stop the program for "delay" seconds. What is "delay"?
- #--------------------------------------Exercise 4 Start----------------------------------#
- #--------------------------------------Exercise 4 End----------------------------------#
- #Managing the movement of the food
- # First we need to see if the snake has made contact with the food
- if head.distance(food) < 20: # Each turtle is 20 pixels wide. The "distance()"
- # function compares the centres of each turtle.
- x = random.randint(-290, 290)
- y = random.randint(-290, 290)
- # Generating random x and y co-ordinates for the food
- food.goto(x, y)
- # PART 8 CONT.
- # Creating the new body part
- newBodyPart = drawCharacter("white", "square")
- newBodyPart.penup()
- # Now we need to add the new body part onto the rest of the snake
- snakeBody.append(newBodyPart)
- # We want the snake body parts to follow the head of the snake as it moves
- for index in range(len(snakeBody) - 1, 0, -1): # Note that index 0 is not included in this loop:
- # this is intentional
- # Send each body part to follow the one in front of it, starting from the tail
- x = snakeBody[index - 1].xcor()
- y = snakeBody[index - 1].ycor()
- snakeBody[index].goto(x, y)
- # Since the for loop doesn't include the "0th" body part, we will deal with it here
- if len(snakeBody) > 0: # We have to make sure a body part other than the head exists first
- x = head.xcor()
- y = head.ycor()
- snakeBody[0].goto(x, y)
- move()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement