Skip to content

Commit c1fe0d9

Browse files
committed
V0.9
v0.9
1 parent 636c929 commit c1fe0d9

File tree

4 files changed

+133
-44
lines changed

4 files changed

+133
-44
lines changed

alien.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Alien(pygame.sprite.Sprite):
44
def __init__(self, type, x, y):
55
super().__init__()
6+
self.type = type
67
path = f"Graphics/alien_{type}.png"
78
self.image = pygame.image.load(path)
89
self.rect = self.image.get_rect(topleft = (x, y))
@@ -21,7 +22,7 @@ def __init__(self, screen_width, offset):
2122
self.speed = 3
2223
else:
2324
self.speed = -3
24-
self.rect = self.image.get_rect(topleft = (x, 50))
25+
self.rect = self.image.get_rect(topleft = (x, 70))
2526

2627
def update(self):
2728
self.rect.x += self.speed

game.py

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77

88
class Game():
99
def __init__(self, screen_width, screen_height, offset):
10+
11+
self.lives = 3
12+
self.lives_surface = pygame.image.load("Graphics/ship.png").convert_alpha()
13+
self.score = 0
14+
self.highscore = 0
15+
self.load_highscore()
16+
1017
self.screen_width = screen_width
1118
self.screen_height = screen_height
1219
self.offset = offset
1320
self.spaceship = pygame.sprite.GroupSingle()
1421
self.spaceship.add(Spaceship(screen_width, screen_height, offset))
15-
self.obstacle_1 = Obstacle(screen_width/4 - 128,screen_height - 100)
16-
self.obstacle_2 = Obstacle((screen_width/4)*2 - 128,screen_height - 100)
17-
self.obstacle_3 = Obstacle((screen_width/4)*3 - 128,screen_height - 100)
18-
self.obstacle_4 = Obstacle((screen_width/4)*4 - 128,screen_height - 100)
22+
self.obstacle_1 = Obstacle(screen_width/4 - 110,screen_height - 100)
23+
self.obstacle_2 = Obstacle((screen_width/4)*2 - 110,screen_height - 100)
24+
self.obstacle_3 = Obstacle((screen_width/4)*3 - 110,screen_height - 100)
25+
self.obstacle_4 = Obstacle((screen_width/4)*4 - 110,screen_height - 100)
1926
self.aliens = pygame.sprite.Group()
2027
self.alien_lasers = pygame.sprite.Group()
2128
self.mystery_ship = pygame.sprite.GroupSingle()
22-
self.alien_direction = 1
29+
self.alien_direction = 1
30+
self.run = True
2331
self.create_aliens()
2432

2533
def create_mystery_ship(self):
@@ -33,10 +41,16 @@ def check_for_collisions(self):
3341
if pygame.sprite.spritecollide(laser, obstacle.blocks, True):
3442
laser.kill()
3543

36-
if pygame.sprite.spritecollide(laser, self.aliens, True):
37-
laser.kill()
44+
aliens_hit = pygame.sprite.spritecollide(laser, self.aliens, True)
45+
if aliens_hit:
46+
for alien in aliens_hit:
47+
self.score += alien.type * 100
48+
self.check_for_highscore()
49+
laser.kill()
3850

3951
if pygame.sprite.spritecollide(laser, self.mystery_ship, True):
52+
self.score += 500
53+
self.check_for_highscore()
4054
laser.kill()
4155

4256
#Aliens Laser
@@ -46,7 +60,10 @@ def check_for_collisions(self):
4660
if pygame.sprite.spritecollide(laser, obstacle.blocks, True):
4761
laser.kill()
4862
if pygame.sprite.spritecollide(laser, self.spaceship, False):
49-
print("Game Over!")
63+
laser.kill()
64+
self.lives -= 1
65+
if self.lives == 0:
66+
self.game_over()
5067

5168
def create_aliens(self):
5269
for row in range(5):
@@ -80,4 +97,40 @@ def alien_shoot_laser(self):
8097
if self.aliens.sprites():
8198
random_alien = random.choice(self.aliens.sprites())
8299
laser_sprite = Laser(random_alien.rect.center, -6, self.screen_height)
83-
self.alien_lasers.add(laser_sprite)
100+
self.alien_lasers.add(laser_sprite)
101+
102+
def game_over(self):
103+
self.run = False
104+
105+
def reset(self):
106+
self.run = True
107+
self.lives = 3
108+
self.spaceship.sprite.reset()
109+
self.aliens.empty()
110+
self.create_aliens()
111+
self.spaceship.sprite.lasers.empty()
112+
self.alien_lasers.empty()
113+
114+
# Recreate the obstacles
115+
self.obstacle_1 = Obstacle(self.screen_width / 4 - 110, self.screen_height - 100)
116+
self.obstacle_2 = Obstacle((self.screen_width / 4) * 2 - 110, self.screen_height - 100)
117+
self.obstacle_3 = Obstacle((self.screen_width / 4) * 3 - 110, self.screen_height - 100)
118+
self.obstacle_4 = Obstacle((self.screen_width / 4) * 4 - 110, self.screen_height - 100)
119+
120+
self.score = 0
121+
122+
def check_for_highscore(self):
123+
if self.score > self.highscore:
124+
self.highscore = self.score
125+
126+
# Save the highscore to a file
127+
with open('highscore.txt', 'w') as file:
128+
file.write(str(self.highscore))
129+
130+
def load_highscore(self):
131+
try:
132+
with open('highscore.txt', 'r') as file:
133+
self.highscore = int(file.read())
134+
except FileNotFoundError:
135+
# If the file doesn't exist, set a default highscore
136+
self.highscore = 0

main.py

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
SCREEN_HEIGHT = 700
1313
OFFSET = 50
1414

15+
title_font = pygame.font.Font("Font/monogram.ttf", 40)
16+
level_surface = title_font.render("LEVEL 01", False, YELLOW)
17+
game_over_surface = title_font.render("GAME OVER", False, YELLOW)
18+
score_text_surface = title_font.render("SCORE", False, YELLOW)
19+
high_score_text_surface = title_font.render("HIGH-SCORE", False, YELLOW)
20+
1521
screen = pygame.display.set_mode((SCREEN_WIDTH + OFFSET, SCREEN_HEIGHT + 2*OFFSET))
1622
pygame.display.set_caption("Space Invaders")
1723

@@ -25,36 +31,61 @@
2531
game = Game(SCREEN_WIDTH, SCREEN_HEIGHT, OFFSET)
2632

2733
while True:
28-
for event in pygame.event.get():
29-
if event.type == pygame.QUIT:
30-
pygame.quit()
31-
sys.exit()
32-
if event.type == ALIENLASER:
33-
game.alien_shoot_laser()
34-
if event.type == MYSTERYSHIP:
35-
game.create_mystery_ship()
36-
37-
screen.fill(GREY)
38-
pygame.draw.rect(screen, YELLOW, (10, 10 ,775, 775), 2, 0, 60, 60, 60, 60)
39-
pygame.draw.line(screen, YELLOW, (25, 800 - 70), (800 -25, 800 - 70), 3)
40-
41-
game.alien_lasers.update()
42-
game.spaceship.update()
43-
game.mystery_ship.update()
44-
game.check_for_collisions()
45-
46-
game.alien_position_checker()
47-
game.aliens.update(game.alien_direction)
48-
49-
game.spaceship.draw(screen)
50-
game.spaceship.sprite.lasers.draw(screen)
51-
game.obstacle_1.blocks.draw(screen)
52-
game.obstacle_2.blocks.draw(screen)
53-
game.obstacle_3.blocks.draw(screen)
54-
game.obstacle_4.blocks.draw(screen)
55-
game.aliens.draw(screen)
56-
game.alien_lasers.draw(screen)
57-
game.mystery_ship.draw(screen)
58-
59-
pygame.display.update()
60-
clock.tick(60)
34+
for event in pygame.event.get():
35+
if event.type == pygame.QUIT:
36+
pygame.quit()
37+
sys.exit()
38+
if event.type == ALIENLASER:
39+
game.alien_shoot_laser()
40+
if event.type == MYSTERYSHIP and game.run:
41+
game.create_mystery_ship()
42+
43+
keys = pygame.key.get_pressed()
44+
if keys[pygame.K_SPACE] and game.run == False:
45+
game.reset()
46+
47+
screen.fill(GREY)
48+
pygame.draw.rect(screen, YELLOW, (10, 10 ,775, 775), 2, 0, 60, 60, 60, 60)
49+
pygame.draw.line(screen, YELLOW, (25, 800 - 70), (800 -25, 800 - 70), 3)
50+
51+
if game.run:
52+
game.alien_lasers.update()
53+
game.spaceship.update()
54+
game.mystery_ship.update()
55+
game.check_for_collisions()
56+
game.alien_position_checker()
57+
game.aliens.update(game.alien_direction)
58+
59+
game.spaceship.draw(screen)
60+
game.spaceship.sprite.lasers.draw(screen)
61+
game.obstacle_1.blocks.draw(screen)
62+
game.obstacle_2.blocks.draw(screen)
63+
game.obstacle_3.blocks.draw(screen)
64+
game.obstacle_4.blocks.draw(screen)
65+
game.aliens.draw(screen)
66+
game.alien_lasers.draw(screen)
67+
game.mystery_ship.draw(screen)
68+
screen.blit(score_text_surface, (50, 15, 50, 50))
69+
screen.blit(high_score_text_surface, (550, 15, 50, 50))
70+
71+
formatted_high_score = str(game.highscore).zfill(5) # Pad with leading zeros to make it 4 digits
72+
formatted_score = str(game.score).zfill(5) # Pad with leading zeros to make it 4 digits
73+
score_surface = title_font.render(formatted_score, False, YELLOW)
74+
high_score_surface = title_font.render(formatted_high_score, False, YELLOW)
75+
screen.blit(score_surface, (50, 40, 50, 50))
76+
screen.blit(high_score_surface, (625, 40, 50, 50))
77+
78+
if game.run:
79+
screen.blit(level_surface, (570, SCREEN_HEIGHT + 38, 50, 50))
80+
else:
81+
screen.blit(game_over_surface, (570, SCREEN_HEIGHT + 38, 50, 50))
82+
83+
x_start = 50 # Adjust this based on your desired starting x-coordinate
84+
y = SCREEN_HEIGHT + OFFSET - 5
85+
86+
for _ in range(game.lives):
87+
screen.blit(game.lives_surface, (x_start, y))
88+
x_start += 50 # Adjust this value to control the spacing between lives
89+
90+
pygame.display.update()
91+
clock.tick(60)

spaceship.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, screen_width, screen_height, offset):
1010
self.speed = 5
1111
self.screen_width = screen_width
1212
self.screen_height = screen_height
13+
self.offset = offset
1314
self.laser_ready = True
1415
self.laser_time = 0
1516
self.laser_cooldown = 300
@@ -45,4 +46,7 @@ def update(self):
4546
self.player_input()
4647
self.constrain_position()
4748
self.recharge()
48-
self.lasers.update()
49+
self.lasers.update()
50+
51+
def reset(self):
52+
self.rect = self.image.get_rect(midbottom = ((self.screen_width + self.offset)/2, self.screen_height))

0 commit comments

Comments
 (0)