-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceshipGame.py
186 lines (169 loc) · 5.72 KB
/
SpaceshipGame.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
import turtle, random, math
class Game:
'''
Purpose: to spawn a player in the SpaceCrast class, draw the moon's surface, initialize the world coordinates, spawn obstacles with obastacle class, and run the gameloop()
Instance variables:
surface: surface of the moon
asteroids: list of asteroids
Methods:
__init__: initializes the class
gameloop: loops the game and determines when it's over under what condition
'''
def __init__(self):
#Bottom left corner of screen is (0, 0)
#Top right corner is (500, 500)
turtle.setworldcoordinates(0, 0, 500, 500)
cv = turtle.getcanvas()
cv.adjustScrolls()
#Ensure turtle is running as fast as possible
turtle.delay(0)
self.surface = turtle.Turtle()
self.surface.penup()
self.surface.goto(-5,18)
self.surface.pendown()
#self.surface.right(90)
self.surface.forward(530)
self.asteroids = []
player_px = random.uniform(100,400)
player_py = random.uniform(250,450)
player_vx = random.uniform(-4,4)
player_vy = random.uniform(-2,0)
self.player = SpaceCraft(player_px, player_py, player_vx, player_vy)
#self.player.goto(250, 250)
turtle.onkeypress(self.player.thrust, 'Up')
turtle.onkeypress(self.player.left_turn, 'Left')
turtle.onkeypress(self.player.right_turn, 'Right')
for i in range(10):
px = random.uniform(0,500)
py = random.uniform(0,500)
vx = random.uniform(-5,5)
vy = random.uniform(-5,5)
self.asteroids.append(Obstacles(px, py, vx, vy))
turtle.tracer(0,0)
self.gameloop()
#These two lines must always be at the BOTTOM of __init__
turtle.listen()
turtle.mainloop()
def gameloop(self):
crash = True
for i in self.asteroids:
i.move_obstacles()
if (abs(self.player.xcor() - i.xcor()) <= 9) and (abs(self.player.ycor() - i.ycor()) <= 9):
crash = False
self.player.move()
if (self.player.ycor() >= 20) and crash:
turtle.ontimer(self.gameloop, 30)
turtle.update()
else:
if (self.player.vx < 3 and self.player.vx > -3) and (self.player.vy < 3 and self.player.vy > -3) and crash:
turtle.write("Successful landing!", font=("Ariel", 20, "normal"))
else:
turtle.write("You crashed!", font=("Ariel", 20, "normal"))
class SpaceCraft(turtle.Turtle):
'''
Purpose:
to create the spacecraft and the mechanics required for the player to move it
Instance Variables:
px: x-position
py: y-position
vx: x-velocity
vy: y-velocity
fuel_remaining: fuel remaining in spaceship
Methods:
__init__: initializes
move: moves the spaceship to x and y coordinates and sets gravity
thrust: propels the ship in the direction it's facing if there is remaining fuel
left_turn: turns the spaceship left 15 degrees if there is fuel remaining
right_turn: turns the spaceship right 15 degrees if there is fuel remaining
'''
def __init__(self, px, py, vx, vy):
turtle.Turtle.__init__(self)
self.px = px
self.py = py
self.vx = vx
self.vy = vy
# self.vx = vx
# self.vy = vy
self.fuel_remaining = 40
self.left(90)
self.penup()
self.speed(0)
self.goto(px, py)
def move(self):
self.vy -= 0.0468
px = self.xcor() + self.vx
py = self.ycor() + self.vy
self.goto(px,py)
def thrust(self):
if self.fuel_remaining > 0:
self.fuel_remaining -= 1
angle_pointing = math.radians(self.heading())
self.vx += math.cos(angle_pointing)
self.vy += math.sin(angle_pointing)
print(self.fuel_remaining)
else:
print("Out of fuel")
def left_turn(self):
if self.fuel_remaining > 0:
self.fuel_remaining -= 1
self.left(15)
print(self.fuel_remaining)
else:
print("Out of fuel")
def right_turn(self):
if self.fuel_remaining > 0:
self.fuel_remaining -= 1
self.right(15)
print(self.fuel_remaining)
else:
print("Out of fuel")
class Obstacles(turtle.Turtle):
'''
Purpose:
to create the moving obstacles (asteroids) which the player must avoid
Instance Variables:
vx = x-velocity
vy = y-velocity
Methods:
__init__: initializes
move_obstacles: moves the obstacles and bounces them off the walls.
'''
def __init__(self, px, py, vx, vy):
turtle.Turtle.__init__(self)
self.vx = vx
self.vy = vy
self.shape('circle')
self.color('red')
self.penup()
self.speed(0)
self.goto(px,py)
self.goto(px, py)
self.move_obstacles()
def move_obstacles(self):
self.speed(50)
self.vy -= 0.1
px = self.xcor()
py = self.ycor()
px += self.vx
py += self.vy
if px > 500:
px = 500
self.vx *= -1
if px < 0:
px = 0
self.vx *= -1
if py > 500:
py = 500
self.vy *= -1
if py < 0:
py = 0
self.vy *= -1
self.goto(px,py)
if __name__ == '__main__':
Game()
# for i in range(10):
# px = random.uniform(0,500)
# py = random.uniform(0,500)
# vx = random.uniform(-5,5)
# vy = random.uniform(-5,5)
# asteroids = Obstacles(px, py, vx, vy)