From: Skullheadx <704277@pdsb.net> Date: Wed, 6 Jul 2022 02:19:53 +0000 (-0400) Subject: added player, block, and lots of stuff X-Git-Url: http://git.skullheadx.com/nixos/static/git-logo.png?a=commitdiff_plain;h=c6896d60a639f970e3c2697b567581195453e285;p=Pygame-Jam.git added player, block, and lots of stuff --- diff --git a/Block.py b/Block.py new file mode 100644 index 0000000..61b318b --- /dev/null +++ b/Block.py @@ -0,0 +1,20 @@ +from Setup import * + + +class Block: + width, height = 50, 50 + colour = (71, 77, 97) + + def __init__(self, pos, collision_layer): + self.position = pg.Vector2(pos) + self.velocity = pg.Vector2(0,0) # So that we may have moving blocks + collision_layer.add(self) + + def update(self, delta): + pass # when player "moves", it's actually the blocks + + 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) diff --git a/Game.py b/Game.py index d22f73b..9ec04f9 100644 --- a/Game.py +++ b/Game.py @@ -1,13 +1,22 @@ from Setup import * from Player import Player +from Block import Block class Game: def __init__(self): - self.player = Player(center) + 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])] def update(self, delta): self.player.update(delta) + for block in self.blocks: + block.update(delta) def draw(self, surf): + screen.fill((255, 255, 255)) + for block in self.blocks: + block.draw(surf) + self.player.draw(surf) diff --git a/Player.py b/Player.py index 2039bd0..6ced60b 100644 --- a/Player.py +++ b/Player.py @@ -1,15 +1,83 @@ +import pygame.key + from Setup import * + class Player: - def __init__(self, pos): + width, height = 50, 100 + colour = (52, 94, 235) + speed = 0.1 + jump_strength = 1 + gravity = 0.098 + + def __init__(self, pos, collision_layer, collision_mask): self.position = pg.Vector2(pos) - self.velocity = pg.Vector2(1,0) - + self.velocity = pg.Vector2(0, 0) + + self.on_ground = False + + collision_layer.add(self) # the layer the player is on for collisions + self.collision_mask = collision_mask # the layer the player detects collisions against + def update(self, delta): - self.position += self.velocity + # Get and handle input + self.handle_input() + + # 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) + + 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 pressed[pg.K_d] or pressed[pg.K_RIGHT]: + self.move_right() + + 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 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) + 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) + return pos, vel - # def move_and_collide(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.circle(surf, (255,0,0), self.position, 10) - \ No newline at end of file + pg.draw.rect(surf, self.colour, self.get_collision_rect(), border_radius=15) diff --git a/main.py b/main.py index 7c7128d..f60e526 100644 --- a/main.py +++ b/main.py @@ -6,9 +6,7 @@ scene = Game() delta = 1000//fps is_running = True while is_running: - screen.fill((255,255,255)) - - if pg.event.peek(pg.QUIT, pump=True): + if pg.event.peek(pg.QUIT): is_running = False scene.update(delta) @@ -17,4 +15,4 @@ while is_running: pg.display.update() delta = clock.tick(fps) -pg.quit() \ No newline at end of file +pg.quit()