-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_snake_game.py
142 lines (111 loc) · 3.53 KB
/
14_snake_game.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from tkinter import font
from turtle import Turtle, Screen
import random as rand
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)
starting_postions = [(0,0), (-20,0), (-40,0)]
move_distance = 15
up = 90
down = 270
left = 180
right = 0
class Snake():
def __init__(self):
self.segments = []
self.create_snake()
self.head = self.segments[0]
def create_snake(self):
for position in starting_postions:
self.add_segment(position)
def add_segment(self, position):
segment = Turtle(shape="square")
segment.penup()
segment.color("white")
segment.goto(position)
self.segments.append(segment)
def extend(self):
self.add_segment(self.segments[-1].position())
def move(self):
for seg in range(len(self.segments) - 1,0,-1):
new_x = self.segments[seg - 1].xcor()
new_y = self.segments[seg - 1].ycor()
self.segments[seg].goto(new_x, new_y)
self.head.forward(move_distance)
def up(self):
if self.head.heading() != down:
self.head.setheading(up)
def down(self):
if self.head.heading() != up:
self.head.setheading(down)
def left(self):
if self.head.heading() != right:
self.head.setheading(left)
def right(self):
if self.head.heading() != left:
self.head.setheading(right)
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(stretch_len=0.5, stretch_wid=0.5)
self.color("red")
self.speed("fastest")
random_x = rand.randint(-280, 280)
random_y = rand.randint(-280, 280)
self.goto(random_x, random_y)
def refresh(self):
random_x = rand.randint(-280, 280)
random_y = rand.randint(-280, 280)
self.goto(random_x, random_y)
class Score(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.color("white")
self.penup()
self.goto(0,270)
self.update_score()
self.hideturtle()
def update_score(self):
self.write(f"Score: {self.score}", align="center", font=("Arial",24,"normal"))
def increase_score(self):
self.score += 1
self.clear()
self.update_score()
def game_over(self):
self.goto(0,0)
self.write("GAME OVER", align="center", font=("Arial",24,"normal"))
snake = Snake()
food = Food()
scoreboard = Score()
snake.create_snake()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
end_game = True
while end_game:
screen.update()
time.sleep(0.1)
snake.move()
# collision with food
if snake.head.distance(food) < 15:
food.refresh()
snake.extend()
scoreboard.increase_score()
#collision with wall
if snake.head.xcor() > 290 or snake.head.xcor() < -290 or snake.head.ycor() > 290 or snake.head.ycor() < -290:
end_game = False
scoreboard.game_over()
#if head collides with tail
for segment in snake.segments[1:]:
if snake.head.distance(segment) < 5:
end_game = False
scoreboard.game_over()
screen.exitonclick()