Advertisement
JkSoftware

main.py

Dec 5th, 2021
592
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. from turtle import Screen
  3. from snake import Snake
  4. from food import Food
  5. from scoreboard import Scoreboard
  6.  
  7.  
  8. starting_positions = [(0, 0), (-20, 0), (-40, 0)]
  9. screen = Screen()
  10. screen.setup(600, 600)
  11. screen.bgcolor("black")
  12. screen.title("Snake")
  13. screen.tracer(0)
  14.  
  15. snake = Snake()
  16. food = Food()
  17. scoreboard = Scoreboard()
  18.  
  19. screen.listen()
  20. screen.onkey(snake.up, "w")
  21. screen.onkey(snake.down, "s")
  22. screen.onkey(snake.left, "a")
  23. screen.onkey(snake.right, "d")
  24.  
  25. game_is_on = True
  26. while game_is_on:
  27.     screen.update()
  28.     time.sleep(0.1)
  29.     snake.move()
  30.  
  31.     # Detect Food Collision
  32.     if snake.head.distance(food) < 20:
  33.         food.refresh()
  34.         snake.extend()
  35.         scoreboard.increase_score()
  36.  
  37.     # Detect Wall Collision
  38.     if snake.head.xcor() > 300 or snake.head.xcor() < -300 or snake.head.ycor() > 300 or snake.head.ycor() < -300:
  39.         game_is_on = False
  40.         scoreboard.game_over()
  41.  
  42.     #Detect Tail Collision
  43.     for segment in snake.segments[1:]:
  44.         if snake.head.distance(segment) < 10:
  45.             game_is_on = False
  46.             scoreboard.game_over()
  47.  
  48.  
  49. screen.exitonclick()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement