]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
Created actors or things that move in the game
authorSkullheadx <704277@pdsb.net>
Wed, 6 Jul 2022 16:21:39 +0000 (12:21 -0400)
committerSkullheadx <704277@pdsb.net>
Wed, 6 Jul 2022 16:21:39 +0000 (12:21 -0400)
Actors.py [new file with mode: 0644]
Area.py
Enemy.py
Game.py
Player.py
Setup.py

diff --git a/Actors.py b/Actors.py
new file mode 100644 (file)
index 0000000..fd1c1d7
--- /dev/null
+++ b/Actors.py
@@ -0,0 +1,108 @@
+from Setup import *
+
+class Actor:
+    width, height = 50, 100
+    colour = (76, 82, 92)
+    speed = 0.2
+    jump_strength = 1
+    gravity = 0.098
+    friction = 0.9
+    
+
+    def __init__(self, pos, collision_layer, collision_mask):
+        self.position = pg.Vector2(pos)
+        self.velocity = pg.Vector2(0, 0)
+
+        self.on_ground = False
+        
+        collision_layer.add(self)  # the layer the actor is on for collisions
+        self.collision_mask = collision_mask  # the layer the actor detects collisions against
+
+        self.health = 100
+
+        self.areas = dict()
+
+
+    def update(self, delta):
+        for area in self.areas.values():
+            area.update(delta,self.position)
+
+        # Apply friction so the enemy isn't walking on ice
+        if self.on_ground:
+            self.velocity.x *= self.friction 
+
+        # Apply gravity
+        self.velocity.y += self.gravity
+
+
+
+    def follow_target(self, node):
+        target = node.position
+
+        # So that actor doesn't come up and hug u lol
+        if (self.position - target).length_squared() < pow(max(self.height, self.width) * 1.5,2):
+            return
+
+        if target.x < self.position.x:
+            self.move_left()
+        elif target.x > self.position.x:
+            self.move_right()
+
+        if target.y < self.position.y:
+            self.jump()
+
+    def jump(self):
+        if self.on_ground:
+            self.velocity.y = -self.jump_strength
+
+    def move_left(self):
+        self.velocity.x = -self.speed
+
+    def move_right(self):
+        self.velocity.x = self.speed
+
+    def move_and_collide(self, pos, vel, delta):
+        pos.x += vel.x * delta
+        collision_rect = self.get_collision_rect(pos)
+        for mask in self.collision_mask:
+            for thing in mask:
+                if thing == self:
+                    continue
+                if collision_rect.colliderect(thing.get_collision_rect()):
+                    if vel.x > 0:
+                        pos.x = thing.position.x - self.width
+                        vel.x = min(vel.x + thing.velocity.x, 0)
+                    elif vel.x < 0:
+                        pos.x = thing.position.x + thing.width
+                        vel.x = max(vel.x + thing.velocity.x, 0)
+        self.on_ground = False
+        pos.y += vel.y * delta
+        collision_rect = self.get_collision_rect(pos)
+        for mask in self.collision_mask:        
+            for thing in mask:
+                if thing == self:
+                    continue
+                if collision_rect.colliderect(thing.get_collision_rect()):
+                    for area in self.areas.values():
+                        if area.is_colliding(thing):
+                            area.signal(thing)
+                    if vel.y > 0:
+                        pos.y = thing.position.y - self.height
+                        vel.y = min(vel.y + thing.velocity.y, 0)
+                        self.on_ground = True
+                    elif vel.y < 0:
+                        pos.y = thing.position.y + thing.height
+                        vel.y = max(vel.y + thing.velocity.y, 0)
+        return pos, vel
+
+    def get_collision_rect(self, pos=None):
+        if pos is None:
+            pos = self.position
+        return pg.Rect(pos, (self.width, self.height))
+
+    def draw(self, surf):
+        pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=15)
+
+        # Uncomment for debugging area hitboxes
+        # for area in self.areas.values():
+        #     area.draw(surf)
diff --git a/Area.py b/Area.py
index 8cc5c45046101de39ba7bf0998316c37716207c4..23905484f76a21917d5b6c9742197c98fb65fe3e 100644 (file)
--- a/Area.py
+++ b/Area.py
@@ -1,18 +1,23 @@
 from Setup import *
 
 areas = set()
+def do_nothing(node): # placeholder func
+    pass
+
 
 class Area:
 
-    def __init__(self, pos,offset, width ,height, target_node_type):
+    def __init__(self, pos,offset, width ,height, target_node_type, func=do_nothing):
         self.position = pg.Vector2(pos)
         self.offset = pg.Vector2(offset)
         self.width = width
         self.height = height
         self.target = target_node_type
+        self.func = func
         global areas
         areas.add(self)
 
+
     def update(self, delta, pos):
         self.position = pos
 
@@ -22,6 +27,8 @@ class Area:
                 return True
         return False
 
+    def signal(self, node):
+        self.func(node)
 
     def get_collision_rect(self):
         return pg.Rect(self.position + self.offset, (self.width, self.height))
index 0bc6f71fe537a9c1c5bf89b07e4cb58f44c4e736..6121458df95b01384cc21865999bca4f16e8874f 100644 (file)
--- a/Enemy.py
+++ b/Enemy.py
+from argparse import Action
 from Setup import *
 from Player import Player
+from Actors import Actor
 
-class Enemy:
+class Enemy(Actor):
     width, height = 50, 100
     colour = (235, 64, 52)
-    speed = 0.15
-    jump_strength = 1
+    speed = 0.2
+    jump_strength = 1.1
     gravity = 0.098
     friction = 0.9
     
 
     def __init__(self, pos, collision_layer, collision_mask):
-        self.position = pg.Vector2(pos)
-        self.velocity = pg.Vector2(0, 0)
+        super().__init__(pos, collision_layer, collision_mask)
 
-        self.on_ground = False
-        
-        collision_layer.add(self)  # the layer the enemy is on for collisions
-        self.collision_mask = collision_mask  # the layer the enemy detects collisions against
-
-        self.health = 100 # each instance of enemy has it's own unique health
-
-        self.head_area = Area(self.position,pg.Vector2(0,-15), self.width, 25, Player)
+        self.areas = {"head":Area(self.position,pg.Vector2(0,-15), self.width, 25, Player, self.knockout)}
 
         self.dizzy_time = 0
 
-    def update(self, delta):
-
-        # Apply friction so the enemy isn't walking on ice
-        if self.on_ground:
-            self.velocity.x *= self.friction 
-
-        # Apply gravity
-        self.velocity.y += self.gravity
+    def update(self, delta, target=None):
+        super().update(delta)
+        if target is not None and self.dizzy_time == 0:
+            self.follow_target(target)
+        self.dizzy_time -= delta
+        self.dizzy_time = max(0,self.dizzy_time)
 
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position, self.velocity, delta)
 
-        self.head_area.update(delta, self.position)
-
-    def follow_player(self, player):
-        target = player.position
-
-        # So that enemy doesn't come up and hug u lol
-        if (self.position - target).length_squared() < pow(self.width * 2,2):
-            return
-
-
-        if target.x < self.position.x:
-            self.move_left()
-        elif target.x > self.position.x:
-            self.move_right()
-
-        if target.y < self.position.y:
-            self.jump()
-
-    def jump(self):
-        if self.on_ground:
-            self.velocity.y = -self.jump_strength
-
-    def move_left(self):
-        self.velocity.x = -self.speed
-
-    def move_right(self):
-        self.velocity.x = self.speed
-
-    def knockout(self):
-        self.dizzy_time = 1000
-
-    def move_and_collide(self, pos, vel, delta):
-        pos.x += vel.x * delta
-        collision_rect = self.get_collision_rect(pos)
-        for mask in self.collision_mask:
-            for thing in mask:
-                if thing == self:
-                    continue
-                if collision_rect.colliderect(thing.get_collision_rect()):
-                    if vel.x > 0:
-                        pos.x = thing.position.x - self.width
-                        vel.x = min(vel.x + thing.velocity.x, 0)
-                    elif vel.x < 0:
-                        pos.x = thing.position.x + thing.width
-                        vel.x = max(vel.x + thing.velocity.x, 0)
-        self.on_ground = False
-        pos.y += vel.y * delta
-        collision_rect = self.get_collision_rect(pos)
-        for mask in self.collision_mask:        
-            for thing in mask:
-                if thing == self:
-                    continue
-                if collision_rect.colliderect(thing.get_collision_rect()):
-                    if self.head_area.is_colliding(thing):
-                        self.knockout()
-                    if vel.y > 0:
-                        pos.y = thing.position.y - self.height
-                        vel.y = min(vel.y + thing.velocity.x, 0)
-                        self.on_ground = True
-                    elif vel.y < 0:
-                        pos.y = thing.position.y + thing.height
-                        vel.y = max(vel.y + thing.velocity.x, 0)
-        return pos, vel
-
-    def get_collision_rect(self, pos=None):
-        if pos is None:
-            pos = self.position
-        return pg.Rect(pos, (self.width, self.height))
+    def knockout(self, node):
+        self.dizzy_time = 10000
 
-    def draw(self, surf):
-        pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=15)
-        self.head_area.draw(surf)
diff --git a/Game.py b/Game.py
index 0b738077baf1344fead6502f12e265ec8198b44e..0cb2e3cc611d636a482f8550ea2d7572a9875844 100644 (file)
--- a/Game.py
+++ b/Game.py
@@ -8,6 +8,7 @@ class Game:
     def __init__(self):
         self.collision_layer = {0:set(),1: set(), 2:set()}
         self.player = Player(center, self.collision_layer[1], [self.collision_layer[2], self.collision_layer[0]])
+
         self.enemies = [Enemy((SCREEN_WIDTH * 3 /4, 0),self.collision_layer[2], [self.collision_layer[1], self.collision_layer[0]])]
         self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4),self.collision_layer[0]),
                     #    Block((SCREEN_WIDTH/2 - 50, SCREEN_HEIGHT * 3 / 4),self.collision_layer[0]),
@@ -17,13 +18,13 @@ class Game:
 
     def update(self, delta):
         self.player.update(delta)
+
         for enemy in self.enemies:
-            enemy.update(delta)
-            enemy.follow_player(self.player)
+            enemy.update(delta, self.player)
 
         for block in self.blocks:
             block.update(delta)
-
+        
     def draw(self, surf):
         screen.fill((255, 255, 255))
         for enemy in self.enemies:
@@ -32,3 +33,4 @@ class Game:
             block.draw(surf)
 
         self.player.draw(surf)
+
index bde8503bbceb2cf991b435c4a033ff7bb6581a62..8c3f5d292fcce9e88a37f69367fd3193171842b2 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -1,9 +1,9 @@
 import pygame.key
 
 from Setup import *
+from Actors import Actor
 
-
-class Player:
+class Player(Actor):
     width, height = 50, 100
     colour = (52, 94, 235)
     speed = 0.3
@@ -13,27 +13,16 @@ class Player:
     
 
     def __init__(self, pos, collision_layer, collision_mask):
-        self.position = pg.Vector2(pos)
-        self.velocity = pg.Vector2(0, 0)
-
-        self.on_ground = False
-
-        collision_layer.add(self)  # the layer the player is on for collisions
-        self.collision_mask = collision_mask  # the layer the player detects collisions against
+        super().__init__(pos, collision_layer, collision_mask)
+        self.areas = {"body":Area(self.position, pg.Vector2(0, self.height/2),self.width, self.height/2,Actor)}
 
-        self.health = 100 # each instance of player has it's own unique health
 
     def update(self, delta):
+        super().update(delta)
+
         # Get and handle input
         self.handle_input()
 
-        # Apply friction so the player isn't walking on ice
-        if self.on_ground:
-            self.velocity.x *= self.friction 
-
-        # Apply gravity
-        self.velocity.y += self.gravity
-
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position, self.velocity, delta)
 
@@ -46,55 +35,8 @@ class Player:
         if pressed[pg.K_d] or pressed[pg.K_RIGHT]:
             self.move_right()
 
-    def jump(self):
-
-        if self.on_ground:
-            self.velocity.y = -self.jump_strength
-
-    def move_left(self):
-        self.velocity.x = -self.speed
-
-    def move_right(self):
-        self.velocity.x = self.speed
-
-    def move_and_collide(self, pos, vel, delta):
-        pos.x += vel.x * delta
-        collision_rect = self.get_collision_rect(pos)
-        for mask in self.collision_mask:        
-            for thing in mask:
-                if thing == self:
-                    continue
-                if collision_rect.colliderect(thing.get_collision_rect()):
-                    if vel.x > 0:
-                        pos.x = thing.position.x - self.width
-                        vel.x = min(vel.x + thing.velocity.x, 0)
-                    elif vel.x < 0:
-                        pos.x = thing.position.x + thing.width
-                        vel.x = max(vel.x + thing.velocity.x, 0)
-        self.on_ground = False
-        pos.y += vel.y * delta
-        collision_rect = self.get_collision_rect(pos)
-        for mask in self.collision_mask:        
-            for thing in mask:
-                if thing == self:
-                    continue
-                if collision_rect.colliderect(thing.get_collision_rect()):
-                    if vel.y > 0:
-                        pos.y = thing.position.y - self.height
-                        vel.y = min(vel.y + thing.velocity.x, 0)
-                        self.on_ground = True
-                    elif vel.y < 0:
-                        pos.y = thing.position.y + thing.height
-                        vel.y = max(vel.y + thing.velocity.x, 0)
-        return pos, vel
-
-    def get_collision_rect(self, pos=None):
-        if pos is None:
-            pos = self.position
-        return pg.Rect(pos, (self.width, self.height))
-
     def draw(self, surf):
-        pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=15)
+        super().draw(surf)
 
     
         #Healthbar Stuff
index 47aaf59cab3597d9a8c68e1b19078d44bb383b0b..40cfdc57da342b59eee03d853e01969acaf0c000 100644 (file)
--- a/Setup.py
+++ b/Setup.py
@@ -16,4 +16,3 @@ fps = 60
 
 screen = pg.display.set_mode(dimensions, pg.SCALED)
 
-