added sound

This commit is contained in:
ayabusa 2024-10-24 10:30:42 +02:00
parent 2bd9fec9f9
commit 1a40951950
5 changed files with 39 additions and 4 deletions

BIN
assets/RETROTECH.ttf Normal file

Binary file not shown.

BIN
assets/blood-splatter.mp3 Normal file

Binary file not shown.

BIN
assets/music.mp3 Normal file

Binary file not shown.

BIN
assets/upheavtt.ttf Normal file

Binary file not shown.

43
game.py
View File

@ -3,6 +3,7 @@ import pygame
import math import math
import csv import csv
import random import random
import datetime
# pygame setup # pygame setup
pygame.init() pygame.init()
@ -139,6 +140,9 @@ class Pnj:
village.final_boss() village.final_boss()
elif player.killcounter >= game.pnj_number+8: elif player.killcounter >= game.pnj_number+8:
game.game_over() game.game_over()
# play sound effect
blood = pygame.mixer.Sound("assets/blood-splatter.mp3")
pygame.mixer.Sound.play(blood)
class Village: class Village:
def __init__(self, nb_pnj: int) -> None: def __init__(self, nb_pnj: int) -> None:
@ -173,7 +177,6 @@ class Village:
objective_waypoint = start_waypoint.get_new_connected() 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)) 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:
self.x = 800 self.x = 800
@ -201,9 +204,9 @@ class Player:
class Game: class Game:
def __init__(self): def __init__(self):
self.is_paused = False self.is_paused = False
self.pnj_number = 5 self.pnj_number = 20
pygame.font.init() pygame.font.init()
self.font = pygame.font.SysFont('Comic Sans MS', 30) self.font = pygame.font.Font('assets/upheavtt.ttf', 60)
self.load_sprites() self.load_sprites()
self.load_collisions() self.load_collisions()
@ -235,6 +238,22 @@ class Game:
screen.blit(kill_surface, (0,90)) 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 display_ui(self):
color = "black"
if player.killcounter < game.pnj_number:
kill_surface = self.font.render("Kills: "+str(player.killcounter)+"\\"+str(game.pnj_number), False, color)
else:
kill_surface = self.font.render("Kills: "+str(player.killcounter)+"\\"+str(game.pnj_number+8), False, color)
screen.blit(kill_surface, (10,0))
seconds = pygame.time.get_ticks()/1000
seconds = seconds % (24 * 3600)
seconds %= 3600
minutes = seconds // 60
seconds %= 60
time_surface = self.font.render("Time: "+str(int(minutes)).zfill(2)+":"+str(int(seconds)).zfill(2), False, color)
screen.blit(time_surface, (740,0))
def check_input(self): def check_input(self):
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_z]: if keys[pygame.K_z]:
@ -264,15 +283,26 @@ class Game:
self.draw_player() self.draw_player()
# We display it at the end so it's on top of all # We display it at the end so it's on top of all
self.display_debug_text() # self.display_debug_text()
self.display_ui()
# 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: def game_over(self)->None:
screen.blit(pygame.image.load("assets/gameover.png"), (0,0)) screen.blit(pygame.image.load("assets/gameover.png"), (0,0))
seconds = pygame.time.get_ticks()/1000
seconds = seconds % (24 * 3600)
seconds %= 3600
minutes = seconds // 60
seconds %= 60
time_surface = self.font.render(str(int(minutes)).zfill(2)+":"+str(int(seconds)).zfill(2), False, "white")
screen.blit(time_surface, (500,500))
# flip() the display to put your work on screen # flip() the display to put your work on screen
pygame.display.flip() pygame.display.flip()
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -283,6 +313,11 @@ 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(game.pnj_number) village = Village(game.pnj_number)
# start music
pygame.mixer.music.load("assets/music.mp3")
pygame.mixer.music.play(-1)
while running: while running:
# poll for events # poll for events
# pygame.QUIT event means the user clicked X to close your window # pygame.QUIT event means the user clicked X to close your window