-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspaceInvaders.py
More file actions
144 lines (117 loc) · 4.16 KB
/
spaceInvaders.py
File metadata and controls
144 lines (117 loc) · 4.16 KB
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
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Invaders")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Load images
player_image = pygame.image.load("player.png").convert_alpha()
alien_image = pygame.image.load("alien.png").convert_alpha()
bullet_image = pygame.image.load("bullet.png").convert_alpha()
# Player class
class Player:
def __init__(self):
self.image = player_image
self.rect = self.image.get_rect(center=(WIDTH // 2, HEIGHT - 50))
self.speed = 5
def move(self, dx):
self.rect.x += dx * self.speed
self.rect.x = max(0, min(WIDTH - self.rect.width, self.rect.x))
def draw(self, surface):
surface.blit(self.image, self.rect.topleft)
# Alien class
class Alien:
def __init__(self, x, y):
self.image = alien_image
self.rect = self.image.get_rect(topleft=(x, y))
self.speed = 1
def move(self):
self.rect.y += self.speed
def draw(self, surface):
surface.blit(self.image, self.rect.topleft)
# Bullet class
class Bullet:
def __init__(self, x, y):
self.image = bullet_image
self.rect = self.image.get_rect(center=(x, y))
self.speed = -10
def move(self):
self.rect.y += self.speed
def draw(self, surface):
surface.blit(self.image, self.rect.topleft)
# Main game function
def main():
clock = pygame.time.Clock()
player = Player()
aliens = [] # Start with an empty list of aliens
bullets = []
score = 0
# Timer for alien generation
alien_spawn_time = 1000 # milliseconds
last_spawn_time = pygame.time.get_ticks()
game_over = False # Track game state
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-1)
if keys[pygame.K_RIGHT]:
player.move(1)
if keys[pygame.K_SPACE]:
if len(bullets) < 5: # Limit the number of bullets on screen
bullets.append(Bullet(player.rect.centerx, player.rect.top))
# Update bullets
for bullet in bullets[:]:
bullet.move()
if bullet.rect.bottom < 0:
bullets.remove(bullet)
# Spawn new aliens at intervals
current_time = pygame.time.get_ticks()
if current_time - last_spawn_time > alien_spawn_time and not game_over:
# Randomly place a new alien at the top
new_alien_x = random.randint(50, WIDTH - 50)
aliens.append(Alien(new_alien_x, 0))
last_spawn_time = current_time
# Update aliens
for alien in aliens[:]:
alien.move()
if alien.rect.top > HEIGHT:
game_over = True # Game over if an alien reaches the bottom
# Check for collisions
for bullet in bullets[:]:
for alien in aliens[:]:
if bullet.rect.colliderect(alien.rect):
bullets.remove(bullet)
aliens.remove(alien)
score += 1
break
# Draw everything
screen.fill(BLACK)
player.draw(screen)
for alien in aliens:
alien.draw(screen)
for bullet in bullets:
bullet.draw(screen)
# Display score
font = pygame.font.Font(None, 36)
score_text = font.render(f'Score: {score}', True, WHITE)
screen.blit(score_text, (10, 10))
# Display game over message
if game_over:
game_over_text = font.render('Game Over', True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 20))
final_score_text = font.render(f'Final Score: {score}', True, WHITE)
screen.blit(final_score_text, (WIDTH // 2 - final_score_text.get_width() // 2, HEIGHT // 2 + 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()