-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
193 lines (148 loc) · 4.64 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from turtle import Turtle, Screen
import time
from random import choice
HEADING = 0
CYCLE_COMPLETED = True
current_positions = [(0, 0), (-20, 0), (-40, 0)]
colour_scheme = ["white", "white", "pink"]
white_segment = True
def main():
global HEADING
global CYCLE_COMPLETED
global current_positions
global colour_scheme
screen = Screen()
screen.setup(width=600, height=600)
screen.title("Snek")
screen.bgcolor("black")
screen.tracer(0)
screen.listen()
screen.onkey(north, key="w")
screen.onkey(east, key="d")
screen.onkey(south, key="s")
screen.onkey(west, key="a")
starting_positions = [(0, 0), (-20, 0), (-40, 0)]
snek = []
for position in current_positions:
new_segment = Turtle(shape="square")
new_segment.penup()
colour_select = segment_colour_selector(colour_scheme)
colour_scheme = segment_colour_changer(colour_scheme)
new_segment.color(colour_select)
new_segment.goto(position)
snek.append(new_segment)
food = Turtle(shape="circle")
food.penup()
food.color("white")
place_food(food)
screen.update()
game_is_on = True
while game_is_on:
game_is_on = move_snek(snek, food, screen)
time.sleep(0.1)
screen.exitonclick()
def direction(heading):
global CYCLE_COMPLETED
global HEADING
if HEADING == 0 and heading == 180:
return
elif HEADING == 90 and heading == 270:
return
elif HEADING == 180 and heading == 0:
return
elif HEADING == 270 and heading == 90:
return
else:
HEADING = heading
CYCLE_COMPLETED = False
return
def north():
global CYCLE_COMPLETED
if CYCLE_COMPLETED:
heading = 90
direction(heading)
def east():
global CYCLE_COMPLETED
if CYCLE_COMPLETED:
heading = 0
direction(heading)
def south():
global CYCLE_COMPLETED
if CYCLE_COMPLETED:
heading = 270
direction(heading)
def west():
global CYCLE_COMPLETED
if CYCLE_COMPLETED:
heading = 180
direction(heading)
def move_snek(snek, food, screen):
global CYCLE_COMPLETED
global HEADING
global current_positions
global colour_scheme
append_new_segment = False
current_positions = []
for segment in snek:
current_x = segment.xcor()
current_y = segment.ycor()
current_positions.append((current_x, current_y))
move_to_x = current_positions[0][0]
move_to_y = current_positions[0][1]
snek[0].setheading(HEADING)
if HEADING is 0:
snek[0].goto(x=move_to_x + 20, y=move_to_y)
elif HEADING is 90:
snek[0].goto(x=move_to_x, y=move_to_y + 20)
elif HEADING is 180:
snek[0].goto(x=move_to_x - 20, y=move_to_y)
else:
snek[0].goto(x=move_to_x, y=move_to_y - 20)
if (snek[0].xcor(), snek[0].ycor()) in current_positions:
return False
if snek[0].xcor() > 290 or snek[0].xcor() < -290:
return False
if snek[0].ycor() > 290 or snek[0].ycor() < -290:
return False
if snek[0].xcor() == food.xcor() and snek[0].ycor() == food.ycor():
place_food(food)
new_segment_co_ords = (snek[-1].xcor(), snek[-1].ycor())
append_new_segment = True
for i in range(1, len(snek)):
move_from_x = current_positions[i][0]
move_from_y = current_positions[i][1]
co_ord = (move_to_x, move_to_y)
snek[i].goto(co_ord)
move_to_x = move_from_x
move_to_y = move_from_y
if append_new_segment:
new_segment = Turtle(shape="square")
new_segment.penup()
colour_select = segment_colour_selector(colour_scheme)
colour_scheme = segment_colour_changer(colour_scheme)
new_segment.color(colour_select)
new_segment.goto(new_segment_co_ords)
snek.append(new_segment)
screen.update()
CYCLE_COMPLETED = True
return True
def place_food(food):
global current_positions
positions = [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180]
choosing_co_ordinates = True
while choosing_co_ordinates:
random_x = choice(positions)
random_y = choice(positions)
co_ordinates = (random_x, random_y)
if co_ordinates not in current_positions:
food.hideturtle()
food.goto(co_ordinates)
food.showturtle()
return
def segment_colour_selector(colour_scheme):
select_segment_colour = colour_scheme[0]
return select_segment_colour
def segment_colour_changer(colour_scheme):
colour_scheme.append(colour_scheme[0])
return colour_scheme[1:]
main()