Jeu-GTC/game.py

372 lines
16 KiB
Python
Raw Permalink Normal View History

2024-10-26 06:23:07 +00:00
# Here we import everything we need
2024-10-22 14:15:06 +00:00
import pygame
import math
import csv
2024-10-23 06:39:17 +00:00
import random
2024-10-22 14:15:06 +00:00
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1080,720))
clock = pygame.time.Clock()
running = True
dt = 0
2024-10-23 06:39:17 +00:00
class Waypoint:
def __init__(self, name: str) -> None:
2024-10-26 06:23:07 +00:00
"""Create the corresponding waypoint according to the letter specified in name: str"""
2024-10-23 06:39:17 +00:00
self.name = name
match name.capitalize():
case "A":
self.x = 800
self.y = 900
self.connection = ("B", "H")
case "B":
self.x = 1575
self.y = 900
self.connection = ("A", "E", "C")
case "C":
self.x = 2500
self.y = 900
self.connection = ("B", "D")
case "D":
self.x = 2500
self.y = 1600
self.connection = ("C", "E", "F")
case "E":
self.x = 1575
self.y = 1600
self.connection = ("B", "D", "G")
case "F":
self.x = 2500
self.y = 2200
self.connection = ("D", "G")
case "G":
self.x = 1575
self.y = 2200
self.connection = ("H", "E", "F")
case "H":
self.x = 800
self.y = 2200
self.connection = ("A", "G")
def get_new_connected(self)-> object:
2024-10-26 06:23:07 +00:00
"""Return a new waypoint connected to the current one"""
2024-10-23 06:39:17 +00:00
letter = random.choice(self.connection)
return Waypoint(letter)
def get_direction(self, pointA: object, pointB: object)-> str:
2024-10-26 06:23:07 +00:00
"""Helper that returns the direction: str from pointA: Waypoint, to pointB: Waypoint"""
2024-10-23 06:39:17 +00:00
a = pointA.name
b = pointB.name
if (a == "H" and b == "A") or (a == "G" and b == "E") or (a == "E" and b == "B") or (a == "F" and b == "D") or (a == "D" and b == "C"):
return "north"
elif (a == "A" and b == "H") or (a == "E" and b == "G") or (a == "B" and b == "E") or (a == "D" and b == "F") or (a == "C" and b == "D"):
return "south"
elif (a == "A" and b == "B") or (a == "B" and b == "C") or (a == "E" and b == "D") or (a == "H" and b == "G") or (a == "G" and b == "F"):
return "east"
elif (a == "B" and b == "A") or (a == "C" and b == "B") or (a == "D" and b == "E") or (a == "G" and b == "H") or (a == "F" and b == "G"):
return "west"
else:
2024-10-26 06:23:07 +00:00
# If the direction is impossible
2024-10-23 06:39:17 +00:00
raise Exception("crash: POINT A and B impossible")
class Pnj:
2024-10-23 09:58:57 +00:00
def __init__(self, x: int, y: int, sprite_path: str, direction: str, objectif: Waypoint, speed: int) -> None:
2024-10-26 06:23:07 +00:00
"""Create a single pnj with the following attributes:
- x: int, x coordinate
- y: int, y coordinate
- sprite_path: str, the path to the sprite of the pnj
- direction: str, the direction can be "north", "south", "east", or "west"
- objectif: Waypoint, the goal of the pnj
- speed: int, the desired speed of the pnj"""
2024-10-23 06:39:17 +00:00
self.x = x
self.y = y
self.sprite = pygame.transform.scale_by(pygame.image.load(sprite_path), 3.0)
self.direction = direction
self.objectif = objectif
2024-10-23 09:58:57 +00:00
self.speed = speed
2024-10-23 18:11:41 +00:00
self.alive = True
2024-10-23 06:39:17 +00:00
def avance(self):
'''
Vérifie que l'objectif n'est pas atteint et avance le pnj dans la direction
'''
2024-10-23 18:11:41 +00:00
if not self.alive:
return
2024-10-23 06:39:17 +00:00
if self.check_objectif():
return
else:
match self.direction:
case "north":
self.y -= self.speed
case "south":
self.y += self.speed
case "east":
self.x += self.speed
case "west":
self.x -= self.speed
case _:
pass
def check_objectif(self):
"""
2024-10-26 06:23:07 +00:00
Vérifie que l'objectif n'est pas atteint et renvoie true si un l'est, s'occupe aussi de réassigner un nouvel objectif
2024-10-23 06:39:17 +00:00
"""
match self.direction:
case "north":
if self.y <= self.objectif.y:
new_objectif = self.objectif.get_new_connected()
self.direction = self.objectif.get_direction(self.objectif, new_objectif)
self.objectif = new_objectif
return True
case "south":
if self.y >= self.objectif.y:
new_objectif = self.objectif.get_new_connected()
self.direction = self.objectif.get_direction(self.objectif, new_objectif)
self.objectif = new_objectif
return True
case "east":
if self.x >= self.objectif.x:
new_objectif = self.objectif.get_new_connected()
self.direction = self.objectif.get_direction(self.objectif, new_objectif)
self.objectif = new_objectif
return True
case "west":
if self.x <= self.objectif.x:
new_objectif = self.objectif.get_new_connected()
self.direction = self.objectif.get_direction(self.objectif, new_objectif)
self.objectif = new_objectif
return True
return False
2024-10-23 18:11:41 +00:00
def kill(self):
2024-10-26 06:23:07 +00:00
"""Kill the PNJ by replacing it with a blood splatter"""
2024-10-23 18:11:41 +00:00
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()
2024-10-24 08:30:42 +00:00
# play sound effect
blood = pygame.mixer.Sound("assets/blood-splatter.mp3")
pygame.mixer.Sound.play(blood)
2024-10-23 06:39:17 +00:00
2024-10-23 09:58:57 +00:00
class Village:
def __init__(self, nb_pnj: int) -> None:
2024-10-26 06:23:07 +00:00
"""This class defines the village of pnj in it's globality and nb_pnj (int) determines how many pnj should be generated"""
2024-10-23 09:58:57 +00:00
self.liste_pnj = []
2024-10-26 06:23:07 +00:00
# here we generate each pnj randomly and store it in liste_pnj
2024-10-23 09:58:57 +00:00
for i in range(nb_pnj):
start_waypoint = Waypoint(random.choice(("A", "B", "C", "D", "E", "F", "G", "H")))
objective_waypoint = start_waypoint.get_new_connected()
2024-10-23 10:38:59 +00:00
self.liste_pnj.append(Pnj(start_waypoint.x, start_waypoint.y, "assets/pnj/pnj"+str(random.randint(1,8))+".png", start_waypoint.get_direction(start_waypoint, objective_waypoint), objective_waypoint, random.randint(1, 4)))
2024-10-26 06:23:07 +00:00
2024-10-23 09:58:57 +00:00
def update_pnj(self)-> None:
2024-10-26 06:23:07 +00:00
"""Make each pnj move a little bit"""
2024-10-23 09:58:57 +00:00
for p in self.liste_pnj:
p.avance()
def get_village_sprites(self)->list:
2024-10-26 06:23:07 +00:00
"""Method that returns a list of tuple containing the coordinates and sprite of a pnj
The tuple is in this shape: (x, y, sprite)"""
2024-10-23 09:58:57 +00:00
village_sprites = []
2024-10-26 06:23:07 +00:00
for p in self.liste_pnj:
2024-10-23 09:58:57 +00:00
village_sprites.append((p.x, p.y, p.sprite))
return village_sprites
2024-10-23 18:11:41 +00:00
def check_kill(self)->None:
2024-10-26 06:23:07 +00:00
"""Check for each pnj if he is being killed by the player"""
2024-10-23 18:11:41 +00:00
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:
2024-10-26 06:23:07 +00:00
"""Called when all original pnj where killed, it create an army of really fast knight"""
2024-10-23 18:11:41 +00:00
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))
2024-10-22 14:15:06 +00:00
class Player:
def __init__(self)-> None:
2024-10-26 06:23:07 +00:00
"""This class defines how the player can behave,
it should be created as soon as possible and only once in the game"""
2024-10-22 14:15:06 +00:00
self.x = 800
self.y = 900
self.mov_speed = 8
self.rotate_speed = 5
self.angle = 90
2024-10-23 18:11:41 +00:00
self.killcounter = 0
2024-10-22 14:15:06 +00:00
def rotate(self, angle: math.degrees)-> None:
2024-10-26 06:23:07 +00:00
"""Rotate the player to the desired angle"""
2024-10-22 14:15:06 +00:00
self.angle += angle*self.rotate_speed
def move(self, mov: int)-> None:
2024-10-26 06:23:07 +00:00
"""Move the player in the direction he is looking or backward if mov is negative"""
2024-10-22 14:15:06 +00:00
new_x = self.x + self.mov_speed*mov*math.cos(math.radians(self.angle))
new_y = self.y + self.mov_speed*mov*math.sin(math.radians(self.angle))
if(game.test_collision(pygame.Rect(new_x-50, new_y-50, 100, 100), game.collisions) == False):
self.x = new_x
self.y = new_y
elif(game.test_collision(pygame.Rect(self.x-50, new_y-50, 100, 100), game.collisions) == False):
self.y = new_y
elif(game.test_collision(pygame.Rect(new_x-50, self.y-50, 100, 100), game.collisions) == False):
self.x = new_x
2024-10-23 18:11:41 +00:00
village.check_kill()
2024-10-22 14:15:06 +00:00
class Game:
def __init__(self):
2024-10-26 06:23:07 +00:00
"""The main class that defines the game and it's behaviour,
it should be created only once during the game, as soon as possible"""
2024-10-22 14:15:06 +00:00
self.is_paused = False
2024-10-24 08:30:42 +00:00
self.pnj_number = 20
2024-10-22 14:15:06 +00:00
pygame.font.init()
2024-10-24 08:30:42 +00:00
self.font = pygame.font.Font('assets/upheavtt.ttf', 60)
2024-10-22 14:15:06 +00:00
self.load_sprites()
self.load_collisions()
def load_sprites(self):
2024-10-26 06:23:07 +00:00
"""Useful to load sprites in memory, should be called as soon as possible to avoid errors"""
2024-10-23 18:11:41 +00:00
self.perso_sprite = pygame.image.load("assets/Horse2.png")
2024-10-22 14:15:06 +00:00
self.map_sprite = pygame.image.load("assets/map3.png")
def load_collisions(self):
2024-10-26 06:23:07 +00:00
"""Load all collisions stored in the sheets located in assets/collisions.csv,
should be called as soon as possible to avoid errors"""
2024-10-22 14:15:06 +00:00
self.collisions = []
with open('assets/collisions.csv', 'r', newline='') as file:
reader = csv.DictReader(file)
for row in reader:
self.collisions.append(pygame.Rect(float(row["starting_point_x"]), float(row["starting_point_y"]), float(row["len_x"]), float(row["len_y"])))
2024-10-26 06:23:07 +00:00
def test_collision(self, objet: pygame.Rect, l_collisions: list) -> bool:
"""This is an helper to check if there is collision between an object (pygame.Rect) and a list
of collisions containing pygame.Rect"""
2024-10-22 14:15:06 +00:00
for col in l_collisions:
if objet.colliderect(col):
return True
return False
def display_debug_text(self):
2024-10-26 06:23:07 +00:00
"""This is only for debuging and should be called in display_all() if in debug mode,
but not on the final game"""
2024-10-22 14:15:06 +00:00
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))
y_surface = self.font.render("y: "+str(player.y), False, (0, 0, 0))
2024-10-23 18:11:41 +00:00
kill_surface = self.font.render("kills: "+str(player.killcounter), False, (0, 0, 0))
2024-10-22 14:15:06 +00:00
screen.blit(angle_surface, (0,0))
screen.blit(x_surface, (0,30))
screen.blit(y_surface, (0,60))
2024-10-23 18:11:41 +00:00
screen.blit(kill_surface, (0,90))
2024-10-22 14:15:06 +00:00
#pygame.draw.rect(screen, "blue",pygame.Rect(player.x-50, player.y-50, 100, 100))
2024-10-24 08:30:42 +00:00
def display_ui(self):
2024-10-26 06:23:07 +00:00
"""Handle the display of the killcounter and time,
should be called at each frames"""
2024-10-24 08:30:42 +00:00
color = "black"
2024-10-26 06:23:07 +00:00
2024-10-24 08:30:42 +00:00
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))
2024-10-22 14:15:06 +00:00
def check_input(self):
2024-10-26 06:23:07 +00:00
"""Here we check all possible user input and execut and action acordingly"""
2024-10-22 14:15:06 +00:00
keys = pygame.key.get_pressed()
if keys[pygame.K_z]:
_ = player.move(1)
if keys[pygame.K_s]:
_ = player.move(-1)
if keys[pygame.K_q]:
player.rotate(-1)
if keys[pygame.K_d]:
player.rotate(1)
def draw_player(self):
2024-10-26 06:23:07 +00:00
"""Draw the player after applying a rotozoom in the center of the screen"""
2024-10-23 18:11:41 +00:00
img = pygame.transform.rotozoom(self.perso_sprite, -player.angle-90, 1)
2024-10-22 14:15:06 +00:00
screen.blit(img, (540-img.get_rect().centerx, 360-img.get_rect().centery))
2024-10-23 09:58:57 +00:00
def draw_village(self):
2024-10-26 06:23:07 +00:00
"""Draw each sprite of the village"""
2024-10-23 09:58:57 +00:00
for s in village.get_village_sprites():
screen.blit(s[2], (540-player.x + s[0] - s[2].get_rect().centerx, 360-player.y + s[1] - s[2].get_rect().centery))
2024-10-23 06:39:17 +00:00
2024-10-22 14:15:06 +00:00
def display_all(self):
2024-10-26 06:23:07 +00:00
"""This is a helper that will render everything needed on the screen"""
# fill the screen with a color to wipe away anything from last frame and draw the map
2024-10-23 18:11:41 +00:00
screen.fill("gray")
2024-10-22 14:15:06 +00:00
screen.blit(self.map_sprite, (540-player.x, 360-player.y))
2024-10-23 09:58:57 +00:00
self.draw_village()
2024-10-22 14:15:06 +00:00
self.draw_player()
2024-10-26 06:23:07 +00:00
# We display them at the end so it's on top of all
#self.display_debug_text()
2024-10-24 08:30:42 +00:00
self.display_ui()
2024-10-22 14:15:06 +00:00
2024-10-26 06:23:07 +00:00
# flip() the display to put our work on screen
2024-10-22 14:15:06 +00:00
pygame.display.flip()
2024-10-23 18:11:41 +00:00
def game_over(self)->None:
2024-10-26 06:23:07 +00:00
"""Create a gameover loop displaying the scoreboard and an image"""
2024-10-23 18:11:41 +00:00
screen.blit(pygame.image.load("assets/gameover.png"), (0,0))
2024-10-24 08:30:42 +00:00
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))
2024-10-23 18:11:41 +00:00
# flip() the display to put your work on screen
pygame.display.flip()
2024-10-24 08:30:42 +00:00
2024-10-23 18:11:41 +00:00
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
2024-10-26 06:23:07 +00:00
# Here we create the principal objects that will always be used during the game
2024-10-22 14:15:06 +00:00
player = Player()
game = Game()
2024-10-23 18:11:41 +00:00
village = Village(game.pnj_number)
2024-10-22 14:15:06 +00:00
2024-10-26 06:23:07 +00:00
# start music with -1 meaning infinite loop
2024-10-24 08:30:42 +00:00
pygame.mixer.music.load("assets/music.mp3")
pygame.mixer.music.play(-1)
2024-10-26 06:23:07 +00:00
# Infinite game loop
2024-10-22 14:15:06 +00:00
while running:
# poll for events
2024-10-26 06:23:07 +00:00
# pygame.QUIT event means the user clicked X on the window
2024-10-22 14:15:06 +00:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
2024-10-23 09:58:57 +00:00
# make all the pnj move and do their pnj stuff
village.update_pnj()
2024-10-22 14:15:06 +00:00
2024-10-26 06:23:07 +00:00
# Here we check all inputs from the user and do what is requiered
2024-10-22 14:15:06 +00:00
game.check_input()
2024-10-26 06:23:07 +00:00
# Here we display everything to the screen
2024-10-22 14:15:06 +00:00
game.display_all()
# limits FPS to 60
2024-10-26 06:23:07 +00:00
# dt is delta time in seconds since last frame
2024-10-22 14:15:06 +00:00
dt = clock.tick(60) / 1000
2024-10-26 06:23:07 +00:00
# cleanly quit the pygame instance
2024-10-22 14:15:06 +00:00
pygame.quit()