-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathport_invaders.py
217 lines (187 loc) · 6.02 KB
/
port_invaders.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
import pygame
import random
import time
import os
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
PLAYER_SIZE = 50
ENEMY_SIZE = 50
ALLOWED_SIZE = 50
FONT_SIZE = 30
WHITE = (255, 255, 255)
# Function to display text on screen
def display_text(text, pos, screen, font, color=WHITE):
text_surface = font.render(text, True, color)
screen.blit(text_surface, pos)
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Port Invaders")
# Load font and images
font = pygame.font.SysFont(None, FONT_SIZE)
player_image = pygame.image.load(os.path.join("assets", "player_ship.png"))
enemy_image = pygame.image.load(os.path.join("assets", "enemy_ship.png"))
allowed_image = pygame.image.load(os.path.join("assets", "allowed_ship.png"))
explosion_image = pygame.image.load(os.path.join("assets", "explosion.png"))
# Main game loop
clock = pygame.time.Clock()
level = 1
score = 0
common_ports = [22, 80, 443, 21, 25, 110, 143, 161, 162, 389, 636, 989, 990]
port_names = {
20: "FTP",
21: "FTP",
22: "SSH",
23: "Telnet",
25: "SMTP",
53: "DNS",
80: "HTTP",
110: "POP3",
143: "IMAP",
443: "HTTPS",
465: "SMTPS",
587: "SMTP",
993: "IMAPS",
995: "POP3S",
3306: "MySQL",
3389: "RDP",
5432: "PostgreSQL",
27017: "MongoDB",
22: "SFTP",
161: "SNMP",
162: "SNMP Trap",
389: "LDAP",
636: "LDAPS",
989: "FTPS Data",
990: "FTPS Control",
2049: "NFS",
3690: "SVN",
22: "SCP",
873: "RSYNC",
6660: "IRC",
6667: "IRC SSL",
6697: "IRC SSL",
9418: "Git",
1194: "OpenVPN",
1723: "PPTP",
5060: "SIP",
514: "Syslog",
1080: "SOCKS Proxy",
3128: "HTTP Proxy",
8080: "HTTP Proxy",
8081: "HTTP Proxy",
21: "FTPS",
137: "NetBIOS",
139: "NetBIOS",
445: "SMB/CIFS",
514: "Shell",
135: "MS RPC",
1433: "MSSQL",
1521: "Oracle SQL"
}
while True:
start_time = time.time()
player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT - 2 * PLAYER_SIZE]
allowed_ports = random.sample(common_ports, 3)
enemies = []
allowed = []
bullets = []
while time.time() - start_time < 15:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullets.append([player_pos[0] + PLAYER_SIZE // 2, player_pos[1]])
# Move player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_pos[0] -= 10
if keys[pygame.K_RIGHT]:
player_pos[0] += 10
# Generate random enemies
if random.randint(1, 20) == 20:
enemies.append([random.randint(0, SCREEN_WIDTH - ENEMY_SIZE), 0])
# Generate allowed ships
if random.randint(1, 20) == 20:
allowed.append([random.randint(0, SCREEN_WIDTH - ALLOWED_SIZE), 0, random.choice(allowed_ports)])
# Update enemy and bullet positions
for enemy in enemies:
enemy[1] += 5
if enemy[1] > SCREEN_HEIGHT:
enemies.remove(enemy)
score -= 50
for bullet in bullets:
bullet[1] -= 5
if bullet[1] < 0:
bullets.remove(bullet)
# Update allowed positions
for allow in allowed:
allow[1] += 5
if allow[1] > SCREEN_HEIGHT:
allowed.remove(allow)
score += 100
# Collision detection
for enemy in enemies:
for bullet in bullets:
if (
bullet[0] > enemy[0]
and bullet[0] < enemy[0] + ENEMY_SIZE
and bullet[1] > enemy[1]
and bullet[1] < enemy[1] + ENEMY_SIZE
):
bullets.remove(bullet)
enemies.remove(enemy)
score += 100
for allow in allowed:
for bullet in bullets:
if (
bullet[0] > allow[0]
and bullet[0] < allow[0] + ALLOWED_SIZE
and bullet[1] > allow[1]
and bullet[1] < allow[1] + ALLOWED_SIZE
):
bullets.remove(bullet)
allowed.remove(allow)
score -= 150
# Draw everything
screen.fill((0, 0, 0))
# Draw player using image
screen.blit(player_image, (player_pos[0], player_pos[1]))
# Draw bullets
for bullet in bullets:
pygame.draw.circle(screen, WHITE, (bullet[0], bullet[1]), 5)
# Draw enemies using image
for enemy in enemies:
screen.blit(enemy_image, (enemy[0], enemy[1]))
# Draw allowed using image and display port numbers
for allow in allowed:
screen.blit(allowed_image, (allow[0], allow[1]))
display_text(str(allow[2]), (allow[0] + 10, allow[1] + 55), screen, font, color=(255, 255, 255))
# Draw score and allowed ports
display_text(f"Score: {score}", [30, 30], screen, font)
display_text(f"Allowed Ports: {allowed_ports} - {[port_names[port] for port in allowed_ports]}", [30, 60], screen, font)
pygame.display.flip()
clock.tick(30)
# Level Transition Screen
screen.fill((0, 0, 0))
display_text(f"Level {level} Complete!", [250, 250], screen, font)
display_text(f"Score: {score}", [300, 300], screen, font)
display_text("Press any key to continue...", [200, 350], screen, font)
pygame.display.update()
time.sleep(3)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
break
if event.type == pygame.KEYDOWN:
break
level += 1
allowed_ports = random.sample(common_ports, 3)