Advertisement
Guest User

Untitled

a guest
Jan 25th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5. delay = 0.1
  6.  
  7. wn = turtle.Screen()
  8. wn.title("My first Snake game!")
  9. wn.bgcolor("blue")
  10. wn.setup(width=600, height=600)
  11. wn.tracer(0)
  12. #Note: wn.tracer turns off the animations/screen updates
  13. #Note:This is a setup for the screen so mess around with the colors, width, height!
  14.  
  15. #Snake head time!
  16. head = turtle.Turtle()
  17. head.speed( 0)
  18. #note: head.speed is the speed of the animation
  19. head.shape("square")
  20. head.color("yellow")
  21. head.penup()
  22. #Note: Penup is for writing on the screen thats why i didnt put anything
  23. head.goto(0,0)
  24. #Note: head.goto is the location
  25. head.direction = "stop"
  26.  
  27. #Snake food
  28. food = turtle.Turtle()
  29. food.speed( 0)
  30. food.shape("circle")
  31. food.color("red")
  32. food.penup()
  33. food.goto(0,100)
  34.  
  35. segments = []
  36.  
  37. # Functions
  38. def go_up():
  39. head.direction = "up"
  40.  
  41. def go_down():
  42. head.direction = "down"
  43.  
  44. def go_right():
  45. head.direction = "right"
  46.  
  47. def go_left():
  48. head.direction = "left"
  49.  
  50. # Functions
  51. def move():
  52. if head.direction == "up":
  53. y = head.ycor()
  54. head.sety(y + 20) #When I call a move function if the head direction is up it will move 20 pixels
  55. if head.direction == "down":
  56. y = head.ycor()
  57. head.sety(y - 20)
  58. if head.direction == "right":
  59. x = head.xcor()
  60. head.setx(x + 20)
  61. if head.direction == "left":
  62. x = head.xcor()
  63. head.setx(x - 20) #This code can also work to reduce one line. The code is head.sety(head.xcor() - 20)
  64. #Important: if it left/right its x and up and down is y so you'll need to change it from head.ycor to head.xcor, aswell as head.sety to head.setx
  65.  
  66. # Keybinds
  67. wn.listen()
  68. wn.onkeypress(go_up, "w") #note: at line 29, it is go_up() while its (go_up,. dont make that mistake or it will not run.
  69. wn.onkeypress(go_down, "s")
  70. wn.onkeypress(go_right, "d")
  71. wn.onkeypress(go_left, "a")
  72.  
  73.  
  74. #Main game loop
  75. while True:
  76. wn.update() #This updates the screen so our snake can move
  77.  
  78. # Check for a collision with the food
  79. if head.distance(food) < 20:
  80. #Move the food to a random spot
  81. x = random.randint(-290, 290)
  82. y = random.randint(-290, 290)
  83. food.goto(x,y)
  84.  
  85. # Add a segemnt
  86. new_segment = turtle.Turtle()
  87. new_segment.speed(0)
  88. new_segment.shape("square")
  89. new_segment.color("grey")
  90. new_segment.penup()
  91. segments.append(new_segment)
  92.  
  93. # Move the end segments first
  94. for index in range(len(segments)-1, 0, -1)
  95. x = segments[index-1].xcor()
  96. y = segments[index-1].ycor()
  97. segments[index].goto(x, y)
  98.  
  99. # Move segment 0 to where the head is
  100. if len(segments > 0:)
  101. x = head.xcor()
  102. y = head.ycor()
  103. segments[0].goto(x, y)
  104.  
  105. move()
  106.  
  107. time.sleep(delay)
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. wn.mainloop()
  116. #Note: wn.mainloop() keeps the window open for us
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement