-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (42 loc) · 1.22 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
44
45
46
47
48
49
from turtle import Screen
from food import Food
from scoreboard import ScoreBoard
from snake import Snake
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Classic Snake Game")
screen.tracer(0)
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
screen.listen()
screen.onkey(key="Up", fun=snake.up)
screen.onkey(key="Down", fun=snake.down)
screen.onkey(key="Left", fun=snake.left)
screen.onkey(key="Right", fun=snake.right)
game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.1)
snake.move()
# Detect food collision.
if snake.head.distance(food) < 15:
scoreboard.increase_score()
snake.extend()
food.refresh()
# Detect wall collision.
if snake.head.xcor() > 285 or snake.head.xcor() < -285 or snake.head.ycor() > 285 or snake.head.ycor() < -285:
scoreboard.reset()
snake.reset()
# game_is_on = False
# scoreboard.game_over()
# Detect tail collision.
for segment in snake.segments[1:]:
if snake.head.distance(segment) < 10:
scoreboard.reset()
snake.reset()
# game_is_on = False
# scoreboard.game_over()
screen.exitonclick()