From 7efa1f2f7c76cb56e4be1da8b6b266ff7c2c482a Mon Sep 17 00:00:00 2001 From: Skullheadx <704277@pdsb.net> Date: Tue, 5 Jul 2022 23:41:10 -0400 Subject: [PATCH] Added tons of stuff Enemies, Area, Blocks, Player and health stuff. Basically made everything we are gonna need later. --- .gitignore | 6 ++ Area.py | 30 ++++++++ Block.py | 2 +- Enemy.py | 111 ++++++++++++++++++++++++++++++ Game.py | 18 ++++- Player.py | 69 ++++++++++++++----- Setup.py | 5 +- __pycache__/Game.cpython-39.pyc | Bin 1050 -> 1262 bytes __pycache__/Player.cpython-39.pyc | Bin 2698 -> 3249 bytes __pycache__/Setup.cpython-39.pyc | Bin 402 -> 429 bytes 10 files changed, 217 insertions(+), 24 deletions(-) create mode 100644 Area.py create mode 100644 Enemy.py diff --git a/.gitignore b/.gitignore index 86ff0f3..c2e0923 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,9 @@ __pycache__/Block.cpython-39.pyc .idea/Pygame-Jam.iml __pycache__/Game.cpython-39.pyc __pycache__/Player.cpython-39.pyc +__pycache__/Area.cpython-39.pyc +__pycache__/Enemy.cpython-39.pyc +__pycache__/Game.cpython-39.pyc +__pycache__/Player.cpython-39.pyc +__pycache__/Setup.cpython-39.pyc +*.pyc diff --git a/Area.py b/Area.py new file mode 100644 index 0000000..8cc5c45 --- /dev/null +++ b/Area.py @@ -0,0 +1,30 @@ +from Setup import * + +areas = set() + +class Area: + + def __init__(self, pos,offset, width ,height, target_node_type): + self.position = pg.Vector2(pos) + self.offset = pg.Vector2(offset) + self.width = width + self.height = height + self.target = target_node_type + global areas + areas.add(self) + + def update(self, delta, pos): + self.position = pos + + def is_colliding(self,node): + if isinstance(node, self.target): + if self.get_collision_rect().colliderect(node.get_collision_rect()): + return True + return False + + + def get_collision_rect(self): + return pg.Rect(self.position + self.offset, (self.width, self.height)) + + def draw(self, surf): + pg.draw.rect(surf, (255,0,0),self.get_collision_rect()) \ No newline at end of file diff --git a/Block.py b/Block.py index 61b318b..6158ddf 100644 --- a/Block.py +++ b/Block.py @@ -2,7 +2,7 @@ from Setup import * class Block: - width, height = 50, 50 + width, height = SCREEN_WIDTH, 50 colour = (71, 77, 97) def __init__(self, pos, collision_layer): diff --git a/Enemy.py b/Enemy.py new file mode 100644 index 0000000..0bc6f71 --- /dev/null +++ b/Enemy.py @@ -0,0 +1,111 @@ +from Setup import * +from Player import Player + +class Enemy: + width, height = 50, 100 + colour = (235, 64, 52) + speed = 0.15 + 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 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.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 + + # 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 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 9ec04f9..0b73807 100644 --- a/Game.py +++ b/Game.py @@ -1,21 +1,33 @@ from Setup import * from Player import Player +from Enemy import Enemy from Block import Block class Game: def __init__(self): - self.collision_layer = {0:set(),1: set()} - self.player = Player(center, self.collision_layer[1], self.collision_layer[0]) - self.blocks = [Block((SCREEN_WIDTH/2, SCREEN_HEIGHT * 3 / 4),self.collision_layer[0])] + 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]), + # Block((SCREEN_WIDTH/2 + 50, SCREEN_HEIGHT * 3 / 4),self.collision_layer[0]), + + ] def update(self, delta): self.player.update(delta) + for enemy in self.enemies: + enemy.update(delta) + enemy.follow_player(self.player) + for block in self.blocks: block.update(delta) def draw(self, surf): screen.fill((255, 255, 255)) + for enemy in self.enemies: + enemy.draw(surf) for block in self.blocks: block.draw(surf) diff --git a/Player.py b/Player.py index 6ced60b..bde8503 100644 --- a/Player.py +++ b/Player.py @@ -6,9 +6,11 @@ from Setup import * class Player: width, height = 50, 100 colour = (52, 94, 235) - speed = 0.1 - jump_strength = 1 + speed = 0.3 + jump_strength = 1.5 gravity = 0.098 + friction = 0.9 + def __init__(self, pos, collision_layer, collision_mask): self.position = pg.Vector2(pos) @@ -19,10 +21,16 @@ class Player: collision_layer.add(self) # the layer the player is on for collisions self.collision_mask = collision_mask # the layer the player detects collisions against + self.health = 100 # each instance of player has it's own unique health + def update(self, 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 @@ -52,26 +60,32 @@ class Player: def move_and_collide(self, pos, vel, delta): pos.x += vel.x * delta collision_rect = self.get_collision_rect(pos) - for thing in self.collision_mask: - 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) + 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 thing in self.collision_mask: - 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) + 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): @@ -81,3 +95,20 @@ class Player: def draw(self, surf): pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=15) + + + #Healthbar Stuff + # self.position is the top left + + background_rect = pg.Rect(0, 0, 1080*0.2, 640*0.08) + background_rect.center = (1080*0.5, 50) + foreground_rect = pg.Rect(0, 0, 1080*0.19, 640*0.07) + foreground_rect.center = background_rect.center + pg.draw.rect(surf, (54, 54, 54), background_rect) + pg.draw.rect(surf, (255, 0, 0), foreground_rect) + + font = pg.font.Font(None , 20) + current_health = str(self.health) + "/100" + current_health_display = font.render(current_health, True, (255, 255, 255)) + surf.blit(current_health_display, foreground_rect) + diff --git a/Setup.py b/Setup.py index 6e49b54..47aaf59 100644 --- a/Setup.py +++ b/Setup.py @@ -1,4 +1,5 @@ import pygame as pg +from Area import Area pg.init() @@ -13,4 +14,6 @@ pg.display.set_caption("Jam") clock = pg.time.Clock() fps = 60 -screen = pg.display.set_mode(dimensions, pg.SCALED) \ No newline at end of file +screen = pg.display.set_mode(dimensions, pg.SCALED) + + diff --git a/__pycache__/Game.cpython-39.pyc b/__pycache__/Game.cpython-39.pyc index a1c42609e104821cc4628ffc035655c50be2054c..0a14f572d80b9b8fa2014e9056b33940a2325eea 100644 GIT binary patch delta 829 zcmbQm@s5)(k(ZZ?fq{YH7vs^SM=TThWEkxxYAf5OFr={LFy}HyF*7o_Go-Mlu(dFx zur)J9v81pEGiY+W1nJRayv3-c$#{z`ASbahwMdik7OQJsYHsDkvXpu@1_lOa1_p-W zJ_ZJc5{70tPGOwGT*DC0Si)4oT*HvXn8GB<0H&FtG>ardI#V-94ND1Y30n%Q2tx{+ zG(!qY8WV~-m_DfbbjBKnc=i&G6qXXs6jpDDCa4w`Nrq-7umY|WhF}Iwc0Wz#B1Q%V zhL>Q1iGhKkh#f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