-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
50 lines (39 loc) · 1.26 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
from turtle import Turtle, Screen
import time
from snake import Snake
from food import Food
from scoreboard import ScoreBoard
snake = Snake()
food = Food()
screen = Screen()
score_board = ScoreBoard()
screen.setup(width = 600, height = 600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)
screen.listen()
screen.onkeypress(snake.up, "Up")
screen.onkeypress(snake.down, "Down")
screen.onkeypress(snake.left, "Left")
screen.onkeypress(snake.right, "Right")
snake.createSnake()
is_game_on = True
while is_game_on:
screen.update()
time.sleep(0.1)
snake.move()
# Detcet food eat or not
if snake.segments[0].distance(food) < 20:
food.EatFood()
snake.ExtendSnake()
score_board.UpdateScore()
# Game over when Bite into Wall
if snake.segments[0].xcor() > 300 or snake.segments[0].xcor() < -300 or snake.segments[0].ycor() > 300 or snake.segments[0].ycor() < -300 :
score_board.GameOver()
is_game_on = False
# Game Over when Bite It Self
for segment in snake.segments[1:]:
if snake.segments[0].distance(segment) < 10:
score_board.GameOver()
is_game_on = False
screen.exitonclick()