-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3-2-e.doingAlone.py
73 lines (70 loc) · 1.86 KB
/
3-2-e.doingAlone.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
# 3.2 혼자해보기
# prey에 닿을 때마다 점수를 올리거나
# 일정 시간이 지남에 따라 추격하는 Turtle객체를 추가해보자.
# 점수추가 버전 turtle객체 추가는 너무 복잡해짐
# 먹이 먹을때마다 속도 추가해주는건 어떨지?
import turtle as t
from random import randint
import time
start = time.time() # 게임 실행 시간으로 적 속도 조절하자
gameon =False
score = 0#점수
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, score
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))
score += 1#먹으면 점수추가
if t.distance(p)<15:
t.write("Game Over\nScore:{}".format(score), 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()