-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacerace.py
336 lines (225 loc) · 8.81 KB
/
spacerace.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
import random
import time
from pygame.locals import (
K_UP,
K_DOWN,
K_ESCAPE,
KEYDOWN,
QUIT,
K_w,
K_s,
)
pygame.init()
# Constants (You can change these to make the game easier or harder! Try it out!)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SIZE_OF_SHIP = 20
MAX_VEL = 2
ACCERLATION = 0.01
WIDTH_OF_STARS = 20
HEIGHT_OF_STARS = 2
MAX_SPEED_OF_STARS = 2
MIN_SPEED_OF_STARS = 0.5
DISABILITY_PENALTY_TIME = 200 # Frames you are disabled after hitting a star
DISABILITY_PENALTY_POINTS = 0 # Points you lose when you hit a star
NUM_STARS = 20
GAME_TIME = 60 # Game time in seconds
# Variables
velocity_ship1 = 0
velocity_ship2 = 0
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Make 2 ships
ship1 = pygame.image.load("spaceship.png")
ship1 = pygame.transform.scale(ship1, (SIZE_OF_SHIP, SIZE_OF_SHIP))
ship1_x = SCREEN_WIDTH * 0.2 - SIZE_OF_SHIP/2
ship1_y = SCREEN_HEIGHT * 0.8
ship2 = pygame.image.load("spaceship.png")
ship2 = pygame.transform.scale(ship2, (SIZE_OF_SHIP, SIZE_OF_SHIP))
ship2_x = SCREEN_WIDTH * 0.8 - SIZE_OF_SHIP/2
ship2_y = SCREEN_HEIGHT * 0.8
# Make 2 scores
isDisabledShip1 = False
isDisabledShip2 = False
disabilityTime1 = 0
disabilityTime2 = 0
score1 = 0
score2 = 0
myFont = pygame.font.SysFont("monospace", 15)
# Make stars
class Star:
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
def move(self):
self.x += self.speed
# If the star goes off the screen, move it to the other side
if self.x > SCREEN_WIDTH:
self.x = 0
if self.x < 0:
self.x = SCREEN_WIDTH
def draw(self):
pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, WIDTH_OF_STARS, HEIGHT_OF_STARS))
pygame.draw.ellipse(screen, (255, 255, 255), (self.x, self.y, WIDTH_OF_STARS, HEIGHT_OF_STARS))
# Add 10 stars to the screen at random positions
stars = []
for i in range(NUM_STARS):
x = random.randint(0, SCREEN_WIDTH)
y = random.randint(SCREEN_HEIGHT*.02, int(SCREEN_HEIGHT*0.75))
speed = random.choice((-1, 1)) * round(random.uniform(MIN_SPEED_OF_STARS, MAX_SPEED_OF_STARS), 2)
stars.append(Star(x, y, speed))
def flip_ship(ship, direction):
ship1 = pygame.image.load("spaceship.png")
ship1 = pygame.transform.scale(ship1, (SIZE_OF_SHIP, SIZE_OF_SHIP))
if direction == "up":
ship = pygame.transform.flip(ship1, False, True)
if direction == "down":
ship = pygame.transform.flip(ship1, False, False)
return ship
def add_score(ship):
print ("Ship " + str(ship) + " scored a point!")
if ship == 1:
global score1
score1 += 1
if ship == 2:
global score2
score2 += 1
def add_velocity(direction, velocity):
if direction == "down" and velocity < MAX_VEL:
velocity += ACCERLATION
if direction == "down" and velocity < MAX_VEL and velocity < 0:
velocity += ACCERLATION * 3
if direction == "up" and velocity > -MAX_VEL:
velocity -= ACCERLATION
if direction == "up" and velocity > -MAX_VEL and velocity > 0:
velocity -= ACCERLATION * 3
return velocity
def check_point(ship1_y, ship2_y):
if ship1_y <= 0:
ship1_y = SCREEN_HEIGHT - SIZE_OF_SHIP
add_score(1)
if ship1_y >= SCREEN_HEIGHT - SIZE_OF_SHIP:
ship1_y = SCREEN_HEIGHT - SIZE_OF_SHIP
if ship2_y <= 0:
ship2_y = SCREEN_HEIGHT - SIZE_OF_SHIP
add_score(2)
if ship2_y >= SCREEN_HEIGHT - SIZE_OF_SHIP:
ship2_y = SCREEN_HEIGHT - SIZE_OF_SHIP
return ship1_y, ship2_y
def update(keys, ship1_y, ship2_y, velocity_ship1, velocity_ship2, isShip1Disabled, isShip2Disabled):
if (not isDisabledShip1):
if keys[K_w]:
velocity_ship1 = add_velocity("up", velocity_ship1)
ship1_y += velocity_ship1
if keys[K_s]:
velocity_ship1 = add_velocity("down", velocity_ship1)
ship1_y += velocity_ship1
if not keys[K_w] and not keys[K_s]:
if velocity_ship1 > .1:
velocity_ship1 -= ACCERLATION * 2
ship1_y += velocity_ship1
if velocity_ship1 < -.1:
velocity_ship1 += ACCERLATION * 2
ship1_y += velocity_ship1
if velocity_ship1 < .1 and velocity_ship1 > -.1:
velocity_ship1 = 0
if (not isDisabledShip2):
if keys[K_UP]:
velocity_ship2 = add_velocity("up", velocity_ship2)
ship2_y += velocity_ship2
if keys[K_DOWN]:
velocity_ship2 = add_velocity("down", velocity_ship2)
ship2_y += velocity_ship2
if not keys[K_UP] and not keys[K_DOWN]:
if velocity_ship2 > .1:
velocity_ship2 -= ACCERLATION * 2
ship2_y += velocity_ship2
if velocity_ship2 < -.1:
velocity_ship2 += ACCERLATION * 2
ship2_y += velocity_ship2
if velocity_ship2 < .1 and velocity_ship2 > -.1:
velocity_ship2 = 0
ship1_y, ship2_y = check_point(ship1_y, ship2_y)
return ship1_y, ship2_y, velocity_ship1, velocity_ship2
game_start_time = time.time()
###### Main Running Function ######
def get_game_state():
game_state = [ship1_y, ship2_y, velocity_ship1, velocity_ship2, isDisabledShip1, isDisabledShip2, disabilityTime1, disabilityTime2, score1, score2, stars]
time_left = time.time() - game_start_time
keys_pressed = pygame.key.get_pressed()
game_state.append(keys_pressed)
game_state.append(time_left)
return game_state
running = True
while running:
if time.time() - game_start_time > GAME_TIME:
running = False
print("Game Over!")
print("Player 1 Score: " + str(score1))
print("Player 2 Score: " + str(score2))
if score1 > score2:
print("Player 1 Wins!")
if score2 > score1:
print("Player 2 Wins!")
if score1 == score2:
print("Tie Game!")
screen.fill((0, 0, 0))
pressed_keys = pygame.key.get_pressed()
if isDisabledShip1:
disabilityTime1 += 1
pygame.draw.rect(screen, (255, 0, 0), (ship1_x, ship1_y + SIZE_OF_SHIP, SIZE_OF_SHIP, HEIGHT_OF_STARS))
if disabilityTime1 > DISABILITY_PENALTY_TIME:
isDisabledShip1 = False
disabilityTime1 = 0
if isDisabledShip2:
disabilityTime2 += 1
pygame.draw.rect(screen, (255, 0, 0), (ship2_x, ship2_y + SIZE_OF_SHIP, SIZE_OF_SHIP, HEIGHT_OF_STARS))
if disabilityTime2 > DISABILITY_PENALTY_TIME:
isDisabledShip2 = False
disabilityTime2 = 0
ship1_y, ship2_y, velocity_ship1, velocity_ship2 = update(pressed_keys, ship1_y, ship2_y, velocity_ship1, velocity_ship2, isDisabledShip1, isDisabledShip2)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
# Draw all the stars
for star in stars:
star.draw()
star.move()
# Check collision with ship
if star.x < ship1_x + SIZE_OF_SHIP and star.x > ship1_x and star.y < ship1_y + SIZE_OF_SHIP and star.y > ship1_y:
isDisabledShip1 = True
ship1_y = SCREEN_HEIGHT * 0.9
velocity_ship1 = 0
score1 -= DISABILITY_PENALTY_POINTS
print ("Ship 1 is disabled!")
if star.x < ship2_x + SIZE_OF_SHIP and star.x > ship2_x and star.y < ship2_y + SIZE_OF_SHIP and star.y > ship2_y:
isDisabledShip2 = True
velocity_ship2 = 0
ship2_y = SCREEN_HEIGHT * 0.9
score2 -= DISABILITY_PENALTY_POINTS
print ("Ship 2 is disabled!")
if velocity_ship1 >= .5:
ship1 = flip_ship(ship1, "up")
if velocity_ship2 >= .5:
ship2 = flip_ship(ship2, "up")
if velocity_ship1 <= 0:
ship1 = flip_ship(ship1, "down")
if velocity_ship2 <= 0:
ship2 = flip_ship(ship2, "down")
screen.blit(ship1, (ship1_x, ship1_y))
screen.blit(ship2, (ship2_x, ship2_y))
score1display = myFont.render("Score 1: " + str(score1), 1, (255,255,0))
score2display = myFont.render("Score 2: " + str(score2), 1, (255,255,0))
timer_display = myFont.render("Time: " + str(int(GAME_TIME - (time.time() - game_start_time))), 1, (255,255,0))
screen.blit(score1display, (SCREEN_WIDTH * 0.2 - score1display.get_width()/2, 0))
screen.blit(score2display, (SCREEN_WIDTH * 0.8 - score2display.get_width()/2, 0))
screen.blit(timer_display, (SCREEN_WIDTH * 0.5 - timer_display.get_width()/2, 0))
pygame.display.update()
pygame.display.flip()
pygame.quit()