Skip to content

Commit 164260c

Browse files
authored
Updated main branch
1 parent 4af2c95 commit 164260c

18 files changed

+330
-0
lines changed

assets/background-black.png

8.94 KB
Loading

assets/font/joystix monospace.ttf

46.5 KB
Binary file not shown.

assets/icon.png

1.28 KB
Loading

assets/pixel_laser_blue.png

5.94 KB
Loading

assets/pixel_laser_green.png

5.71 KB
Loading

assets/pixel_laser_red.png

5.92 KB
Loading

assets/pixel_laser_yellow.png

6.09 KB
Loading

assets/pixel_ship_blue_small.png

7.57 KB
Loading

assets/pixel_ship_green_small.png

7.68 KB
Loading

assets/pixel_ship_red_small.png

7.73 KB
Loading

assets/pixel_ship_yellow.png

10.4 KB
Loading

assets/sound/background-music.wav

8.7 MB
Binary file not shown.

assets/sound/bruh.wav

214 KB
Binary file not shown.

assets/sound/explosion.wav

184 KB
Binary file not shown.

assets/sound/gameover.wav

377 KB
Binary file not shown.

assets/sound/laser.wav

172 KB
Binary file not shown.

assets/sound/pop.wav

451 KB
Binary file not shown.

spacecolonizer.py

+330
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
import pygame
2+
import os
3+
import time
4+
import random
5+
from pygame.constants import WINDOWCLOSE
6+
from pygame import mixer
7+
# Initializes Font
8+
pygame.font.init()
9+
# Initializes mixer (music)
10+
pygame.mixer.init()
11+
12+
# Made by: Joshua Lagat
13+
14+
# Pygame Window
15+
WIDTH, HEIGHT = 1280, 720
16+
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
17+
pygame.display.set_caption("Space Colonizers")
18+
# Defines the Width and Height and initializes the window and window name
19+
20+
# Main Menu Font
21+
main_font = pygame.font.Font("assets/font/joystix monospace.ttf", 25)
22+
#os.path.join("assets", "joystix monospace.ttf", 25)
23+
24+
# Sounds
25+
laser_sound = mixer.Sound("assets/sound/laser.wav")
26+
explosion_sound = mixer.Sound("assets/sound/explosion.wav")
27+
pop_sound = mixer.Sound("assets/sound/pop.wav")
28+
gameover_sound = mixer.Sound("assets/sound/gameover.wav")
29+
bruh_sound = mixer.Sound("assets/sound/bruh.wav")
30+
31+
# Game Icon
32+
GAME_ICON = pygame.image.load(
33+
os.path.join("assets", "icon.png"))
34+
pygame.display.set_icon(GAME_ICON)
35+
36+
# Load image assets
37+
RED_SPACE_SHIP = pygame.image.load(
38+
os.path.join("assets", "pixel_ship_red_small.png"))
39+
BLUE_SPACE_SHIP = pygame.image.load(
40+
os.path.join("assets", "pixel_ship_blue_small.png"))
41+
GREEN_SPACE_SHIP = pygame.image.load(
42+
os.path.join("assets", "pixel_ship_green_small.png"))
43+
# User Ship
44+
YELLOW_SPACE_SHIP = pygame.image.load(
45+
os.path.join("assets", "pixel_ship_yellow.png"))
46+
47+
# Projectiles
48+
RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
49+
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
50+
GREEN_LASER = pygame.image.load(
51+
os.path.join("assets", "pixel_laser_green.png"))
52+
YELLOW_LASER = pygame.image.load(
53+
os.path.join("assets", "pixel_laser_yellow.png"))
54+
55+
# Space Background
56+
SPCBG = pygame.transform.scale(pygame.image.load(
57+
os.path.join("assets", "background-black.png")), (WIDTH, HEIGHT))
58+
59+
# Background Music
60+
mixer.music.load("assets/sound/background-music.wav")
61+
mixer.music.play(-1)
62+
63+
# Laser
64+
65+
66+
class Laser:
67+
def __init__(self, x, y, img):
68+
self.x = x
69+
self.y = y
70+
self.img = img
71+
self.mask = pygame.mask.from_surface(self.img)
72+
73+
def draw(self, window):
74+
window.blit(self.img, (self.x, self.y))
75+
76+
def move(self, vel):
77+
self.y += vel
78+
79+
def off_screen(self, height):
80+
return not(self.y < height and self.y >= 0)
81+
82+
def collision(self, obj):
83+
return collide(self, obj)
84+
85+
# Ship
86+
87+
88+
class Ship:
89+
COOLDOWN = 30
90+
91+
def __init__(self, x, y, health=100):
92+
self.x = x
93+
self.y = y
94+
self.health = health
95+
self.ship_img = YELLOW_SPACE_SHIP
96+
self.laser_img = YELLOW_LASER
97+
self.lasers = []
98+
self.cool_down_counter = 0
99+
100+
def draw(self, window):
101+
window.blit(self.ship_img, (self.x, self.y))
102+
for laser in self.lasers:
103+
laser.draw(window)
104+
105+
def move_lasers(self, vel, obj):
106+
self.cooldown()
107+
for laser in self.lasers:
108+
laser.move(vel)
109+
if laser.off_screen(HEIGHT):
110+
self.lasers.remove(laser)
111+
elif laser.collision(obj):
112+
obj.health -= 10
113+
self.lasers.remove(laser)
114+
explosion_sound.play()
115+
116+
def cooldown(self):
117+
if self.cool_down_counter >= self.COOLDOWN:
118+
self.cool_down_counter = 0
119+
elif self.cool_down_counter > 0:
120+
self.cool_down_counter += 1
121+
122+
def shoot(self):
123+
if self.cool_down_counter == 0:
124+
laser = Laser(self.x, self.y, self.laser_img)
125+
self.lasers.append(laser)
126+
self.cool_down_counter = 1
127+
laser_sound.play()
128+
129+
def get_width(self):
130+
return self.ship_img.get_width()
131+
132+
def get_height(self):
133+
return self.ship_img.get_width()
134+
135+
# Player
136+
137+
138+
class Player(Ship):
139+
def __init__(self, x, y, health=100):
140+
super().__init__(x, y, health)
141+
self.player_img = YELLOW_SPACE_SHIP
142+
self.laser_img = YELLOW_LASER
143+
self.mask = pygame.mask.from_surface(self.ship_img)
144+
self.max_health = health
145+
146+
def move_lasers(self, vel, objs):
147+
self.cooldown()
148+
for laser in self.lasers:
149+
laser.move(vel)
150+
if laser.off_screen(HEIGHT):
151+
self.lasers.remove(laser)
152+
else:
153+
for obj in objs:
154+
if laser.collision(obj):
155+
explosion_sound.play()
156+
objs.remove(obj)
157+
if laser in self.lasers:
158+
self.lasers.remove(laser)
159+
160+
def health_bar(self, window):
161+
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y +
162+
self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
163+
pygame.draw.rect(window, (0, 255, 0), (self.x, self.y + self.ship_img.get_height() +
164+
10, self.ship_img.get_width() * (self.health/self.max_health), 10))
165+
166+
def draw(self, window):
167+
super().draw(window)
168+
self.health_bar(window)
169+
170+
# Enemy
171+
172+
173+
class Enemy(Ship):
174+
# COLOR_MAP is a dictionary where it defines which spaceships and lasers go where using strings.
175+
COLOR_MAP = {
176+
"red": (RED_SPACE_SHIP, RED_LASER),
177+
"green": (GREEN_SPACE_SHIP, GREEN_LASER),
178+
"blue": (BLUE_SPACE_SHIP, BLUE_LASER)
179+
}
180+
181+
def __init__(self, x, y, color, health=100):
182+
super().__init__(x, y, health)
183+
pop_sound.play()
184+
# This calls on color_map to define the ships using strings that are in a dictionary.
185+
self.ship_img, self.laser_img = self.COLOR_MAP[color]
186+
self.mask = pygame.mask.from_surface(self.ship_img)
187+
188+
def move(self, vel):
189+
self.y += vel
190+
191+
def shoot(self):
192+
laser_sound.play()
193+
if self.cool_down_counter == 0:
194+
laser = Laser(self.x-24, self.y, self.laser_img)
195+
self.lasers.append(laser)
196+
self.cool_down_counter = 1
197+
198+
# Collision Loop
199+
200+
201+
def collide(obj1, obj2):
202+
offset_x = obj2.x - obj1.x
203+
offset_y = obj2.y - obj1.y
204+
# Uses map.overlap to identify if there are overlapping pixels.
205+
return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) != None
206+
207+
# Main Loop
208+
209+
210+
def main():
211+
run = True
212+
FPS = 360
213+
level = 0
214+
lives = 5
215+
main_font = pygame.font.Font("assets/font/joystix monospace.ttf", 40)
216+
lost_font = pygame.font.Font("assets/font/joystix monospace.ttf", 45)
217+
218+
enemies = []
219+
wave_length = 2
220+
enemy_vel = 2
221+
222+
player_vel = 14
223+
laser_vel = 12
224+
225+
player = Player(350, 400)
226+
227+
clock = pygame.time.Clock()
228+
229+
lost = False
230+
lost_count = 0
231+
232+
def redraw_window():
233+
# "Blit" places the defined "SPCBG" into WIN
234+
WIN.blit(SPCBG, (0, 0))
235+
# Draws Text
236+
lives_label = main_font.render(f"Lives: {lives}", 1, (0, 255, 255))
237+
level_label = main_font.render(f"Level: {level}", 1, (255, 255, 255))
238+
# Shows Text to "WIN" with BLIT
239+
WIN.blit(lives_label, (10, 10))
240+
WIN.blit(level_label, (WIDTH - level_label.get_width() - 10, 10))
241+
242+
for enemy in enemies:
243+
enemy.draw(WIN)
244+
245+
player.draw(WIN)
246+
# Lost Screen
247+
if lost:
248+
gameover_sound.play()
249+
lost_label = lost_font.render("You Lost, loser!", 1, (255, 0, 0))
250+
WIN.blit(lost_label, (WIDTH/2 - lost_label.get_width()/2, 350))
251+
252+
pygame.display.update()
253+
254+
while run:
255+
clock.tick(FPS)
256+
redraw_window()
257+
258+
if lives <= 0 or player.health <= 0:
259+
lost = True
260+
lost_count += 1
261+
262+
if lost:
263+
if lost_count > FPS * 0.25:
264+
pop_sound.play()
265+
run = False
266+
else:
267+
continue
268+
269+
if len(enemies) == 0:
270+
level += 1
271+
wave_length += 3
272+
for i in range(wave_length):
273+
enemy = Enemy(random.randrange(50, WIDTH-100),
274+
random.randrange(-1500*level/5, -100), random.choice(["red", "blue", "green"]))
275+
enemies.append(enemy)
276+
277+
for event in pygame.event.get():
278+
if event.type == pygame.QUIT:
279+
quit()
280+
281+
keys = pygame.key.get_pressed()
282+
if keys[pygame.K_a] and player.x - player_vel > 0: # Move Left
283+
player.x -= player_vel
284+
if keys[pygame.K_d] and player.x + player_vel + player.get_width() < WIDTH: # Move Right
285+
player.x += player_vel
286+
if keys[pygame.K_w] and player.y - player_vel > 0: # Move Up
287+
player.y -= player_vel
288+
if keys[pygame.K_s] and player.y + player_vel + player.get_height() + 15 < HEIGHT: # Move Down
289+
player.y += player_vel
290+
if keys[pygame.K_SPACE]: # Shoots Lasers
291+
player.shoot()
292+
if keys[pygame.K_q]: # Quits Game
293+
run = False
294+
295+
for enemy in enemies[:]:
296+
enemy.move(enemy_vel)
297+
enemy.move_lasers(laser_vel, player)
298+
299+
if random.randrange(0, 2*60) == 1:
300+
enemy.shoot()
301+
302+
if collide(enemy, player):
303+
player.health -= 10
304+
enemies.remove(enemy)
305+
explosion_sound.play()
306+
bruh_sound.play()
307+
elif enemy.y + enemy.get_height() > HEIGHT:
308+
lives -= 1
309+
enemies.remove(enemy)
310+
311+
player.move_lasers(-laser_vel, enemies)
312+
313+
314+
def main_menu():
315+
run = True
316+
while run:
317+
WIN.blit(SPCBG, (0, 0))
318+
menu_label = main_font.render(
319+
"Press any mouse key to start...", 1, (255, 255, 255))
320+
WIN.blit(menu_label, (WIDTH/2 - menu_label.get_width()/2, 350))
321+
pygame.display.update()
322+
for event in pygame.event.get():
323+
if event.type == pygame.QUIT:
324+
run = False
325+
if event.type == pygame.MOUSEBUTTONDOWN:
326+
main()
327+
quit()
328+
329+
330+
main_menu()

0 commit comments

Comments
 (0)