-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (155 loc) · 5.54 KB
/
main.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
import pygame
import random
import math
pygame.init()
# Creating the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('alien.png')
pygame.display.set_icon(icon)
# Loading images
backImg = pygame.image.load('background.jpg')
playerImg = pygame.image.load('invader.png')
enemyImg = pygame.image.load('enemy.png')
bulletImg = pygame.image.load('bullet.png')
# Player
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
# Enemy properties
enemies = []
num_of_enemies = 6 # Set initial number of enemies
max_enemies = 6 # Maximum number of enemies allowed
# Create enemies
for i in range(num_of_enemies):
enemy_x = random.randint(0, 730)
enemy_y = random.randint(50, 150)
enemies.append({'x': enemy_x, 'y': enemy_y, 'x_change': 0.3, 'y_change': 40, 'state': 'alive'})
# Bullet
bulletX = 0
bulletY = 480
bulletY_change = 10
bullet_state = "ready"
game_over_font = pygame.font.Font(None, 64)
restart_font = pygame.font.Font(None, 32)
def player(x, y):
screen.blit(playerImg, (x, y))
def draw_enemy(x, y):
screen.blit(enemyImg, (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = 'fire'
screen.blit(bulletImg, (x + 16, y + 10))
def is_collision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + math.pow(enemyY - bulletY, 2))
if distance < 27: # Adjust this value for better collision accuracy
return True
else:
return False
def player_enemy_collision(playerX, playerY, enemyX, enemyY):
distance = math.sqrt(math.pow(playerX - enemyX, 2) + math.pow(playerY - enemyY, 2))
if distance < 27: # Adjust this value for better collision accuracy
return True
else:
return False
def reset_game():
global playerX, playerY, bullet_state
playerX = 370
playerY = 480
bullet_state = "ready"
def game_over_text(text):
over_text = game_over_font.render(text, True, (255, 255, 255))
restart_text = restart_font.render("Press 'R' to restart", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
screen.blit(restart_text, (300, 320))
def create_enemy():
if len(enemies) < max_enemies:
enemy_x = random.randint(0, 730)
enemy_y = random.randint(50, 150)
enemies.append({'x': enemy_x, 'y': enemy_y, 'x_change': 0.3, 'y_change': 40, 'state': 'alive'})
running = True
game_over = False
while running:
screen.fill((0, 0, 0))
screen.blit(backImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not game_over:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.4
if event.key == pygame.K_RIGHT:
playerX_change = 0.4
if event.key == pygame.K_UP:
playerY_change = -0.4
if event.key == pygame.K_DOWN:
playerY_change = 0.4
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
game_over = False
enemies.clear()
for i in range(num_of_enemies):
enemy_x = random.randint(0, 730)
enemy_y = random.randint(50, 150)
enemies.append({'x': enemy_x, 'y': enemy_y, 'x_change': 0.3, 'y_change': 40, 'state': 'alive'})
reset_game()
playerX += playerX_change
playerY += playerY_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
if playerY <= 0:
playerY = 0
elif playerY >= 536:
playerY = 536
for enemy in enemies:
if enemy['state'] == 'alive':
enemy['x'] += enemy['x_change']
if enemy['x'] <= 0:
enemy['x_change'] = 0.3
enemy['y'] += enemy['y_change']
elif enemy['x'] >= 736:
enemy['x_change'] = -0.3
enemy['y'] += enemy['y_change']
draw_enemy(enemy['x'], enemy['y'])
create_enemy()
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
for enemy in enemies:
if enemy['state'] == 'alive':
collision = is_collision(enemy['x'], enemy['y'], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
enemy['state'] = 'dead'
alive_enemies = sum(enemy['state'] == 'alive' for enemy in enemies)
if alive_enemies == 0 and not game_over:
game_over = True
for enemy in enemies:
if enemy['state'] == 'alive':
collision = player_enemy_collision(playerX, playerY, enemy['x'], enemy['y'])
if collision:
game_over = True
if game_over and alive_enemies == 0:
game_over_text("YOU WIN!")
elif game_over:
game_over_text("YOU'RE DEAD!")
player(playerX, playerY)
pygame.display.update()
pygame.quit()