-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3-2.turtle_example.py
290 lines (288 loc) · 7 KB
/
3-2.turtle_example.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#======================================================================
# 3.2 turtle run 게임 만들기
#가. 윈도우 생성하고 제목과 배경색 설정
#======================================================================
import turtle as t
def main():
t.setup(500,500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
if __name__ == '__main__':
main()
t.mainloop()
#======================================================================
#3.2 나. 거북이 t 생성하기
#======================================================================
import turtle as t
gameon =False
def turnright():
t.setheading(0)
def turnleft():
t.setheading(180)
def turnup():
t.setheading(90)
def turndown():
t.setheading(270)
def runforward():
global gameon
t.forward(10)
if gameon:
t.ontimer(runforward,100)
def main():
global gameon
t.setup(500, 500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
t.shape('turtle')
t.color('white')
t.up()
t.onkeypress(turnright,'Right')
t.onkeypress(turnleft, 'Left')
t.onkeypress(turnup, 'Up')
t.onkeypress(turndown, 'Down')
t.listen()
gameon = True
runforward()
if __name__ == '__main__':
main()
t.mainloop()
#======================================================================
#3.2 다. 거북이 t를 추격하는 거북이p 생성하기
#======================================================================
import turtle as t
gameon =False
def turnright():
t.setheading(0)
def turnleft():
t.setheading(180)
def turnup():
t.setheading(90)
def turndown():
t.setheading(270)
def runforward():
global gameon, p
t.forward(10)
angle = p.towards(t.pos())
p.setheading(angle)
p.forward(7)
if gameon:
t.ontimer(runforward,100)
def createpursuer():
global p
p=t.Turtle()
p.shape('turtle')
p.color('black')
p.up()
p.hideturtle()
p.goto(-250,-250)
p.showturtle()
def main():
global gameon
t.setup(500, 500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
t.shape('turtle')
t.color('white')
t.up()
t.onkeypress(turnright,'Right')
t.onkeypress(turnleft, 'Left')
t.onkeypress(turnup, 'Up')
t.onkeypress(turndown, 'Down')
t.listen()
gameon = True
createpursuer()
runforward()
if __name__ == '__main__':
main()
t.mainloop()
#======================================================================
#3.2 라. prey 생성하기
#======================================================================
import turtle as t
from random import randint
gameon =False
def turnright():
t.setheading(0)
def turnleft():
t.setheading(180)
def turnup():
t.setheading(90)
def turndown():
t.setheading(270)
def runforward():
global gameon, p
t.forward(10)
angle = p.towards(t.pos())
p.setheading(angle)
p.forward(7)
if t.distance(prey)<15:
prey.goto(randint(-200,200),randint(-200,200))
if gameon:
t.ontimer(runforward,100)
def createpursuer():
global p
p=t.Turtle()
p.shape('turtle')
p.color('black')
p.up()
p.hideturtle()
p.goto(-250,-250)
p.showturtle()
def createprey():
global prey
prey=t.Turtle()
prey.shape('circle')
prey.color('hotpink')
prey.up()
prey.goto(randint(-200, 200),randint(-200, -200))
def main():
global gameon
t.setup(500, 500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
t.shape('turtle')
t.color('white')
t.up()
t.onkeypress(turnright,'Right')
t.onkeypress(turnleft, 'Left')
t.onkeypress(turnup, 'Up')
t.onkeypress(turndown, 'Down')
t.listen()
gameon = True
createpursuer()
createprey()
runforward()
if __name__ == '__main__':
main()
t.mainloop()
#======================================================================
#3.2 마. 게임 끝내기
#======================================================================
import turtle as t
from random import randint
gameon =False
def turnright():
t.setheading(0)
def turnleft():
t.setheading(180)
def turnup():
t.setheading(90)
def turndown():
t.setheading(270)
def runforward():
global gameon, p
t.forward(10)
angle = p.towards(t.pos())
p.setheading(angle)
p.forward(7)
if t.distance(prey)<15:
prey.goto(randint(-200,200),randint(-200,200))
if t.distance(p)<15:
t.write('Game Over', font=('Aril', 20, 'bold'))
gameon = False
return
if gameon:
t.ontimer(runforward,100)
def createpursuer():
global p
p=t.Turtle()
p.shape('turtle')
p.color('black')
p.up()
p.hideturtle()
p.goto(-250,-250)
p.showturtle()
def createprey():
global prey
prey=t.Turtle()
prey.shape('circle')
prey.color('hotpink')
prey.up()
prey.goto(randint(-200, 200),randint(-200, -200))
def main():
global gameon
t.setup(500, 500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
t.shape('turtle')
t.color('white')
t.up()
t.onkeypress(turnright,'Right')
t.onkeypress(turnleft, 'Left')
t.onkeypress(turnup, 'Up')
t.onkeypress(turndown, 'Down')
t.listen()
gameon = True
createpursuer()
createprey()
runforward()
if __name__ == '__main__':
main()
t.mainloop()
#======================================================================
#3.2 혼자해보기 점수추가 버전 -> score는 아직 구현 안됨
# 요거 먹이 먹을때마다 검은색 거북이 속도만 빠르게 하는건? -> 완료
#======================================================================
import turtle as t
from random import randint
import time
start = time.time() # 게임 실행 시간으로 적 속도 조절하자
gameon =False
def turnright():
t.setheading(0)
def turnleft():
t.setheading(180)
def turnup():
t.setheading(90)
def turndown():
t.setheading(270)
def runforward():
global gameon, p
t.forward(10)
angle = p.towards(t.pos())
p.setheading(angle)
p.forward(6+(time.time()-start)/2) # 적 속도 조절
if t.distance(prey)<15:
prey.goto(randint(-200,200),randint(-200,200))
if t.distance(p)<15:
t.write("Game Over", font=('Aril', 20, 'bold'))
gameon = False
return
if gameon:
t.ontimer(runforward,100)
def createpursuer(): # 적
global p
p=t.Turtle()
p.shape('turtle')
p.color('black')
p.up()
p.hideturtle()
p.goto(-250,-250)
p.showturtle()
def createprey(): #먹이
global prey
prey=t.Turtle()
prey.shape('circle')
prey.color('hotpink')
prey.up()
prey.goto(randint(-200, 200),randint(-200, -200))
def main():
global gameon
t.setup(500, 500)
t.title('Turtle Run')
t.bgcolor('forestgreen')
t.shape('turtle')
t.color('white')
t.up()
t.onkeypress(turnright,'Right')
t.onkeypress(turnleft, 'Left')
t.onkeypress(turnup, 'Up')
t.onkeypress(turndown, 'Down')
t.listen()
gameon = True
createpursuer()
createprey()
runforward()
if __name__ == '__main__':
main()
t.mainloop()