-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18A-turtle_run2.py
More file actions
92 lines (76 loc) · 1.6 KB
/
18A-turtle_run2.py
File metadata and controls
92 lines (76 loc) · 1.6 KB
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
import turtle as t
import random
score=0
playing=False
te=t.Turtle()
te.shape("turtle")
te.color("red")
te.speed(0)
te.up()
te.goto(0,200)
ts=t.Turtle()
ts.shape("circle")
ts.color("green")
ts.speed(0)
ts.up()
ts.goto(0,-200)
def turn_right():
t.seth(0)
def turn_up():
t.seth(90)
def turn_left():
t.seth(180)
def turn_down():
t.seth(270)
def start():
global playing
if playing==False:
playing=True
t.clear()
play()
def play():
global score
global playing
t.forward(10)
if random.randint(1,5)==3:
ang=te.towards(t.pos())
te.seth(ang)
speed=score+5
if speed>15:
speed=15
te.forward(speed)
if t.distance(te)<12:
text="Score: "+str(score)
message("Game over",text)
playing=False
score=0
if t.distance(ts)<12:
score+=1
t.write(score)
star_x=random.randint(-230,230)
star_y=random.randint(-230,230)
ts.goto(star_x,star_y)
if playing:
t.ontimer(play,100)
def message(m1,m2):
t.clear()
t.goto(0,100)
t.write(m1,False,"center",("",20))
t.goto(0,-100)
t.write(m2,False,"center",("",15))
t.home()
t.title("Turtle Run")
t.setup(500,500)
t.bgcolor("orange")
t.shape("turtle")
t.color("white")
t.speed(0)
t.up()
t.color("white")
t.onkeypress(turn_right,"Right")
t.onkeypress(turn_left,"Left")
t.onkeypress(turn_up,"Up")
t.onkeypress(turn_down,"Down")
t.onkeypress(start,"space")
t.listen()
message("Turtle Run","[Space]")