-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
151 lines (130 loc) · 5.05 KB
/
Copy pathgame.py
File metadata and controls
151 lines (130 loc) · 5.05 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
145
146
147
148
149
150
151
import pygame
import random
# Initialization
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Elite Shooter 2025")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Consolas", 20)
# Colors
BLACK = (10, 10, 15)
WHITE = (255, 255, 255)
PLAYER_CLR = (50, 205, 50)
BULLET_CLR = (255, 255, 0)
# Colors of enemies
ENEMY_REG = (220, 20, 60) # Ordinary
ENEMY_FAST = (255, 255, 0) # Fast
ENEMY_TANK = (148, 0, 211) # Tank
class Bullet:
def __init__(self, x, y, target_x, target_y):
self.pos = pygame.Vector2(x, y)
direction = pygame.Vector2(target_x, target_y) - self.pos
self.velocity = direction.normalize() * 12
self.rect = pygame.Rect(0, 0, 8, 8)
def update(self):
self.pos += self.velocity
self.rect.center = self.pos
class Enemy:
def __init__(self, type):
self.type = type # 'normal', 'fast', 'tank'
side = random.choice(['t', 'b', 'l', 'r'])
if side == 't': self.pos = pygame.Vector2(random.randint(0, WIDTH), -50)
elif side == 'b': self.pos = pygame.Vector2(random.randint(0, WIDTH), HEIGHT + 50)
elif side == 'l': self.pos = pygame.Vector2(-50, random.randint(0, HEIGHT))
else: self.pos = pygame.Vector2(WIDTH + 50, random.randint(0, HEIGHT))
# Type settings
if type == 'fast':
self.speed = 4.5
self.hp = 1
self.size = 18
self.color = ENEMY_FAST
elif type == 'tank':
self.speed = 1.0
self.hp = 3
self.size = 50
self.color = ENEMY_TANK
else: # normal
self.speed = 2.2
self.hp = 1
self.size = 30
self.color = ENEMY_REG
self.rect = pygame.Rect(0, 0, self.size, self.size)
def update(self, player_pos):
direction = (player_pos - self.pos).normalize()
self.pos += direction * self.speed
self.rect.center = self.pos
# Initialization of variables
player_pos = pygame.Vector2(WIDTH//2, HEIGHT//2)
hp, score = 100, 0
bullets, enemies = [], []
last_shot, shoot_delay = 0, 300
game_over = False
while True:
screen.fill(BLACK)
now = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if not game_over:
# 1. Control
keys = pygame.key.get_pressed()
move = pygame.Vector2(0, 0)
if keys[pygame.K_a]: move.x = -1
if keys[pygame.K_d]: move.x = 1
if keys[pygame.K_w]: move.y = -1
if keys[pygame.K_s]: move.y = 1
if move.length() > 0:
player_pos += move.normalize() * 5
# 2. Shooting (Auto)
if pygame.mouse.get_pressed()[0] and now - last_shot > shoot_delay:
mx, my = pygame.mouse.get_pos()
bullets.append(Bullet(player_pos.x, player_pos.y, mx, my))
last_shot = now
# 3. Spawn (Type Logic)
if random.randint(1, 40) == 1:
r = random.random()
if r < 0.15: etype = 'tank'
elif r < 0.35: etype = 'fast'
else: etype = 'normal'
enemies.append(Enemy(etype))
# 4. Collisions
p_rect = pygame.Rect(0,0,35,35); p_rect.center = player_pos
for b in bullets[:]:
b.update()
if not screen.get_rect().collidepoint(b.pos):
bullets.remove(b)
continue
for e in enemies[:]:
if b.rect.colliderect(e.rect):
e.hp -= 1
if b in bullets: bullets.remove(b)
if e.hp <= 0:
score += 500 if e.type == 'tank' else 100
enemies.remove(e)
break
for e in enemies[:]:
e.update(player_pos)
if e.rect.colliderect(p_rect):
hp -= 10
enemies.remove(e)
if hp <= 0: game_over = True
# 5. Drawing
pygame.draw.rect(screen, PLAYER_CLR, p_rect, border_radius=5)
for b in bullets: pygame.draw.circle(screen, BULLET_CLR, (int(b.pos.x), int(b.pos.y)), 4)
for e in enemies:
pygame.draw.rect(screen, e.color, e.rect, border_radius=4)
if e.type == 'tank': # Health bar for a tank
pygame.draw.rect(screen, WHITE, (e.rect.x, e.rect.y-10, e.size * (e.hp/3), 5))
# UI
screen.blit(font.render(f"SCORE: {score}", True, WHITE), (20, 20))
pygame.draw.rect(screen, (255,0,0), (20, 50, hp*2, 15)) # HP Bar
else:
# Game Over
msg = font.render("GAME OVER! PRESS 'R' TO RESTART", True, WHITE)
screen.blit(msg, (WIDTH//2 - 150, HEIGHT//2))
if pygame.key.get_pressed()[pygame.K_r]:
hp, score, enemies, bullets, game_over = 100, 0, [], [], False
pygame.display.flip()
clock.tick(60)