added gameover

This commit is contained in:
ayabusa 2024-10-23 20:11:41 +02:00
parent 4281545f3c
commit 2bd9fec9f9
9 changed files with 43 additions and 4 deletions

BIN
assets/Horse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 869 B

BIN
assets/Horse.xcf Normal file

Binary file not shown.

BIN
assets/Horse2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 996 B

BIN
assets/blood.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 906 B

BIN
assets/blood.xcf Normal file

Binary file not shown.

BIN
assets/gameover.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 934 KiB

BIN
assets/gameover.xcf Normal file

Binary file not shown.

BIN
assets/pnj/knight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

47
game.py
View File

@ -77,11 +77,14 @@ class Pnj:
self.direction = direction self.direction = direction
self.objectif = objectif self.objectif = objectif
self.speed = speed self.speed = speed
self.alive = True
def avance(self): def avance(self):
''' '''
Vérifie que l'objectif n'est pas atteint et avance le pnj dans la direction Vérifie que l'objectif n'est pas atteint et avance le pnj dans la direction
''' '''
if not self.alive:
return
if self.check_objectif(): if self.check_objectif():
return return
else: else:
@ -127,6 +130,15 @@ class Pnj:
self.objectif = new_objectif self.objectif = new_objectif
return True return True
return False return False
def kill(self):
self.sprite = pygame.transform.scale_by(pygame.image.load("assets/blood.png"), 3.0)
self.alive = False
player.killcounter += 1
if player.killcounter == game.pnj_number:
village.final_boss()
elif player.killcounter >= game.pnj_number+8:
game.game_over()
class Village: class Village:
def __init__(self, nb_pnj: int) -> None: def __init__(self, nb_pnj: int) -> None:
@ -148,6 +160,19 @@ class Village:
for p in self.liste_pnj: for p in self.liste_pnj:
village_sprites.append((p.x, p.y, p.sprite)) village_sprites.append((p.x, p.y, p.sprite))
return village_sprites return village_sprites
def check_kill(self)->None:
player_rect = pygame.Rect(player.x-50, player.y-50, 100, 100)
for p in self.liste_pnj:
if p.alive and player_rect.collidepoint(p.x, p.y):
p.kill()
def final_boss(self)->None:
for i in ("A", "B", "C", "D", "E", "F", "G", "H"):
start_waypoint = Waypoint(i)
objective_waypoint = start_waypoint.get_new_connected()
self.liste_pnj.append(Pnj(start_waypoint.x, start_waypoint.y, "assets/pnj/knight.png", start_waypoint.get_direction(start_waypoint, objective_waypoint), objective_waypoint, 10))
class Player: class Player:
def __init__(self)-> None: def __init__(self)-> None:
@ -156,6 +181,7 @@ class Player:
self.mov_speed = 8 self.mov_speed = 8
self.rotate_speed = 5 self.rotate_speed = 5
self.angle = 90 self.angle = 90
self.killcounter = 0
def rotate(self, angle: math.degrees)-> None: def rotate(self, angle: math.degrees)-> None:
self.angle += angle*self.rotate_speed self.angle += angle*self.rotate_speed
@ -170,17 +196,19 @@ class Player:
self.y = new_y self.y = new_y
elif(game.test_collision(pygame.Rect(new_x-50, self.y-50, 100, 100), game.collisions) == False): elif(game.test_collision(pygame.Rect(new_x-50, self.y-50, 100, 100), game.collisions) == False):
self.x = new_x self.x = new_x
village.check_kill()
class Game: class Game:
def __init__(self): def __init__(self):
self.is_paused = False self.is_paused = False
self.pnj_number = 5
pygame.font.init() pygame.font.init()
self.font = pygame.font.SysFont('Comic Sans MS', 30) self.font = pygame.font.SysFont('Comic Sans MS', 30)
self.load_sprites() self.load_sprites()
self.load_collisions() self.load_collisions()
def load_sprites(self): def load_sprites(self):
self.perso_sprite = pygame.image.load("assets/perso_sprite.png") self.perso_sprite = pygame.image.load("assets/Horse2.png")
self.map_sprite = pygame.image.load("assets/map3.png") self.map_sprite = pygame.image.load("assets/map3.png")
def load_collisions(self): def load_collisions(self):
@ -200,9 +228,11 @@ class Game:
angle_surface = self.font.render("angle: "+str(player.angle), False, (0, 0, 0)) angle_surface = self.font.render("angle: "+str(player.angle), False, (0, 0, 0))
x_surface = self.font.render("x: "+str(player.x), False, (0, 0, 0)) x_surface = self.font.render("x: "+str(player.x), False, (0, 0, 0))
y_surface = self.font.render("y: "+str(player.y), False, (0, 0, 0)) y_surface = self.font.render("y: "+str(player.y), False, (0, 0, 0))
kill_surface = self.font.render("kills: "+str(player.killcounter), False, (0, 0, 0))
screen.blit(angle_surface, (0,0)) screen.blit(angle_surface, (0,0))
screen.blit(x_surface, (0,30)) screen.blit(x_surface, (0,30))
screen.blit(y_surface, (0,60)) screen.blit(y_surface, (0,60))
screen.blit(kill_surface, (0,90))
#pygame.draw.rect(screen, "blue",pygame.Rect(player.x-50, player.y-50, 100, 100)) #pygame.draw.rect(screen, "blue",pygame.Rect(player.x-50, player.y-50, 100, 100))
def check_input(self): def check_input(self):
@ -218,7 +248,7 @@ class Game:
def draw_player(self): def draw_player(self):
#pygame.draw.rect(screen, "red", pygame.Rect(player.x, player.y, 50, 50)) #pygame.draw.rect(screen, "red", pygame.Rect(player.x, player.y, 50, 50))
img = pygame.transform.rotozoom(self.perso_sprite, -player.angle-90, 0.2) img = pygame.transform.rotozoom(self.perso_sprite, -player.angle-90, 1)
screen.blit(img, (540-img.get_rect().centerx, 360-img.get_rect().centery)) screen.blit(img, (540-img.get_rect().centerx, 360-img.get_rect().centery))
def draw_village(self): def draw_village(self):
@ -227,7 +257,7 @@ class Game:
def display_all(self): def display_all(self):
# fill the screen with a color to wipe away anything from last frame # fill the screen with a color to wipe away anything from last frame
screen.fill("purple") screen.fill("gray")
screen.blit(self.map_sprite, (540-player.x, 360-player.y)) screen.blit(self.map_sprite, (540-player.x, 360-player.y))
self.draw_village() self.draw_village()
@ -239,10 +269,19 @@ class Game:
# flip() the display to put your work on screen # flip() the display to put your work on screen
pygame.display.flip() pygame.display.flip()
def game_over(self)->None:
screen.blit(pygame.image.load("assets/gameover.png"), (0,0))
# flip() the display to put your work on screen
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
player = Player() player = Player()
game = Game() game = Game()
#pnj = Pnj(800, 900, "assets/MiniPeasant.png", "south", Waypoint("H"), 3) #pnj = Pnj(800, 900, "assets/MiniPeasant.png", "south", Waypoint("H"), 3)
village = Village(20) village = Village(game.pnj_number)
while running: while running:
# poll for events # poll for events