added a pnj

This commit is contained in:
ayabusa 2024-10-23 08:39:17 +02:00
parent a11042d61b
commit 5e34471405
4 changed files with 122 additions and 0 deletions

BIN
assets/MiniPeasant.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
assets/MiniVillagerMan.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
assets/MiniWorker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

122
game.py
View File

@ -2,6 +2,7 @@
import pygame
import math
import csv
import random
# pygame setup
pygame.init()
@ -13,6 +14,121 @@ dt = 0
player_sprite = pygame.Rect(100,100,50,50)
mur = pygame.Rect(500,100,100,100)
class Waypoint:
def __init__(self, name: str) -> None:
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:
letter = random.choice(self.connection)
return Waypoint(letter)
def get_direction(self, pointA: object, pointB: object)-> str:
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:
raise Exception("crash: POINT A and B impossible")
class Pnj:
def __init__(self, x: int, y: int, sprite_path: str, direction: str, objectif: Waypoint) -> None:
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
self.speed = 6
def avance(self):
'''
Vérifie que l'objectif n'est pas atteint et avance le pnj dans la direction
'''
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):
"""
Vérifie que l'objectif n'est pas atteint et renvoie true si un l'est, s'occupe aussi de réassigner un nouvel objectig
"""
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
class Player:
def __init__(self)-> None:
self.x = 800
@ -81,11 +197,15 @@ class Game:
img = pygame.transform.rotozoom(self.perso_sprite, -player.angle-90, 0.2)
screen.blit(img, (540-img.get_rect().centerx, 360-img.get_rect().centery))
def draw_villager(self):
screen.blit(pnj.sprite, (540-player.x + pnj.x - pnj.sprite.get_rect().centerx, 360-player.y + pnj.y - pnj.sprite.get_rect().centery))
def display_all(self):
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
screen.blit(self.map_sprite, (540-player.x, 360-player.y))
self.draw_villager()
self.draw_player()
# We display it at the end so it's on top of all
@ -98,6 +218,7 @@ class Game:
player = Player()
game = Game()
pnj = Pnj(800, 900, "assets/MiniPeasant.png", "south", Waypoint("H"))
while running:
# poll for events
@ -105,6 +226,7 @@ while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pnj.avance()
game.check_input()