]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
sword animation combat
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sun, 10 Jul 2022 03:02:56 +0000 (23:02 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sun, 10 Jul 2022 03:02:56 +0000 (23:02 -0400)
Assets/player/Sword_Slash.gif [new file with mode: 0644]
Player.py
Setup.py

diff --git a/Assets/player/Sword_Slash.gif b/Assets/player/Sword_Slash.gif
new file mode 100644 (file)
index 0000000..8f9cb6e
Binary files /dev/null and b/Assets/player/Sword_Slash.gif differ
index 611c387b9d426506fb73683354bcfb1dd12e698b..7da179402a14919aabcef9b364e983a8654fb0ff 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -11,6 +11,7 @@ from Weapon import Melee
 from CommonImports.colours import red, white
 from Function.createText import createText
 
+
 class Player(Actor):
     scale = 100
     factor = 640 / scale
@@ -18,6 +19,12 @@ class Player(Actor):
     idle_frames = [
         pg.transform.scale(pg.image.load(path.join("Assets/player/idle", file)).subsurface(pg.Rect(179, 169, 170, 401)),
                            (50, 100)) for file in listdir("Assets/player/idle")]
+
+    attack_gif = Image.open("Assets/player/Sword_Slash.gif")
+    attack_frames = []
+    for i in range(attack_gif.n_frames):
+        attack_frames.append(pg.transform.scale(pil_to_game(get_gif_frame(attack_gif, i)), (200,200)))
+
     width, height = idle_frames[0].get_size()
 
     colour = (52, 94, 235)
@@ -54,7 +61,14 @@ class Player(Actor):
         self.state = "IDLE"
         self.current_frame = 0
         self.display = self.idle_frames[0]
-        self.display_offsets = {"weapon": pg.Vector2(0, 0)}
+        self.display_offsets = {"weapon": pg.Vector2(0, 0), "player":pg.Vector2(0,0)}
+
+    # def attack(self, enemy, weapon):
+    #     super(Player, self).attack(enemy, weapon)
+    #     self.current_frame = 0
+    #     self.state = "ATTACK"
+    #     print('a')
+
 
     def update(self, delta):
         super().update(delta)
@@ -72,13 +86,16 @@ class Player(Actor):
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
 
         prev_direction = self.direction
-        if self.velocity.x == 0:
-            self.direction = 0
-        else:
-            self.direction = math.copysign(1, self.velocity.x)
+        # if self.velocity.x == 0:
+        #     self.direction = 0
+        # else:
+        #     self.direction = math.copysign(1, self.velocity.x)
+        self.direction = math.copysign(1, pg.mouse.get_pos()[0] - get_display_point(self.position).x)
         self.weapon.update(delta, self.position, self.direction)
 
         if self.state == "IDLE":
+            self.display_offsets["player"] = pg.Vector2(0,0)
+
             frame = math.floor(self.current_frame)
             if self.direction == 1:
                 self.display = self.idle_frames[math.floor(frame)]
@@ -99,7 +116,29 @@ class Player(Actor):
             else:
                 self.display_offsets["weapon"] = pg.Vector2(0, 0)
             self.current_frame = (self.current_frame + 0.1) % len(self.idle_frames)
+        elif self.state == "ATTACK":
+            frame = math.floor(self.current_frame)
+            if self.direction == 1:
+                self.display = self.attack_frames[math.floor(frame)]
+                self.display_offsets["player"] = pg.Vector2(-50,-50)
 
+            elif self.direction == -1:
+                self.display = pg.transform.flip(self.attack_frames[math.floor(frame)], True, False)
+                self.display_offsets["player"] = pg.Vector2(-100,-50)
+
+            else:
+                self.direction = prev_direction
+                if prev_direction == 1:
+                    self.display_offsets["player"] = pg.Vector2(-50,-50)
+
+                    self.display = self.attack_frames[math.floor(frame)]
+                elif prev_direction == -1:
+                    self.display = pg.transform.flip(self.attack_frames[math.floor(frame)], True, False)
+                    self.display_offsets["player"] = pg.Vector2(-100,-50)
+
+            self.current_frame += 0.4
+            if math.floor(self.current_frame) == self.attack_gif.n_frames:
+                self.state = "IDLE"
         return self.position - center
 
     def handle_input(self):
@@ -145,14 +184,17 @@ class Player(Actor):
         if mouse_pressed[0]:  # LMB
             if not self.weapon.attacking:
                 self.weapon.swing()
+                self.state = "ATTACK"
+                self.current_frame = 0
                 for mask in self.targets:
                     for enemy in mask:
                         if self.weapon.get_collision_rect().colliderect(enemy.get_collision_rect()):
                             enemy.attack(self, self.weapon)
 
+
     def draw(self, surf):
-        self.weapon.draw(surf, self.display_offsets["weapon"])
-        surf.blit(self.display, get_display_rect(self.get_collision_rect()))
+        self.weapon.draw(surf, self.display_offsets["weapon"])
+        surf.blit(self.display, get_display_rect(self.get_collision_rect()).topleft + self.display_offsets["player"])
         # super().draw(surf)
         # print(self.position, self.velocity, get_display_rect(self.get_collision_rect()).topleft, Setup.camera_offset)
         # pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=8)
index b36e7e7406a400409d78d1bd5024b08fbaf8f85f..b9e8cb6f07e784b37619e7172927aa6646ea1698 100644 (file)
--- a/Setup.py
+++ b/Setup.py
@@ -44,3 +44,15 @@ def get_display_point(vec):
     return vec - camera_offset
 
 from Area import Area
+
+from PIL import Image
+
+FORMAT = "RGBA"
+
+def pil_to_game(img):
+    data = img.tobytes("raw", FORMAT)
+    return pg.image.fromstring(data, img.size, FORMAT)
+
+def get_gif_frame(img, frame):
+    img.seek(frame)
+    return img.convert(FORMAT)
\ No newline at end of file