Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from inputs import InputManager, PLAYER_KEYMAPS
from player import Player
from npc import NPC
from ui import show_end_message


from config import GAME_FPS
Expand Down Expand Up @@ -67,22 +66,28 @@ def try_spawn_npc(self):
# Nastav nový random interval pro další spawn
self.next_spawn_interval = random.randint(*self.spawn_interval_range)


def try_npc_shoot(self, npc):
now = pygame.time.get_ticks()
npc_id = id(npc)
last_shot = self.npc_last_shot_times.get(npc_id, 0)
if now - last_shot >= self.npc_shoot_cooldown:
target = npc.get_shot_target(self.players.values())
if target:
bullet = Bullet(
npc.rect.centerx, npc.rect.centery,
target.rect.centerx, target.rect.centery,
color=(255, 0, 0),
shooter=npc
)
self.npc_bullets.append(bullet)
self.npc_last_shot_times[npc_id] = now

if now - self.npc_last_shot_times.get(npc_id, 0) < self.npc_shoot_cooldown:
return

target = npc.get_shot_target(
[p for p in self.players.values() if p.health > 0]
)

if not target:
return

bullet = Bullet(
npc.rect.centerx, npc.rect.centery,
target.rect.centerx, target.rect.centery,
color=(255, 0, 0),
shooter=npc
)
self.npc_bullets.append(bullet)
self.npc_last_shot_times[npc_id] = now


def check_bullet_collisions(self):
Expand Down Expand Up @@ -127,13 +132,11 @@ def handle_key_events(self):
def update_players(self, **kwargs):
"""Handles players updates"""


for player in self.players.values():

if player.health <= 0:
continue

inputs = self.input_manager.get_inputs(player.uid)


player.update(inputs = inputs, **kwargs)


Expand All @@ -145,27 +148,32 @@ def update_players(self, **kwargs):

self.input_manager.clear_inputs(player.uid)




def update_npcs(self, **kwargs):
def update_npcs(self, **kwargs):
"""Handles npcs updates"""


for npc in self.npcs:
npc.update(players = self.players, **kwargs)
for npc in self.npcs[:]:
npc.update(players=self.players, **kwargs)
self.try_npc_shoot(npc)

for player in self.players.values():
if player.health <= 0:
continue

if npc.rect.colliderect(player.rect):
player.health -= npc.damage * 2
self.npcs.remove(npc)
break




def render_all(self):
"""Draws itself"""
self.screen.fill((0, 0, 0))


for player in self.players.values():
player.draw(self.screen)
if player.health > 0:
player.draw(self.screen)


for npc in self.npcs:
Expand Down Expand Up @@ -196,7 +204,7 @@ def update_bullets(self):
def check_game_end(self):
alive_players = [p for p in self.players.values() if p.health > 0]
if not alive_players:
if self.score >= 100:
if self.score >= 200:
self.game_result = "win"
else:
self.game_result = "lost"
Expand Down
16 changes: 9 additions & 7 deletions npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def __init__(self, x, y,npc_type="medium"):
self.damage = npc_config["damage"]
self.color = npc_config["color"]




def update(self, **kwargs):
"""Updates self position according to players"""
nearest_player = self.find_closest_player(self, kwargs["players"].values())

all_players = kwargs["players"].values()
living_players = [p for p in all_players if p.health > 0]

if not living_players:
return

nearest_player = self.find_closest_player(self, living_players)

if self.rect.x < nearest_player.rect.x:
self.rect.x += NPC_SPEED
Expand All @@ -48,9 +48,11 @@ def update(self, **kwargs):
elif self.rect.y > nearest_player.rect.y:
self.rect.y -= NPC_SPEED


def get_shot_target(self, players):
return self.find_closest_player(self, players)
living_players = [p for p in players if p.health > 0]
if not living_players:
return None
return self.find_closest_player(self, living_players)


def draw_lifebar(self, screen):
Expand Down
Loading