Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5. delay = 0.08
  6. speed = 10
  7.  
  8. screen = turtle.Screen()
  9. screen.bgcolor("black")
  10. screen.setup(width=600, height=600)
  11. screen.tracer(0)
  12.  
  13. def drawCharacter(colour, shape):
  14. character = turtle.Turtle()
  15. character.penup()
  16. character.speed(0)
  17. character.shape(shape)
  18. character.color(colour)
  19. return character
  20.  
  21. head = drawCharacter("green", "square")
  22. head.direction = "stop"
  23. head.goto(0, 0)
  24.  
  25. food = drawCharacter("yellow", "circle")
  26. food.goto(0, 100)
  27. snakeBody = []
  28.  
  29. #-----------------------------------------Exercise 5.A Start--------------------------#
  30.  
  31.  
  32. #---------------------------------------- Exercise 5.A End----------------------------#
  33.  
  34. #--------------Exercise 2 Start--------------------------------------------------------#
  35. #Pass is just a placeholder so the program can still run before you write your code
  36. def move():
  37. # A function to move the turtle
  38. if head.direction == "up":
  39. pass
  40. elif head.direction == "down":
  41. pass
  42. elif head.direction == "left":
  43. pass
  44. elif head.direction == "right":
  45. pass
  46. #--------------Exercise 2 End-----------------------------------------------------#
  47.  
  48. #--------------Exercise 3 Start--------------------------------------------------------#
  49. def goUp():
  50. pass
  51.  
  52. def goDown():
  53. pass
  54.  
  55. def goLeft():
  56. pass
  57.  
  58. def goRight():
  59. pass
  60.  
  61. #--------------Exercise 3 End----------------------------------------------------------#
  62.  
  63. def gameStop():
  64. time.sleep(1)
  65. head.goto(0, 0)
  66. head.direction = "stop"
  67. for part in snakeBody:
  68. part.goto(1000, 1000)
  69. # Send all the body parts off screen before clearing them
  70. snakeBody.clear()
  71. # This will clear all the turtles in the "snakeBody" list.
  72. # Resetting the score (Something might need to be put here :D)
  73.  
  74. # When the player presses a button, this will run a particular function depending
  75. # on which button is pressed
  76. screen.listen()
  77. # Notice that this "screen" variable is the same one used at the start
  78. screen.onkeypress(goUp, 'w')
  79. screen.onkeypress(goDown, 's')
  80. screen.onkeypress(goLeft, 'a')
  81. screen.onkeypress(goRight, 'd')
  82.  
  83.  
  84. while True:
  85. screen.update()
  86. # Every iteration of this loop will update the screen. Since this loop is
  87. # always running, the screen will keep updating
  88. time.sleep(delay)
  89. # This will stop the program for "delay" seconds. What is "delay"?
  90.  
  91. #--------------------------------------Exercise 4 Start----------------------------------#
  92.  
  93.  
  94.  
  95.  
  96. #--------------------------------------Exercise 4 End----------------------------------#
  97. #Managing the movement of the food
  98. # First we need to see if the snake has made contact with the food
  99. if head.distance(food) < 20: # Each turtle is 20 pixels wide. The "distance()"
  100. # function compares the centres of each turtle.
  101. x = random.randint(-290, 290)
  102. y = random.randint(-290, 290)
  103. # Generating random x and y co-ordinates for the food
  104. food.goto(x, y)
  105.  
  106. # PART 8 CONT.
  107. # Creating the new body part
  108. newBodyPart = drawCharacter("white", "square")
  109. newBodyPart.penup()
  110.  
  111. # Now we need to add the new body part onto the rest of the snake
  112. snakeBody.append(newBodyPart)
  113. # We want the snake body parts to follow the head of the snake as it moves
  114. for index in range(len(snakeBody) - 1, 0, -1): # Note that index 0 is not included in this loop:
  115. # this is intentional
  116. # Send each body part to follow the one in front of it, starting from the tail
  117. x = snakeBody[index - 1].xcor()
  118. y = snakeBody[index - 1].ycor()
  119. snakeBody[index].goto(x, y)
  120. # Since the for loop doesn't include the "0th" body part, we will deal with it here
  121. if len(snakeBody) > 0: # We have to make sure a body part other than the head exists first
  122. x = head.xcor()
  123. y = head.ycor()
  124. snakeBody[0].goto(x, y)
  125. move()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement