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
1 change: 1 addition & 0 deletions bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Bullet:
def __init__(self, x, y, target_x, target_y,color=(0, 0, 0),shooter=None):
self.rect = pygame.Rect(x, y, BULLET_SIZE, BULLET_SIZE)
self.rect.center = (x, y)
angle = math.atan2(target_y - y, target_x - x)
self.dx = math.cos(angle) * BULLET_SPEED
self.dy = math.sin(angle) * BULLET_SPEED
Expand Down
44 changes: 22 additions & 22 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ def __init__(self, **kwargs):

self.game_result = None


def spawn_random_npc(self):
npc_type = random.choices(
["easy", "medium", "hard"],
weights=[70, 25, 5],
k=1
)[0]
x = random.randint(50, self.screen.get_width() - 50)
y = random.randint(50, self.screen.get_height() - 50)
npc_type = random.choices(["easy", "medium", "hard"], weights=[70, 25, 5])[0]

sw, sh = self.screen.get_width(), self.screen.get_height()
margin = 10

corners = [
(margin, margin), # levý horní roh
(sw - margin, margin), # pravý horní roh
(margin, sh - margin), # levý dolní roh
(sw - margin, sh - margin), # pravý dolní roh
]
x, y = random.choice(corners)
self.npcs.append(NPC(x, y, npc_type=npc_type))


Expand Down Expand Up @@ -249,12 +253,12 @@ class SingleGame(AbstractGame):
def __init__(self, **kwargs):
super().__init__(**kwargs)

center_x = self.screen.get_width() // 2
center_y = self.screen.get_height() // 2

pl1 = Player(self.PLAYER1) # todo some better init
pl1.set_coords(20,20)
pl1 = Player(self.PLAYER1)
pl1.set_coords(center_x - 30, center_y)
self.players[self.PLAYER1] = pl1


self.input_manager.add_keymap(self.PLAYER1, PLAYER_KEYMAPS["wasd"])


Expand Down Expand Up @@ -286,24 +290,20 @@ class CoopGame(AbstractGame):
def __init__(self, **kwargs):
super().__init__(**kwargs)

center_x = self.screen.get_width() // 2
center_y = self.screen.get_height() // 2

pl1 = Player(self.PLAYER1)
pl1.set_coords(20,20)
pl1.set_coords(center_x - 30, center_y)
self.players[self.PLAYER1] = pl1


self.input_manager.add_keymap(self.PLAYER1, PLAYER_KEYMAPS["wasd"])


pl2 = Player(self.PLAYER2) # experimental player 2
pl2.set_coords(100, 20)
pl2.color = (0,0,255)
pl2 = Player(self.PLAYER2)
pl2.set_coords(center_x + 30, center_y)
pl2.color = (0, 0, 255)
self.players[self.PLAYER2] = pl2


self.input_manager.add_keymap(self.PLAYER2, PLAYER_KEYMAPS["arrows"])


self.npcs.append(NPC(400, 400))


Expand Down
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ def main():
game = SingleGame(screen=screen)
result = game.run()
if result == "win":
show_end_message(screen, "You win!")
show_end_message(screen, f"You win! Score: {game.score}")
elif result == "lost":
show_end_message(screen, "You lost.")
show_end_message(screen, f"You lost.Score: {game.score}")

if choice == GameType.COOP:
game = CoopGame(screen=screen)
result = game.run()
if result == "win":
show_end_message(screen, "You win!")
show_end_message(screen, f"You win!Score: {game.score}")
elif result == "lost":
show_end_message(screen, "You lost.")
show_end_message(screen, f"You lost.Score: {game.score}")
else:
show_end_message(screen, "Game ended.")

Expand Down
2 changes: 1 addition & 1 deletion npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def draw(self, screen):

def distance(self, rect1, rect2):
"""Helper function for distance computing"""
return math.hypot(rect1.x - rect2.x, rect1.y - rect2.y)
return math.hypot(rect1.centerx - rect2.centerx, rect1.centery - rect2.centery)


def find_closest_player(self, npc, players):
Expand Down
2 changes: 1 addition & 1 deletion player.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def update(self, **kwargs):

def distance(self, rect1, rect2):
"""Helper function for distance computing"""
return math.hypot(rect1.x - rect2.x, rect1.y - rect2.y)
return math.hypot(rect1.centerx - rect2.centerx, rect1.centery - rect2.centery)


def find_closest_npc(self, npcs):
Expand Down
Loading