-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (37 loc) · 1.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from turtle import Screen
from time import sleep
from snake_game import Snake
from food import Food
from scoreboard import ScoreBoard
def main() -> None:
screen = Screen()
screen.tracer(0)
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Snake Game")
game_is_on = True
while game_is_on:
sleep(0.2)
screen.update()
snake.move_snake()
screen.listen()
screen.onkey(snake.move_up, "Up")
screen.onkey(snake.move_down,"Down")
screen.onkey(snake.move_left,"Left")
screen.onkey(snake.move_right, "Right")
if food.distance(snake.head) < 20:
food.make_food()
scoreboard.increase_score()
snake.add_tail()
if snake.detect_collision_with_tail():
snake.reset()
scoreboard.reset()
if snake.has_collided_with_walls():
snake.reset()
scoreboard.reset()
screen.exitonclick()
if __name__ == "__main__":
main()