]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
add combat for player
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 8 Jul 2022 04:00:17 +0000 (00:00 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 8 Jul 2022 04:00:17 +0000 (00:00 -0400)
its very fun so pull now.

Actors.py
Enemy.py
Game.py
PhysicsBody.py
Player.py
Potion.py
Setup.py
Weapon.py
main.py

index b1cb713f0e2a5c4510a65fb6287bfe686b645d82..28d82f98a7ecf204f6f4f1753025202838d510d7 100644 (file)
--- a/Actors.py
+++ b/Actors.py
@@ -55,7 +55,9 @@ class Actor:
         if amount < 0:
             self.stun_time = -amount * 50
         self.is_dead(reason)
-
+    def attack(self, enemy, weapon):
+        self.modify_health(-10,"enemy")
+        self.push(enemy)
     def follow_target(self, node, follow_range=None, stop_dist=None):
         if stop_dist is None:
             stop_dist = max(self.height, self.width) * 1.5
@@ -88,11 +90,11 @@ class Actor:
             self.velocity.x = customSpeed
 
     def push(self, enemy):
-        v = pg.Vector2(0,0)
-        if enemy.velocity.x != v:
-            v = enemy.velocity.normalize().x
+        v = enemy.weapon.direction
+        # if enemy.velocity.x != pg.Vector2(0,0):
+            v = enemy.velocity.normalize().x
 
-        self.velocity += pg.Vector2(v, -1)
+        self.velocity += pg.Vector2(0.5 * v, -1)
 
     def move_and_collide(self, pos, vel, delta):
         pos.x += vel.x * delta
@@ -126,7 +128,6 @@ class Actor:
                     if area.is_colliding(thing):
                         area.signal(thing)
                 if collision_rect.colliderect(thing.get_collision_rect()):
-                    # print(thing, pos, vel, thing.position, thing.velocity)
                     if vel.y > 0:
                         pos.y = thing.position.y - self.height
                         vel.y = min(vel.y, 0)
index 39d422bc3751d512f58a0bd7fe11472c92d02e5f..cb3059c773d7d923244a73a8036c1f284aa97ff8 100644 (file)
--- a/Enemy.py
+++ b/Enemy.py
@@ -16,17 +16,19 @@ class Enemy(Actor):
     def __init__(self, pos, collision_layer, collision_mask):
         super().__init__(pos, collision_layer, collision_mask)
 
-        self.areas = {"head":Area(self.position,pg.Vector2(self.width * 1/3 * 1/2,-5), self.width * 2/3, 25, Player, self.knockout)}
+        self.areas = {"head":Area(self.position,pg.Vector2(self.width * 1/3 * 1/2,-2), self.width * 2/3, 25, Player, self.knockout)}
         self.movable = True
         self.dizzy_time = 0
 
+        # self.health = 0
+
         self.weapon = Melee(self.position, (-Melee.width/2, Melee.height/2), (0, Melee.height), self.width,-1)
 
     def update(self, delta, target=None):
         super().update(delta)
         if target is not None and self.dizzy_time == 0:
             self.follow_target(target,stop_dist=self.weapon.width * 0.8 + self.width + target.width)
-            if not self.weapon.attacking and self.weapon.get_collision_rect().colliderect(target.get_collision_rect()):
+            if random.random() < 2.25/fps and not self.weapon.attacking and self.weapon.get_collision_rect().colliderect(target.get_collision_rect()):
                 self.weapon.swing()
                 target.attack(self, self.weapon)
         self.dizzy_time -= delta
@@ -36,11 +38,15 @@ class Enemy(Actor):
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
 
-        self.weapon.update(delta,self.position, math.copysign(1,self.velocity.x))
+        if self.velocity.x == 0:
+            direction = 0
+        else:
+            direction = math.copysign(1,self.velocity.x)
+        self.weapon.update(delta,self.position, direction)
 
     def knockout(self, node):
-        self.dizzy_time = 2500
-        self.modify_health(-50, None)
+        self.dizzy_time = 100
+        self.modify_health(-25, None)
         node.on_ground = True
         node.jump()
         # self.crouch(1000)
diff --git a/Game.py b/Game.py
index 0363447321e5f1dcde3d30da2b654fbb85fb9335..76a74323aaa50b6503b6891120f30ad6469f4e2c 100644 (file)
--- a/Game.py
+++ b/Game.py
@@ -15,12 +15,13 @@ class Game:
         self.collision_layer = {"world": set(), "player": set(), "enemy": set(), "pet": set()}
 
         self.player = Player(center, self.collision_layer["player"],
-                             [self.collision_layer["enemy"], self.collision_layer["world"]])
+                             [self.collision_layer["enemy"], self.collision_layer["world"]],
+                             [self.collision_layer["enemy"]])
         # self.pet = Pet(center, self.collision_layer["pet"], [self.collision_layer["world"]])
 
         self.enemies = [Enemy((SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2), self.collision_layer["enemy"],
                               [self.collision_layer["player"], self.collision_layer["world"]])]
-        self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4), self.collision_layer["world"])]
+        self.blocks = [Block((-0, SCREEN_HEIGHT * 3 / 4), self.collision_layer["world"])]
 
         self.scene = EndScreen()
         self.level = 1
index e827a1fb9e98d597b0d29c07c127d55c1183d52c..e3b14f93ba4108eebe0780bbf9e77f081ec61638 100644 (file)
@@ -1,5 +1,6 @@
 from Setup import *
 from Block import Block
+from Function.createText import createText
 
 class PhysicsBody:
     speed = 0.2
@@ -10,7 +11,7 @@ class PhysicsBody:
     def __init__(self, pos, vel, width, height, colour, collision_layer, collision_mask):
         self.position = pg.Vector2(pos)
         self.velocity = pg.Vector2(vel)
-        self.width, self.height = width, height
+        self.width, self.height = height, width
         self.colour = colour
 
         self.on_ground = False
@@ -33,7 +34,14 @@ class PhysicsBody:
 
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
         # print(self.position)
+    def attack(self, enemy, weapon):
+        self.push(enemy)
+    def push(self, enemy):
+        v = enemy.weapon.direction
+        # if enemy.velocity.x != pg.Vector2(0,0):
+        #     v = enemy.velocity.normalize().x
 
+        self.velocity += pg.Vector2(0.5 * v, -1)
     def move_and_collide(self, pos, vel, delta):
         pos.x += vel.x * delta
         collision_rect = self.get_collision_rect(pos)
@@ -80,3 +88,4 @@ class PhysicsBody:
     def draw(self, surf):
         # print(self.position, self.velocity)
         pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=8)
+
index 2f1fc418125feaa4574f85234b5a790d0784b27d..2925c9cb21a483096211017c64651f83f426ac29 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -7,6 +7,7 @@ from Setup import *
 from Actors import Actor
 from datetime import datetime, timedelta
 from Potion import Potion
+from Weapon import Melee
 
 class Player(Actor):
     width, height = 25, 50
@@ -16,7 +17,7 @@ class Player(Actor):
     gravity = 0.098
     friction = 0.7
 
-    def __init__(self, pos, collision_layer, collision_mask):
+    def __init__(self, pos, collision_layer, collision_mask, can_hurt):
         super().__init__(pos, collision_layer, collision_mask)
         self.initial_position = pg.Vector2(pos)
         self.dashCooldown = timedelta(seconds=2, microseconds=500000)
@@ -37,6 +38,9 @@ class Player(Actor):
         for i in range(self.starting_potions):
             self.potion_bag.append(Potion(self))
 
+        self.weapon = Melee(self.position, (-Melee.width/2, Melee.height/2), (0, Melee.height), self.width,-1)
+        self.targets = can_hurt
+
     def update(self, delta):
         super().update(delta)
 
@@ -51,6 +55,13 @@ class Player(Actor):
 
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
+
+        if self.velocity.x == 0:
+            direction = 0
+        else:
+            direction = math.copysign(1,self.velocity.x)
+        self.weapon.update(delta,self.position, direction)
+
         return self.position - self.initial_position
 
         
@@ -88,11 +99,19 @@ class Player(Actor):
         self.lastValueL = pressed[pg.K_a] or pressed[pg.K_LEFT]
         self.lastValueR = pressed[pg.K_d] or pressed[pg.K_RIGHT]
 
-    def attack(self, enemy, weapon):
-        self.modify_health(-10,"enemy")
-        self.push(enemy)
+        mouse_pressed = pg.mouse.get_pressed(3)
+        if mouse_pressed[0]: # LMB
+            if not self.weapon.attacking:
+                self.weapon.swing()
+                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)
         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 ae658697c541f9e7aae834ac9ef5a54a4c7379b5..3bd81ad0026ade693dccd3c6dbaddcdf3cd83715 100644 (file)
--- a/Potion.py
+++ b/Potion.py
@@ -7,7 +7,7 @@ class Potion:
         self.player = player
 
     def get_input(self, player):
-        pressed_key = pygame.key.get_pressed()
+        pressed_key = pg.key.get_pressed()
         if pressed_key[pg.K_1] and player.potion_cooldown == 0:
             self.consume_potion(player)
     
index 98afeeda4968c4a25fed1b551484acc2fe9f38f4..348d12653e00297a30d65329b18bc1540e98ecff 100644 (file)
--- a/Setup.py
+++ b/Setup.py
@@ -1,9 +1,7 @@
 import pygame as pg
 import math
+import random
 
-import pygame.transform
-
-from Area import Area
 
 pg.init()
 
@@ -34,3 +32,5 @@ def get_display_rect(collision_rect):
     width, height = collision_rect.w, collision_rect.h
     return pg.Rect(pos - camera_offset, (width, height))
     # return pg.Rect(pos, (width, height))
+
+from Area import Area
index 823e239fdb7d3112d00e91e60eeaf4b2704fd658..b598eb4a18194f37f134d6b32d282df675a3f091 100644 (file)
--- a/Weapon.py
+++ b/Weapon.py
@@ -25,7 +25,8 @@ class Melee:
     def update(self, delta, pos, direction):
         self.position = pg.Vector2(pos)
         self.pivot = self.position + self.offset + pg.Vector2(self.width/2, self.height/2)
-        self.direction = direction
+        if direction != 0:
+            self.direction = direction
 
         if self.direction == -1:
             angle = 25 * (math.sin(math.radians(self.swing_timer)))
diff --git a/main.py b/main.py
index e6927446db6e88cd995302ff3f8bdaabe7aba38d..75a122b68426d31badbc39dca898c4ef8d477dbd 100644 (file)
--- a/main.py
+++ b/main.py
@@ -6,8 +6,8 @@ from MainMenu import Menu
 delta = 1000//fps
 is_running = True
 
-scene = Menu()
-scene = Game()
+scene = Menu()
+scene = Game()
 old_level = 0
 level = 1