-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (47 loc) · 1.44 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
50
51
52
53
54
55
56
57
58
from turtle import Screen
from snake import Snake
from time import sleep
from score import Score
from food import Food
screen = Screen()
screen.setup(width = 500, height=500)
screen.bgcolor("black")
screen.title("The Snake Game")
screen.tracer(0)
snake = Snake()
score = Score()
food = Food()
screen.listen()
screen.onkey(fun = snake.up, key = "Up")
screen.onkey(fun = snake.down, key = "Down")
screen.onkey(fun = snake.left, key = "Left")
screen.onkey(fun = snake.right, key = "Right")
game_is_on = True
while game_is_on:
screen.update()
sleep(0.1)
snake.move()
# Food
if snake.head.distance(food) <= 15:
food.refresh()
score.change_score()
score.update_score()
snake.extend()
# Wall collision
if (snake.head.xcor() > 230) or (snake.head.xcor() < -230) or (snake.head.ycor() > 230) or (snake.head.ycor() < -230):
score.reset()
snake.reset()
exit_game = screen.numinput("Game Over", "Press 0 to Exit the game", 1, minval=0, maxval=1000)
if exit_game == 0:
game_is_on = False
break
# Tail collision
for tail in snake.snake_body[1:]:
if snake.head.distance(tail) < 10:
score.reset()
snake.reset()
screen.numinput("Game Over", "Press 0 to Exit the game", 1, minval=0, maxval=1000)
if exit_game == 0:
game_is_on = False
break
screen.exitonclick()