-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (53 loc) · 1.67 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
from os import path
import turtle
import os
from time import sleep
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import ScoreBoard
# setting up the main game screen 600x600 & Black BG
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("The Snake Game Created By YJ-928")
# to hide all turtle activities on screen
screen.tracer(0)
# initializing our snake and food from resp classes
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
# continuosly listen and wait for key_press or user_commands
# after detecting key, call and execute resp func
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
# while loop condition
game_is_on = True
# loop to continously run the game
while game_is_on:
# to see snake only after all segments have moved
screen.update()
sleep(0.5)
snake.move()
# detect collision with food
# Score a point
if snake.snake_head.distance(food) < 15:
food.refresh()
snake.extend_snake()
scoreboard.increase_score()
# detect collision with wall
# GameOver == GameRestart
if snake.snake_head.xcor() > 290 or snake.snake_head.xcor() < -290 or snake.snake_head.ycor() > 290 or snake.snake_head.ycor() < -290:
scoreboard.game_restart()
snake.reset()
# detect collision with tail
# GameOver == GameRestart
for segment in snake.turtle_snake[1::]:
if snake.snake_head.distance(segment) < 10:
scoreboard.game_restart()
snake.reset()
# to exit screen only if we click on it
screen.exitonclick()