-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (110 loc) · 3.69 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
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
from turtle import Turtle, Screen
from random import randint
import time
STARTING_POSITION = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.m_snake = []
self.add_snake()
self.m_head = self.m_snake[0]
def snake_body(self, position): # Create a segment of the snake.
new_turtle = Turtle("square")
new_turtle.penup()
new_turtle.color("white")
new_turtle.goto(position)
self.m_snake.append(new_turtle)
def add_snake(self): # Create 3 segments in the beginning.
for position in STARTING_POSITION:
self.snake_body(position)
def grow(self): # Add a new segment to the last segment of the snake.
self.snake_body(self.m_snake[-1].position())
def move_forward(self):
for turtle_num in range(len(self.m_snake) - 1, 0, -1):
new_x = self.m_snake[turtle_num - 1].xcor()
new_y = self.m_snake[turtle_num - 1].ycor()
self.m_snake[turtle_num].goto(new_x, new_y)
self.m_head.forward(MOVE_DISTANCE)
def up(self):
if self.m_head.heading() != DOWN:
self.m_head.setheading(UP)
def down(self):
if self.m_head.heading() != UP:
self.m_head.setheading(DOWN)
def left(self):
if self.m_head.heading() != RIGHT:
self.m_head.setheading(LEFT)
def right(self):
if self.m_head.heading() != LEFT:
self.m_head.setheading(RIGHT)
class Beans(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(stretch_wid=0.5, stretch_len=0.5)
self.color("yellow")
self.speed("fastest")
x_cor = randint(-280, 280)
y_cor= randint(-280, 280)
self.goto(x_cor, y_cor)
self.refresh()
def refresh(self):
x_cor = randint(-280, 280)
y_cor = randint(-280, 280)
self.goto(x_cor, y_cor)
class ScoreRecord(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.color("white")
self.penup()
self.goto(0, 270)
self.write(f"Score: {self.score}", align="center", font=("Arial", 24, "normal"))
self.hideturtle()
def add_score(self):
self.score += 1
self.clear()
self.write(f"Score: {self.score}", align="center", font=("Arial", 24, "normal"))
def game_over(self):
self.goto(0,0)
self.write("GAME OVER!", align="center", font=("Arial", 24, "normal"))
# Define the game screen.
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.tracer(0)
# Define objects.
snake = Snake()
bean = Beans()
score_record = ScoreRecord()
# Control the movement of the snake by keys.
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
is_game_on = True
while is_game_on:
screen.update()
time.sleep(0.1)
snake.move_forward()
# Check if the snake get the bean.
if snake.m_head.distance(bean) < 15:
bean.refresh()
snake.grow()
score_record.add_score()
# Detect the collision with walls.
if snake.m_head.xcor() > 280 or snake.m_head.xcor() < -300 or snake.m_head.ycor() > 280 or snake.m_head.ycor() < -280:
is_game_on = False
score_record.game_over()
# Detect the collision with tail.
for body in snake.m_snake[1:]: # Loop through the item in the snake.m_snake list other than the first one.
if snake.m_head.distance(body) < 10:
is_game_on = False
score_record.game_over()
screen.exitonclick()