-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (52 loc) · 1.48 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from turtle import Turtle, Screen
from Snake import Snake
from Food import Food
from Scoreboard import ScoreBoard
import time
#Defining Screen
screen = Screen()
screen.screensize(600, 600)
screen.title("Snake Game")
screen.bgcolor("black")
screen.tracer(0)
#Draw Boundries
boundry = Turtle()
boundry.penup()
boundry.pencolor("white")
boundry.turtlesize(0.3)
boundry.goto((295, -295))
boundry.pendown()
boundry.goto((295, 280))
boundry.goto((-295, 280))
boundry.goto((-295, -295))
boundry.goto((295, -295))
#Creating Food and Snake Objects
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
#Check for user interaction
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.1)
snake.move()
#Detect collision with food
if snake.head.distance(food) < 15:
snake.extend()
scoreboard.increase_score()
food.refresh()
#Detect Collision with wall
if snake.head.xcor()>295 or snake.head.xcor()<-295 or snake.head.ycor()>275 or snake.head.ycor()<-295:
game_is_on = False
scoreboard.game_over()
#Detect collision with Tail
for segments in snake.segments[1:]:
if snake.head.distance(segments) < 10:
scoreboard.game_over()
game_is_on = False
screen.exitonclick()