Skip to content

Commit 84bd251

Browse files
committed
Add lives for player
1 parent d829324 commit 84bd251

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Diff for: game.py

+2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def update(self):
6464

6565
def draw(self):
6666
self.level.draw(self.screen)
67+
# self.screen.blit(self.player.hearts.image)
6768
self.all_sprite_list.draw(self.screen)
69+
self.player.hearts.draw(self.screen)
6870
self.timer.display(self.screen)
6971

7072
def is_player_win(self):

Diff for: player.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pygame
2+
from nax.items import Background
23
from nax.spritesheet import SpriteSheet
34
from nax import PROJECT_DIR, SCREEN_HEIGHT, COLORS
45

@@ -18,6 +19,13 @@ def __init__(self, x, y, speed=10):
1819
self.is_die = False
1920
self.is_hit = False
2021
self.hit_times = 0
22+
self.lives = 3
23+
self.hearts = pygame.sprite.Group()
24+
25+
i = 0
26+
while i < self.lives:
27+
self.hearts.add(Background(PROJECT_DIR+"/assets/HUD/hud_heartFull.png", [15 + (i*70), 20]))
28+
i += 1
2129

2230
# Make our top-left corner the passed-in location.
2331
sprite_sheet = SpriteSheet(PROJECT_DIR+"/assets/Player/p1_walk/p1_walk.png")
@@ -174,6 +182,15 @@ def _calc_collide_enemies(self):
174182
for block in block_hit_list:
175183
diff = self.rect.x - block.rect.x
176184
self.is_hit = True
185+
self.lives -= 1
186+
187+
if self.lives >= 0:
188+
self.hearts.remove(self.hearts.sprites()[-1])
189+
190+
if self.lives == 0:
191+
self.is_die = True
192+
193+
# self.hearts.remove(self.hearts.)
177194
if (diff > 0):
178195
self.change_x = 15
179196
self.change_y = 15

Diff for: window.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import pygame
2-
from time import sleep
32
from nax.game import Game
4-
from nax.timer import GameTimer
5-
from nax import COLORS, SCREEN_WIDTH
63

74

85
class Window(object):

0 commit comments

Comments
 (0)