-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_board.py
56 lines (42 loc) · 1.24 KB
/
game_board.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
# Impoerting necessary module
from snake import Snake
from food import Food
from score_board import ScoreBoard
from turtle import Turtle, Screen
import time
screen = Screen()
screen.setup(600, 600)
screen.tracer(0)
screen.title("Snake Game")
screen.bgcolor("black")
# to listen events
screen.listen()
snake = Snake()
score_board = ScoreBoard()
food = Food()
is_game_on = True
screen.onkey(fun=snake.turn_up, key='Up')
screen.onkey(fun=snake.turn_right, key='Right')
screen.onkey(fun=snake.turn_down, key='Down')
screen.onkey(fun=snake.turn_left, key='Left')
while is_game_on:
snake.moveSnake()
screen.update()
time.sleep(0.14)
# Detect food eating
if snake.head.distance(food) <= 15:
snake.extend()
food.refresh()
score_board.increase_score()
# Detect snake reach to the wall
if snake.head.xcor() < -290 or snake.head.xcor() > 280 or snake.head.ycor() < -280 or snake.head.ycor() > 290:
is_game_on = False
score_board.game_over()
# Detect collision between snake body
for segment in snake.segments:
if snake.head == segment:
pass
elif snake.head.distance(segment) < 15:
is_game_on = False
score_board.game_over()
screen.exitonclick()