From: Skullheadx <704277@pdsb.net> Date: Thu, 7 Jul 2022 17:34:06 +0000 (-0400) Subject: halfway X-Git-Url: http://git.skullheadx.com/pfp.webp?a=commitdiff_plain;h=21321e5ef74d4305f30a1aa744f169dbaa9641fc;p=Pygame-Jam.git halfway --- diff --git a/.gitignore b/.gitignore index 0e40883..3f26c87 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ ignore_* /ignore -*/__pycache__ \ No newline at end of file +*/__pycache__ +.idea/Pygame-Jam-main.iml diff --git a/.idea/Pygame-Jam-main.iml b/.idea/Pygame-Jam-main.iml index d0876a7..1739b24 100644 --- a/.idea/Pygame-Jam-main.iml +++ b/.idea/Pygame-Jam-main.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/Actors.py b/Actors.py index deda98e..bf4edb7 100644 --- a/Actors.py +++ b/Actors.py @@ -92,10 +92,10 @@ class Actor: thing.velocity.x = vel.x if vel.x > 0: pos.x = thing.position.x - self.width - vel.x = min(vel.x + thing.velocity.x, 0) + vel.x = min(vel.x, 0) elif vel.x < 0: pos.x = thing.position.x + thing.width - vel.x = max(vel.x + thing.velocity.x, 0) + vel.x = max(vel.x, 0) collision_rect = self.get_collision_rect(pos) self.on_ground = False @@ -110,15 +110,15 @@ class Actor: area.signal(thing) if collision_rect.colliderect(thing.get_collision_rect()): if thing.movable: - thing.velocity.y += vel.y + thing.velocity.y = vel.y if vel.y > 0: pos.y = thing.position.y - self.height - vel.y = min(vel.y + thing.velocity.y, 0) + vel.y = min(vel.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) + vel.y = max(vel.y, 0) collision_rect = self.get_collision_rect(pos) return pos, vel @@ -129,7 +129,7 @@ class Actor: return pg.Rect(pos, (self.width, self.height)) def draw(self, surf): - pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=8) + pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=8) # pg.draw.rect(surf, (0,0,0), self.get_collision_rect(), border_radius=8, width=2) # Uncomment for debugging area hitboxes diff --git a/Area.py b/Area.py index 8babd5b..9a83242 100644 --- a/Area.py +++ b/Area.py @@ -37,4 +37,4 @@ class Area: 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()) + pg.draw.rect(surf, (255,0,0),get_display_rect(self.get_collision_rect())) diff --git a/Block.py b/Block.py index a5dd3a2..6e77b19 100644 --- a/Block.py +++ b/Block.py @@ -17,6 +17,5 @@ class Block: def get_collision_rect(self): return pg.Rect(self.position, (self.width, self.height)) - def draw(self, surf): - pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=5) + pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=5) diff --git a/Enemy.py b/Enemy.py index 1290015..1b253da 100644 --- a/Enemy.py +++ b/Enemy.py @@ -38,7 +38,7 @@ class Enemy(Actor): def knockout(self, node): self.dizzy_time = 2500 - self.health -= 10 + self.modify_health(-50, None) node.on_ground = True node.jump() # self.crouch(1000) diff --git a/Game.py b/Game.py index 343be76..6c80399 100644 --- a/Game.py +++ b/Game.py @@ -1,4 +1,6 @@ +import Setup from Setup import * +from Setup import camera_offset from Player import Player from Pet import Pet from Enemy import Enemy @@ -11,17 +13,15 @@ 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.pet = Pet(center, self.collision_layer["pet"], [self.collision_layer["world"]]) + # self.pet = Pet(center, self.collision_layer["pet"], [self.collision_layer["world"]]) self.enemies = [Enemy((SCREEN_WIDTH * 3 /4, 0),self.collision_layer["enemy"], [self.collision_layer["player"], self.collision_layer["world"]])] - self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4),self.collision_layer["world"])] - # 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]), + self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4),self.collision_layer["world"]), + Block((SCREEN_WIDTH, SCREEN_HEIGHT * 3 / 4 - 25),self.collision_layer["world"])] def update(self, delta): - for i,enemy in enumerate(self.enemies): enemy.update(delta, self.player) if enemy.dead: @@ -32,8 +32,9 @@ class Game: for block in self.blocks: block.update(delta) - self.player.update(delta) - self.pet.update(delta, self.player) + Setup.camera_offset += self.player.update(delta) + + # self.pet.update(delta, self.player, self.camera_pos) def draw(self, surf): screen.fill((255, 255, 255)) diff --git a/PhysicsBody.py b/PhysicsBody.py index 2a73e13..6dbb269 100644 --- a/PhysicsBody.py +++ b/PhysicsBody.py @@ -45,10 +45,10 @@ class PhysicsBody: thing.velocity.x = vel.x if vel.x > 0: pos.x = thing.position.x - self.width - vel.x = min(vel.x + thing.velocity.x, 0) + vel.x = min(vel.x, 0) elif vel.x < 0: pos.x = thing.position.x + thing.width - vel.x = max(vel.x + thing.velocity.x, 0) + vel.x = max(vel.x, 0) collision_rect = self.get_collision_rect(pos) self.on_ground = False pos.y += vel.y * delta @@ -59,15 +59,15 @@ class PhysicsBody: continue if collision_rect.colliderect(thing.get_collision_rect()): if thing.movable: - thing.velocity.y += vel.y + thing.velocity.y = vel.y if vel.y > 0: pos.y = thing.position.y - self.height - vel.y = min(vel.y + thing.velocity.y, 0) + vel.y = min(vel.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) + vel.y = max(vel.y, 0) collision_rect = self.get_collision_rect(pos) return pos, vel @@ -78,4 +78,4 @@ class PhysicsBody: def draw(self, surf): # print(self.position, self.velocity) - pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=8) + 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 c90ea96..7778500 100644 --- a/Player.py +++ b/Player.py @@ -3,6 +3,7 @@ import pygame.key from Setup import * from Actors import Actor + class Player(Actor): width, height = 25, 50 colour = (52, 94, 235) @@ -10,13 +11,11 @@ class Player(Actor): jump_strength = 0.9 gravity = 0.098 friction = 0.7 - def __init__(self, pos, collision_layer, collision_mask): 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)} - def update(self, delta): super().update(delta) @@ -24,7 +23,8 @@ class Player(Actor): self.handle_input() # Deals with collision and applying velocity - self.position, self.velocity = self.move_and_collide(self.position, self.velocity, delta) + self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity, delta) + return self.velocity * delta def handle_input(self): pressed = pygame.key.get_pressed() @@ -37,23 +37,24 @@ class Player(Actor): def draw(self, surf): super().draw(surf) - - - #Healthbar Stuff - #bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved - background_rect = pg.Rect(20, 20, 1080*0.2, 640*0.08) - - #idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7 - foreground_rect = pg.Rect(0, 0, 1080*0.185*(self.health*0.01), 640*0.06) - #make sure the red part health bar always sits on the left - #sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left - foreground_rect.center = (background_rect.centerx - 1080*0.185*((1 - self.health*0.01)/2), background_rect.centery) + # pg.draw.rect(surf,self.colour,pg.Rect(center, (self.width, self.height)),border_radius=8) + + # Healthbar Stuff + # bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved + background_rect = pg.Rect(20, 20, 1080 * 0.2, 640 * 0.08) + + # idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7 + foreground_rect = pg.Rect(0, 0, 1080 * 0.185 * (self.health * 0.01), 640 * 0.06) + # make sure the red part health bar always sits on the left + # sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left + foreground_rect.center = ( + background_rect.centerx - 1080 * 0.185 * ((1 - self.health * 0.01) / 2), background_rect.centery) pg.draw.rect(surf, (54, 54, 54), background_rect) pg.draw.rect(surf, (255, 0, 0), foreground_rect) - - #text - font = pg.font.Font("Font/Exo2-Regular.ttf" , 30) + + # text + font = pg.font.Font("Font/Exo2-Regular.ttf", 30) current_health = str(self.health) + "/100" current_health_display = font.render(current_health, True, (255, 255, 255)) - text_rect = current_health_display.get_rect(center = background_rect.center) + text_rect = current_health_display.get_rect(center=background_rect.center) surf.blit(current_health_display, text_rect) diff --git a/Setup.py b/Setup.py index 6292488..c3dfdcf 100644 --- a/Setup.py +++ b/Setup.py @@ -26,3 +26,10 @@ def rotate(pos, img, angle, pivot): rot_img = pg.transform.rotozoom(img, angle, 1) rot_rect = rot_img.get_rect(center=vec) return rot_img, rot_rect + +camera_offset = pg.Vector2(0,0) + +def get_display_rect(collision_rect): + pos = collision_rect.topleft + width, height = collision_rect.w, collision_rect.h + return pg.Rect(pos - camera_offset, (width, height)) diff --git a/Weapon.py b/Weapon.py index f8793e0..3d424e3 100644 --- a/Weapon.py +++ b/Weapon.py @@ -48,6 +48,6 @@ class Melee: self.swing_timer = 360 def draw(self, surf): - surf.blit(self.display, self.get_collision_rect().topleft) + surf.blit(self.display, get_display_rect(self.get_collision_rect()).topleft) # pygame.draw.circle(surf,(255,0,255),self.pivot,3) # pygame.draw.circle(surf,(0,255,0),self.position,3)