]> git.ayabusa.dev Git - jeu-gtc.git/commitdiff
added a pnj
authorayabusa <lebgpub@gmail.com>
Wed, 23 Oct 2024 06:39:17 +0000 (08:39 +0200)
committerayabusa <lebgpub@gmail.com>
Wed, 23 Oct 2024 06:39:17 +0000 (08:39 +0200)
assets/MiniPeasant.png [new file with mode: 0644]
assets/MiniVillagerMan.png [new file with mode: 0644]
assets/MiniWorker.png [new file with mode: 0644]
game.py

diff --git a/assets/MiniPeasant.png b/assets/MiniPeasant.png
new file mode 100644 (file)
index 0000000..4f62827
Binary files /dev/null and b/assets/MiniPeasant.png differ
diff --git a/assets/MiniVillagerMan.png b/assets/MiniVillagerMan.png
new file mode 100644 (file)
index 0000000..652f9d9
Binary files /dev/null and b/assets/MiniVillagerMan.png differ
diff --git a/assets/MiniWorker.png b/assets/MiniWorker.png
new file mode 100644 (file)
index 0000000..0bdd8c7
Binary files /dev/null and b/assets/MiniWorker.png differ
diff --git a/game.py b/game.py
index 404be86d9d8232ed5637963aa3b4e308ca21f5ae..aeeef07e7c78e89273568260860d36ab5dda367a 100644 (file)
--- a/game.py
+++ b/game.py
@@ -2,6 +2,7 @@
 import pygame\r
 import math\r
 import csv\r
+import random\r
 \r
 # pygame setup\r
 pygame.init()\r
@@ -13,6 +14,121 @@ dt = 0
 player_sprite = pygame.Rect(100,100,50,50)\r
 mur = pygame.Rect(500,100,100,100)\r
 \r
+class Waypoint:\r
+    def __init__(self, name: str) -> None:\r
+        self.name = name\r
+        match name.capitalize():\r
+            case "A":\r
+                self.x = 800\r
+                self.y = 900\r
+                self.connection = ("B", "H")\r
+            case "B":\r
+                self.x = 1575\r
+                self.y = 900\r
+                self.connection = ("A", "E", "C")\r
+            case "C":\r
+                self.x = 2500\r
+                self.y = 900\r
+                self.connection = ("B", "D")\r
+            case "D":\r
+                self.x = 2500\r
+                self.y = 1600\r
+                self.connection = ("C", "E", "F")\r
+            case "E":\r
+                self.x = 1575\r
+                self.y = 1600\r
+                self.connection = ("B", "D", "G")\r
+            case "F":\r
+                self.x = 2500\r
+                self.y = 2200\r
+                self.connection = ("D", "G")\r
+            case "G":\r
+                self.x = 1575\r
+                self.y = 2200\r
+                self.connection = ("H", "E", "F")\r
+            case "H":\r
+                self.x = 800\r
+                self.y = 2200\r
+                self.connection = ("A", "G")\r
+\r
+    def get_new_connected(self)-> object:\r
+        letter = random.choice(self.connection)\r
+        return Waypoint(letter)\r
+    \r
+    def get_direction(self, pointA: object, pointB: object)-> str:\r
+        a = pointA.name\r
+        b = pointB.name\r
+        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"):\r
+            return "north"\r
+        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"):\r
+            return "south"\r
+        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"):\r
+            return "east"\r
+        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"):\r
+            return "west"\r
+        else:\r
+            raise Exception("crash: POINT A and B impossible")\r
+                \r
+\r
+class Pnj:\r
+    def __init__(self, x: int, y: int, sprite_path: str, direction: str, objectif: Waypoint) -> None:\r
+        self.x = x\r
+        self.y = y\r
+        self.sprite = pygame.transform.scale_by(pygame.image.load(sprite_path), 3.0)\r
+        self.direction = direction\r
+        self.objectif = objectif\r
+        self.speed = 6\r
+    \r
+    def avance(self):\r
+        '''\r
+        Vérifie que l'objectif n'est pas atteint et avance le pnj dans la direction\r
+        '''\r
+        if self.check_objectif():\r
+            return\r
+        else:\r
+            match self.direction:\r
+                case "north":\r
+                    self.y -= self.speed\r
+                case "south":\r
+                    self.y += self.speed\r
+                case "east":\r
+                    self.x += self.speed\r
+                case "west":\r
+                    self.x -= self.speed\r
+                case _:\r
+                    pass\r
+    \r
+    def check_objectif(self):\r
+        """\r
+        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\r
+        """\r
+        match self.direction:\r
+            case "north":\r
+                if self.y <= self.objectif.y: \r
+                    new_objectif = self.objectif.get_new_connected()\r
+                    self.direction = self.objectif.get_direction(self.objectif, new_objectif)\r
+                    self.objectif = new_objectif\r
+                    return True\r
+            case "south":\r
+                if self.y >= self.objectif.y: \r
+                    new_objectif = self.objectif.get_new_connected()\r
+                    self.direction = self.objectif.get_direction(self.objectif, new_objectif)\r
+                    self.objectif = new_objectif\r
+                    return True\r
+            case "east":\r
+                if self.x >= self.objectif.x: \r
+                    new_objectif = self.objectif.get_new_connected()\r
+                    self.direction = self.objectif.get_direction(self.objectif, new_objectif)\r
+                    self.objectif = new_objectif\r
+                    return True\r
+            case "west":\r
+                if self.x <= self.objectif.x: \r
+                    new_objectif = self.objectif.get_new_connected()\r
+                    self.direction = self.objectif.get_direction(self.objectif, new_objectif)\r
+                    self.objectif = new_objectif\r
+                    return True\r
+        return False\r
+\r
 class Player:\r
     def __init__(self)-> None:\r
         self.x = 800\r
@@ -81,11 +197,15 @@ class Game:
         img = pygame.transform.rotozoom(self.perso_sprite, -player.angle-90, 0.2)\r
         screen.blit(img, (540-img.get_rect().centerx, 360-img.get_rect().centery))\r
 \r
+    def draw_villager(self):\r
+        screen.blit(pnj.sprite, (540-player.x + pnj.x - pnj.sprite.get_rect().centerx, 360-player.y + pnj.y - pnj.sprite.get_rect().centery))\r
+\r
     def display_all(self):\r
         # fill the screen with a color to wipe away anything from last frame\r
         screen.fill("purple")\r
         screen.blit(self.map_sprite, (540-player.x, 360-player.y))\r
 \r
+        self.draw_villager()\r
         self.draw_player()\r
 \r
         # We display it at the end so it's on top of all\r
@@ -98,6 +218,7 @@ class Game:
 \r
 player = Player()\r
 game = Game()\r
+pnj = Pnj(800, 900, "assets/MiniPeasant.png", "south", Waypoint("H"))\r
 \r
 while running:\r
     # poll for events\r
@@ -105,6 +226,7 @@ while running:
     for event in pygame.event.get():\r
         if event.type == pygame.QUIT:\r
             running = False\r
+    pnj.avance()\r
 \r
     game.check_input()\r
     \r