-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (44 loc) · 1.24 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
from turtle import Screen
from Snake import Snake
from food import Food
from scoreboard import ScoreBoard
import time
game_on = True
screen = Screen()
screen.setup(600, 600)
screen.bgcolor("green")
screen.title("Snake Cradle")
screen.tracer(0)
score = ScoreBoard()
snake = Snake()
food = Food()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.right, "Right")
screen.onkey(snake.left, "Left")
screen.onkey(snake.up, "w")
screen.onkey(snake.down, "s")
screen.onkey(snake.left, "a")
screen.onkey(snake.right, "d")
while game_on:
screen.update()
time.sleep(0.1)
snake.move()
# Detect Collision with food
if snake.head.distance(food) < 15:
food.refresh()
snake.increase()
score.eaten()
# Detect the Collision with the Wall
if snake.head.xcor() > 280 or snake.head.ycor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() < -280:
print("Game over with wall ")
game_on = False
score.game_over()
# Detect the collision with the snake itself
for part in snake.snake[1:]:
if snake.head.distance(part) < 10:
print("Game over")
game_on = False
score.game_over()
screen.exitonclick()