From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Fri, 8 Jul 2022 04:00:17 +0000 (-0400) Subject: add combat for player X-Git-Url: http://git.skullheadx.com/index.css?a=commitdiff_plain;h=cafe10e971a76eb6caaf54c9962ea1f125838962;p=Pygame-Jam.git add combat for player its very fun so pull now. --- diff --git a/Actors.py b/Actors.py index b1cb713..28d82f9 100644 --- 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) diff --git a/Enemy.py b/Enemy.py index 39d422b..cb3059c 100644 --- 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 0363447..76a7432 100644 --- 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 diff --git a/PhysicsBody.py b/PhysicsBody.py index e827a1f..e3b14f9 100644 --- a/PhysicsBody.py +++ b/PhysicsBody.py @@ -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) + diff --git a/Player.py b/Player.py index 2f1fc41..2925c9c 100644 --- 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) diff --git a/Potion.py b/Potion.py index ae65869..3bd81ad 100644 --- 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) diff --git a/Setup.py b/Setup.py index 98afeed..348d126 100644 --- 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 diff --git a/Weapon.py b/Weapon.py index 823e239..b598eb4 100644 --- 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 e692744..75a122b 100644 --- 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