From: Skullheadx <704277@pdsb.net> Date: Fri, 8 Jul 2022 20:51:04 +0000 (-0400) Subject: added the world blocks & textures X-Git-Url: http://git.skullheadx.com/banner.webp?a=commitdiff_plain;h=ba0ba1650e7bbdaf2f7e800c299101836942e7bf;p=Pygame-Jam.git added the world blocks & textures --- diff --git a/Actors.py b/Actors.py index 6913cba..8468580 100644 --- a/Actors.py +++ b/Actors.py @@ -53,10 +53,10 @@ class Actor: def modify_health(self, amount, reason): self.health += amount if amount < 0: - self.stun_time = -amount * 50 + self.stun_time = -amount * 10 self.is_dead(reason) def attack(self, enemy, weapon): - self.modify_health(-10,"enemy") + self.modify_health(weapon.damage,"enemy") self.push(enemy) def follow_target(self, node, follow_range=None, stop_dist=None): if stop_dist is None: @@ -94,10 +94,10 @@ class Actor: # if enemy.velocity.x != pg.Vector2(0,0): # v = enemy.velocity.normalize().x - self.velocity += pg.Vector2(0.75 * v, -1) + self.velocity += pg.Vector2(v, -1) def push2(self, direction): - self.velocity += pg.Vector2(0.75 * direction, -1) + self.velocity += pg.Vector2(direction, -1) def move_and_collide(self, pos, vel, delta): @@ -115,10 +115,10 @@ class Actor: thing.position.x = pos.x - thing.width if vel.x > 0: pos.x = thing.position.x - self.width - vel.x = min(vel.x, 0) + # vel.x = min(vel.x, 0) elif vel.x < 0: pos.x = thing.position.x + thing.width - vel.x = max(vel.x, 0) + # vel.x = max(vel.x, 0) collision_rect = self.get_collision_rect(pos) self.on_ground = False diff --git a/Assets/ARROW.png b/Assets/ARROW.png new file mode 100644 index 0000000..d79c4e0 Binary files /dev/null and b/Assets/ARROW.png differ diff --git a/Assets/BOW.png b/Assets/BOW.png new file mode 100644 index 0000000..74435e7 Binary files /dev/null and b/Assets/BOW.png differ diff --git a/Assets/BOW_PULLED.png b/Assets/BOW_PULLED.png new file mode 100644 index 0000000..0bfe625 Binary files /dev/null and b/Assets/BOW_PULLED.png differ diff --git a/Assets/FLINTLOCK_PISTOL.png b/Assets/FLINTLOCK_PISTOL.png new file mode 100644 index 0000000..4f6dafa Binary files /dev/null and b/Assets/FLINTLOCK_PISTOL.png differ diff --git a/Assets/GrassBlock.png b/Assets/GrassBlock.png deleted file mode 100644 index b4a37fc..0000000 Binary files a/Assets/GrassBlock.png and /dev/null differ diff --git a/Assets/world/blocks/DIRT.png b/Assets/world/blocks/DIRT.png new file mode 100644 index 0000000..4998792 Binary files /dev/null and b/Assets/world/blocks/DIRT.png differ diff --git a/Assets/world/blocks/GRASS.png b/Assets/world/blocks/GRASS.png new file mode 100644 index 0000000..4ec4647 Binary files /dev/null and b/Assets/world/blocks/GRASS.png differ diff --git a/Assets/LEAFS1.png b/Assets/world/blocks/LEAF.png similarity index 100% rename from Assets/LEAFS1.png rename to Assets/world/blocks/LEAF.png diff --git a/Assets/LEAFS.png b/Assets/world/blocks/LEAFS.png similarity index 100% rename from Assets/LEAFS.png rename to Assets/world/blocks/LEAFS.png diff --git a/Assets/world/blocks/PLACEHOLDER.png b/Assets/world/blocks/PLACEHOLDER.png new file mode 100644 index 0000000..a5379e2 Binary files /dev/null and b/Assets/world/blocks/PLACEHOLDER.png differ diff --git a/Assets/world/blocks/SAND.png b/Assets/world/blocks/SAND.png new file mode 100644 index 0000000..22da885 Binary files /dev/null and b/Assets/world/blocks/SAND.png differ diff --git a/Assets/StoneBlock.png b/Assets/world/blocks/STONE.png similarity index 100% rename from Assets/StoneBlock.png rename to Assets/world/blocks/STONE.png diff --git a/Assets/TREEBARK.png b/Assets/world/blocks/TREEBARK.png similarity index 100% rename from Assets/TREEBARK.png rename to Assets/world/blocks/TREEBARK.png diff --git a/Assets/Water.png b/Assets/world/blocks/WATER.png similarity index 100% rename from Assets/Water.png rename to Assets/world/blocks/WATER.png diff --git a/Assets/world/decor/BLUE_CRYSTAL.png b/Assets/world/decor/BLUE_CRYSTAL.png new file mode 100644 index 0000000..3911548 Binary files /dev/null and b/Assets/world/decor/BLUE_CRYSTAL.png differ diff --git a/Assets/world/decor/PURPLE_CRYSTAL.png b/Assets/world/decor/PURPLE_CRYSTAL.png new file mode 100644 index 0000000..cb927a1 Binary files /dev/null and b/Assets/world/decor/PURPLE_CRYSTAL.png differ diff --git a/Block.py b/Block.py index fcf4a4e..89bcf03 100644 --- a/Block.py +++ b/Block.py @@ -2,20 +2,27 @@ from Setup import * class Block: - width, height = SCREEN_WIDTH*4/5, 50 + textures = {file[:file.index(".")]:pg.transform.scale( + pg.image.load(path.join("Assets/world/blocks", file)), (50, 50)) for + file in listdir("Assets/world/blocks")} + width, height = textures["PLACEHOLDER"].get_size() colour = (71, 77, 97) - def __init__(self, pos, collision_layer): + def __init__(self, pos, collision_layer, texture="PLACEHOLDER"): self.position = pg.Vector2(pos) self.velocity = pg.Vector2(0,0) # So that we may have moving blocks collision_layer.add(self) + self.texture = self.textures[texture] + self.movable = False def update(self, delta): - pass # when player "moves", it's actually the blocks + pass def get_collision_rect(self): return pg.Rect(self.position, (self.width, self.height)) + def draw(self, surf): - pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=5) + pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=3) + surf.blit(self.texture, get_display_rect(self.get_collision_rect())) diff --git a/Enemy.py b/Enemy.py index 0a14e33..6564b2c 100644 --- a/Enemy.py +++ b/Enemy.py @@ -20,9 +20,9 @@ class Enemy(Actor): self.movable = True self.dizzy_time = 0 - # self.health = 0 + self.health = 0 # for debugging without getting killed - self.weapon = Melee(self.position, (-Melee.width/2 + 7, Melee.height/2 + self.height/3 - 8), (-5,Melee.height), self.width,-1) + self.weapon = Melee(self.position, (-Melee.width/2 + 7, Melee.height/2 + self.height/3 - 8), (-5,Melee.height), self.width,-1, -10) def update(self, delta, target=None): super().update(delta) diff --git a/Game.py b/Game.py index 5293288..69d408a 100644 --- a/Game.py +++ b/Game.py @@ -6,22 +6,25 @@ from Pet import Pet from Enemy import Enemy from Block import Block from PhysicsBody import PhysicsBody +from World import World from EndScreen import EndScreen class Game: def __init__(self): - self.collision_layer = {"world": set(), "player": set(), "enemy": set(), "pet": set()} + self.collision_layer = {"none":set(),"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.pet = Pet(center, self.collision_layer["pet"], [self.collision_layer["world"]]) + self.world = World(self.collision_layer) + self.enemies = [Enemy((SCREEN_WIDTH *3/ 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.scene = EndScreen() self.level = 1 @@ -40,17 +43,16 @@ class Game: self.collision_layer["enemy"].remove(enemy) self.collision_layer["enemy"].add(self.enemies[i]) - for block in self.blocks: - block.update(delta) + self.world.update(delta) # self.pet.update(delta, self.player, self.camera_pos) def draw(self, surf): screen.fill((255, 255, 255)) + self.world.draw(surf) for enemy in self.enemies: enemy.draw(surf) - for block in self.blocks: - block.draw(surf) + self.player.draw(surf) diff --git a/Player.py b/Player.py index a75a72c..fc44fc5 100644 --- a/Player.py +++ b/Player.py @@ -9,11 +9,14 @@ from datetime import datetime, timedelta from Potion import Potion from Weapon import Melee + class Player(Actor): scale = 100 - factor = 640/scale - crop = pg.Rect(179,169, 170, 401) - idle_frames = [pygame.transform.scale(pg.image.load(path.join("Assets/player/idle", file)).subsurface(pg.Rect(179,169, 170, 401)), (50,100)) for file in listdir("Assets/player/idle")] + factor = 640 / scale + crop = pg.Rect(179, 169, 170, 401) + idle_frames = [ + pg.transform.scale(pg.image.load(path.join("Assets/player/idle", file)).subsurface(pg.Rect(179, 169, 170, 401)), + (50, 100)) for file in listdir("Assets/player/idle")] width, height = idle_frames[0].get_size() colour = (52, 94, 235) @@ -22,7 +25,6 @@ class Player(Actor): gravity = 0.098 friction = 0.7 - def __init__(self, pos, collision_layer, collision_mask, can_hurt): super().__init__(pos, collision_layer, collision_mask) self.initial_position = pg.Vector2(pos) @@ -36,7 +38,9 @@ class Player(Actor): self.lastValueL = False self.lastPressedRight = datetime.utcnow() self.lastValueR = False - # self.areas = {"body":Area(self.position, pg.Vector2(0, self.height/2),self.width, self.height/2,Actor)} + self.areas = {"body": Area(self.position, pg.Vector2(0, self.height / 2), self.width, self.height / 2, Actor), + "head": Area(self.position, pg.Vector2(self.width * 1 / 3 * 1 / 2, -2), self.width * 2 / 3, 25, + Actor)} self.potion_cooldown = 0 self.starting_potions = 999 @@ -44,7 +48,8 @@ class Player(Actor): for i in range(self.starting_potions): self.potion_bag.append(Potion(self)) - self.weapon = Melee(self.position, (-Melee.width/2 + 7, Melee.height/2 + self.height/3 - 8), (-5,Melee.height), self.width,-1) + self.weapon = Melee(self.position, (-Melee.width / 2 + 7, Melee.height / 2 + self.height / 3 - 8), + (-5, Melee.height), self.width, -1, -25) self.targets = can_hurt self.direction = -1 @@ -52,7 +57,7 @@ class Player(Actor): self.state = "IDLE" self.current_frame = 0 self.display = self.idle_frames[0] - self.display_offsets = {"weapon":pg.Vector2(0,0)} + self.display_offsets = {"weapon": pg.Vector2(0, 0)} def update(self, delta): super().update(delta) @@ -60,8 +65,8 @@ class Player(Actor): # Get and handle input self.handle_input() - #if self.potion_cooldown > 0: - #threading.Thread(Potion.cooldown) + # if self.potion_cooldown > 0: + # threading.Thread(Potion.cooldown) if len(self.potion_bag) > 0: self.potion_bag[0].get_input(self) @@ -73,8 +78,8 @@ class Player(Actor): if self.velocity.x == 0: self.direction = 0 else: - self.direction = math.copysign(1,self.velocity.x) - self.weapon.update(delta,self.position, self.direction) + self.direction = math.copysign(1, self.velocity.x) + self.weapon.update(delta, self.position, self.direction) if self.state == "IDLE": frame = math.floor(self.current_frame) @@ -90,52 +95,56 @@ class Player(Actor): # 1 - 10 up, 11 down by 1, 12 - 18 down by 2, 19-20 down 1 FIX THIS frame += 1 if 1 < frame < 10: - self.display_offsets["weapon"] = pg.Vector2(0,2) + self.display_offsets["weapon"] = pg.Vector2(0, 2) elif frame == 11 or frame == 19 or frame == 20: - self.display_offsets["weapon"] = pg.Vector2(0,1) + self.display_offsets["weapon"] = pg.Vector2(0, 1) else: - self.display_offsets["weapon"] = pg.Vector2(0,0) + self.display_offsets["weapon"] = pg.Vector2(0, 0) self.current_frame = (self.current_frame + 0.1) % len(self.idle_frames) return self.position - self.initial_position - - def handle_input(self): pressed = pygame.key.get_pressed() if pressed[pg.K_w] or pressed[pg.K_UP] or pressed[pg.K_SPACE]: self.jump() if pressed[pg.K_a] or pressed[pg.K_LEFT]: self.move_left() - if(self.lastValueL == False): + if (self.lastValueL == False): timeSincePressed = datetime.utcnow() - self.lastPressedLeft timeSinceLastDash = datetime.utcnow() - self.lastDash - if(timeSinceLastDash >= self.dashCooldown): self.dashPossible = True - else: self.dashPossible = False - if(timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True): - self.move_left(self.dashSpeed) # change this to change how the player dashes (maybe replace with a custom dash function) + if (timeSinceLastDash >= self.dashCooldown): + self.dashPossible = True + else: + self.dashPossible = False + if (timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True): + self.move_left( + self.dashSpeed) # change this to change how the player dashes (maybe replace with a custom dash function) self.dashPossible = False self.lastDash = datetime.utcnow() - self.lastPressedLeft = datetime.utcnow() - + self.lastPressedLeft = datetime.utcnow() + if pressed[pg.K_d] or pressed[pg.K_RIGHT]: self.move_right() - if(self.lastValueR == False): + if (self.lastValueR == False): timeSincePressed = datetime.utcnow() - self.lastPressedRight timeSinceLastDash = datetime.utcnow() - self.lastDash - if(timeSinceLastDash >= self.dashCooldown): self.dashPossible = True - else: self.dashPossible = False - if(timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True): + if (timeSinceLastDash >= self.dashCooldown): + self.dashPossible = True + else: + self.dashPossible = False + if (timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True): self.lastDash = datetime.utcnow() self.dashPossible = False - self.move_right(self.dashSpeed) # change this to change how the player dashes (maybe replace with a custom dash function) - self.lastPressedRight = datetime.utcnow() - + self.move_right( + self.dashSpeed) # change this to change how the player dashes (maybe replace with a custom dash function) + self.lastPressedRight = datetime.utcnow() + self.lastValueL = pressed[pg.K_a] or pressed[pg.K_LEFT] self.lastValueR = pressed[pg.K_d] or pressed[pg.K_RIGHT] mouse_pressed = pg.mouse.get_pressed(3) - if mouse_pressed[0]: # LMB + if mouse_pressed[0]: # LMB if not self.weapon.attacking: self.weapon.swing() for mask in self.targets: @@ -143,12 +152,10 @@ class Player(Actor): if self.weapon.get_collision_rect().colliderect(enemy.get_collision_rect()): enemy.attack(self, self.weapon) - - def draw(self, surf): self.weapon.draw(surf, self.display_offsets["weapon"]) - # super().draw(surf) surf.blit(self.display, get_display_rect(self.get_collision_rect())) + # 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) @@ -163,7 +170,7 @@ class Player(Actor): # 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) + 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) diff --git a/Weapon.py b/Weapon.py index 54e84a1..a36395f 100644 --- a/Weapon.py +++ b/Weapon.py @@ -4,11 +4,11 @@ from Setup import * class Melee: - img = pg.transform.scale(pg.image.load("Assets/SWORD.png"), (40,40)) + img = pg.transform.scale(pg.image.load("Assets/SWORD.png"), (40, 40)) flipped_img = pg.transform.flip(img,True,False) width,height = img.get_size() - def __init__(self, pos, offset, pivot, width,direction): + def __init__(self, pos, offset, pivot, width,direction, damage): self.position = pg.Vector2(pos) self.offset = pg.Vector2(offset) self.pivot = self.position + pg.Vector2(pivot) @@ -21,6 +21,7 @@ class Melee: self.swing_timer = 0 self.attacking = False + self.damage = damage def update(self, delta, pos, direction): self.position = pg.Vector2(pos) diff --git a/World.py b/World.py new file mode 100644 index 0000000..c7ddb92 --- /dev/null +++ b/World.py @@ -0,0 +1,61 @@ +from Setup import * +from Block import Block + +class World: + + def __init__(self, collision_layer): + self.collision_layer = collision_layer + self.blocks = [] + self.add_world() + + def add_world(self): + for i in range(25): + self.blocks.append(Block((i * Block.width,SCREEN_HEIGHT*3/4),self.collision_layer["world"],"GRASS")) + self.blocks.append(Block((i * Block.width,SCREEN_HEIGHT*3/4 + Block.height),self.collision_layer["world"],"DIRT")) + if random.random() < 0.5: + self.blocks.append( + Block((i * Block.width, SCREEN_HEIGHT * 3 / 4 + Block.height * 2), self.collision_layer["world"], + "STONE")) + else: + self.blocks.append( + Block((i * Block.width, SCREEN_HEIGHT * 3 / 4 + Block.height * 2), self.collision_layer["world"], + "DIRT")) + self.blocks.append(Block((i * Block.width,SCREEN_HEIGHT*3/4 + Block.height * 3),self.collision_layer["world"],"STONE")) + self.blocks.append(Block((i * Block.width,SCREEN_HEIGHT*3/4 + Block.height * 4),self.collision_layer["world"],"STONE")) + + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -1),self.collision_layer["none"],"TREEBARK")) + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -2),self.collision_layer["none"],"TREEBARK")) + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"TREEBARK")) + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"TREEBARK")) + + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"LEAFS")) + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"LEAFS")) + self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -5),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 10,SCREEN_HEIGHT*3/4 + Block.height * -6),self.collision_layer["none"],"LEAFS")) + + self.blocks.append(Block((Block.width * 9,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"LEAFS")) + self.blocks.append(Block((Block.width * 9,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"LEAFS")) + self.blocks.append(Block((Block.width * 9,SCREEN_HEIGHT*3/4 + Block.height * -5),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 9,SCREEN_HEIGHT*3/4 + Block.height * -6),self.collision_layer["none"],"LEAFS")) + + self.blocks.append(Block((Block.width * 11,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"LEAFS")) + self.blocks.append(Block((Block.width * 11,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"LEAF")) + self.blocks.append(Block((Block.width * 11,SCREEN_HEIGHT*3/4 + Block.height * -5),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 11,SCREEN_HEIGHT*3/4 + Block.height * -6),self.collision_layer["none"],"LEAFS")) + + + # self.blocks.append(Block((Block.width * 12,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 12,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 12,SCREEN_HEIGHT*3/4 + Block.height * -5),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 8,SCREEN_HEIGHT*3/4 + Block.height * -3),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 8,SCREEN_HEIGHT*3/4 + Block.height * -4),self.collision_layer["none"],"LEAFS")) + # self.blocks.append(Block((Block.width * 8,SCREEN_HEIGHT*3/4 + Block.height * -5),self.collision_layer["none"],"LEAFS")) + + + def update(self, delta): + for block in self.blocks: + block.update(delta) + + def draw(self, surf): + for block in self.blocks: + block.draw(surf) diff --git a/main.py b/main.py index 75a122b..7f5847f 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,6 @@ while is_running: case 1: scene = Game() old_level = level - scene.update(delta) scene.draw(screen)