-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-airport-code-game.py
More file actions
167 lines (141 loc) · 4.14 KB
/
python-airport-code-game.py
File metadata and controls
167 lines (141 loc) · 4.14 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
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
# Turle Graphics Game - Space Turtle Camp
import turtle
import math
import random
import os
import time
import sched
from cities import cities
from threading import Timer
#Set Up Screen
turtle.setup(850,748)
wn = turtle.Screen()
wn.bgpic('world_map.gif')
wn.bgcolor('blue')
wn.tracer(3)
#Draw A Border
mypen = turtle.Turtle()
mypen.color('black')
mypen.penup()
mypen.setposition(-400,-199)
mypen.pendown()
mypen.pensize(3)
for s in range(2):
mypen.forward(800)
mypen.left(90)
mypen.forward(398)
mypen.left(90)
mypen.hideturtle()
s = turtle.Shape("compound")
# poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
poly1 = ((-2,-13),(0,-14), (2,-13), (2,10), (0,11), (-2,10))
s.addcomponent(poly1, "black")
poly2 = ((0,4),(10,-4),(-10,-4))
s.addcomponent(poly2, "black")
poly3 = ((0,-11), (5,-16), (-5,-16))
s.addcomponent(poly3, "black")
wn.register_shape("myshape", s)
print(turtle.getshapes())
#Create player turtle
player = turtle.Turtle()
player.color('black')
player.shape('myshape')
player.penup()
player.speed(0)
#Game Variables
score = 0
speed = 0
timeout = time.time() + 60
#Define Game Functions
def turn_left():
player.left(30)
def turn_right():
player.right(30)
def increase_speed():
global speed
speed += 0.75
def decrease_speed():
global speed
if speed > 1:
speed -= 0.75
def isCollision(t1, t2):
d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
if d < 30:
return True
else:
return False
def draw(penname, x, y, text, font, size):
penname.hideturtle()
penname.penup()
penname.setposition(x, y)
penname.write(text, align='left', font=(font, size))
#Set Keyboard Bindings
turtle.listen()
turtle.onkey(turn_left, 'Left')
turtle.onkey(turn_right, 'Right')
turtle.onkey(increase_speed, 'Up')
turtle.onkey(decrease_speed, 'Down')
#Initialize with first city
#Set first city as current city
current_city = random.choice(cities)
#display city code
citycodepen = turtle.Turtle()
citycodepen.color('black')
citynamepen = turtle.Turtle()
citynamepen.color('black')
citynamepen.hideturtle()
draw(citycodepen, -100, 255, "Find %s" % current_city['code'], 'Arial', 40)
#display score
scorepen = turtle.Turtle()
scorepen.color('black')
draw(scorepen, -120, -255, "Your Score: %s" % score, 'Arial', 40)
#display time
timepen = turtle.Turtle()
timepen.color('black')
draw(mypen, -180, -300, "Time Remaining:", 'Arial', 40)
#set city co-ordinates on the map
citypoint = turtle.Turtle()
# citypoint.color('black')
# citypoint.shape('circle')
# citypoint.shapesize(0.5)
citypoint.penup()
citypoint.hideturtle()
citypoint.setposition(current_city['x'], current_city['y'])
while time.time() < timeout:
#Player
player.forward(speed)
timepen.undo()
draw(timepen, 130, -300, str(int(timeout - time.time())), 'Arial', 40)
#Player - Boundary Check
if player.xcor() > 400:
player.setposition(-400, player.ycor())
if player.xcor() < -400:
player.setposition(400, player.ycor())
if player.ycor() > 200:
player.setposition(player.xcor(), -200)
if player.ycor() < -200:
player.setposition(player.xcor(), 200)
#Collision
if isCollision(player, citypoint):
#draw last city name
citynamepen.undo()
draw(citynamepen, -160, 230, "Good Job! That was %s!" % current_city['name'], 'Arial', 20)
#add one point to score
score += 1
#change current city to another random city
current_city = random.choice(cities)
#reset coordinate
citypoint.setposition(current_city['x'], current_city['y'])
#change city name and score text
citycodepen.undo()
draw(citycodepen, -100, 255, "Find %s" % current_city['code'], 'Arial', 40)
scorepen.undo()
draw(scorepen, -120, -255, "Your Score: %s" % score, 'Arial', 40)
scorepen.setposition(0, 10)
scorepen.color("yellow")
scorestring = "Game Over: You Scored %s" % score
scorepen.write(scorestring, False, align="center", font=("Arial", 28, "normal"))
scorepen.setposition(0, -10)
scorestring = "CLICK TO EXIT"
scorepen.write(scorestring, False, align="center", font=("Arial", 14, "normal"))
wn.exitonclick()