-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser.py
227 lines (176 loc) · 6.53 KB
/
laser.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
# import the random library, to set the initial position of the rock.
import random
# Set the size of the window
WIDTH = 600
HEIGHT = 500
# Create the actors
spacecraft = Actor('spacecraft') # The spacecraft actor
rock = Actor('rock') # Rock actor with the same image
# Create the laser rect
# (Not within the screen area.)
laser = Rect((0,-HEIGHT*4), (2, 1000))
# The centre of the screen
centre_x = WIDTH/2
centre_y = HEIGHT/2
# Global flags
gameOver = False
laserCharged = True
laserFiring = False
rmRock = False
# The score
score = 0
topScore = 0
# ---------------------------------------
def startingPosition():
# A random position along the top of the screen
return (random.randint(0, WIDTH), 0)
# ---------------------------------------
def startingVelocity():
# A random velocity, going downwards away from the top of the screen.
return (random.randint(-5, 5), random.randint(1, 5))
# ---------------------------------------
def initialRockPosition():
global rock
global rock_vx
global rock_vy
global rmRock
rock.image = 'rock' # In case it has been destroyed
rock.pos = 0, HEIGHT*2 # Off the screen
rock_vx = 0 # Set the x component of the rock velocity to be zero
rock_vy = 0 # Set the y component of the rock velocity to be zero
# Random starting position, along top edge.
(rock.x, rock.y) = startingPosition()
# Random starting velocity, going down the screen
(rock_vx, rock_vy) = startingVelocity()
# Set the rock to the default image, just in case it has been
# destroyed.
rock.image = 'rock'
# Reset the rock removal flag, used by the clock to allow the rock
# to be seen to be destroyed.
rmRock = False
# ---------------------------------------
def initialPositions():
global spacecraft
global score
spacecraft.image = 'spacecraft' # In case it has been destroyed
# Initial position and velocity
spacecraft.pos = centre_x, centre_y # Inital position of the spacecraft
initialRockPosition() # Set the initial rock position and image.
score = 0 # Reset the score, since the game has just begun.
# ---------------------------------------
# Starting settings for the game
initialPositions()
# ---------------------------------------
# This function is called by the Pygame Zero timer
def laserFiringComplete():
global laserFiring
laserFiring = False # Set the firing flag, to show the laser beam
# Use the clock to prevent the player from firing the laser straight
# away.
clock.schedule(laserChargingComplete, 1.0)
# ---------------------------------------
# This function is called by the Pygame Zero timer
def laserChargingComplete():
global laserCharged
# Set the charged flag, to allow the laser to be fired.
laserCharged = True
# ---------------------------------------
# This function is called by the Pygame Zero timer
def removeRock():
global rmRock
# Set the rock removal flag, to make sure that the rock is removed.
rmRock = True
# ---------------------------------------
def draw():
global laser
screen.clear()
spacecraft.draw()
rock.draw()
# Draw the score information at the bottom of the screen.
screen.draw.text("Score : "+str(score), center=(centre_x-100, HEIGHT-10.))
screen.draw.text("Top Score : "+str(topScore), center=(centre_x+100, HEIGHT-10.))
# If the laser is currently firing, then draw it.
if laserFiring:
screen.draw.filled_rect(laser,(0,255,0))
# Check to see if the game is over
if gameOver:
# If the game is over, then print a series of red rectangles
# with a text message.
for i in range(20,-5,-5):
screen.draw.filled_rect(Rect((centre_x-(100+i), centre_y-(30+i)), (200+(i*2), 80+(i*2))), (200-(i*8), 0, 0))
screen.draw.text("GAME OVER!", center=(centre_x, centre_y))
screen.draw.text("(n to restart)", center=(centre_x, centre_y+20))
# ---------------------------------------
def updateSpacecraft():
global laserFiring
global laserCharged
global laser
# If the cursor keys are pressed, then move the
# spacecraft within the limits of the screen.
if keyboard.left and spacecraft.left > 2:
spacecraft.x -= 2
if keyboard.right and spacecraft.right < WIDTH+2:
spacecraft.x += 2
if keyboard.down and spacecraft.bottom < HEIGHT+2:
spacecraft.y += 2
if keyboard.up and spacecraft.top > 2:
spacecraft.y -= 2
# If the spacebar has been pressed and the laser is charged, then
# fire the laser.
if keyboard.space and laserCharged:
laserCharged = False
laserFiring = True
# The laser should fire for 3/10 of a second.
clock.schedule(laserFiringComplete, 0.3)
# If the laser is firing, then move it to where the spacecraft is.
if laserFiring:
laser = Rect((spacecraft.x-2,0),(4,spacecraft.top))
# ---------------------------------------
def updateRock():
global rock
# Check if the rock has gone off the screen or has been If it has, then
# reset its starting position and initial velocity.
if rock.right < 0 or rock.left > WIDTH or rock.top > HEIGHT or rmRock:
initialRockPosition()
# Move the x position by the corresponding velocity component
rock.x += rock_vx
# Move the y position by the corresponding velocity component
rock.y += rock_vy
# ---------------------------------------
def update():
global gameOver
global score
global topScore
# If the game is not over, move the spacecraft and rock
if not gameOver:
updateSpacecraft()
updateRock()
else:
# If the game is over, test to see if the n key has been pressed.
# If it has been pressed, reset the positions of the spracecraft and
# rock and restart the game.
if keyboard.n:
initialPositions()
gameOver = False
# Check to see if the rock has collided with the spacecraft
collision = spacecraft.colliderect(rock)
# If there has been a collision, then switch both actors to their
# destroyed versions and set the gameOver flag.
if collision:
rock.image = 'rock_destroyed'
spacecraft.image = 'spacecraft_destroyed'
gameOver = True
# Check to see if the laser has hit the rock
rockLaser = rock.colliderect(laser)
# If the laser has hit the rock and the rock has not already been
# destroyed, then destroy the rock and increment the score.
if rockLaser and laserFiring and rock.image != 'rock_destroyed':
rock.image = 'rock_destroyed'
score = score + 1
# If the current score is bigger than the top score, then update
# the top score.
if score > topScore:
topScore = score
# Set the timer to wait for a 1/5 of a second before removing the
# destroyed rock from view.
clock.schedule(removeRock, 0.2)