]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
added player, block, and lots of stuff
authorSkullheadx <704277@pdsb.net>
Wed, 6 Jul 2022 02:19:53 +0000 (22:19 -0400)
committerSkullheadx <704277@pdsb.net>
Wed, 6 Jul 2022 02:19:53 +0000 (22:19 -0400)
Block.py [new file with mode: 0644]
Game.py
Player.py
main.py

diff --git a/Block.py b/Block.py
new file mode 100644 (file)
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 d22f73bddcef5680da72f77acd8908640cf0fe68..9ec04f945cd9bbd80f4d23668aaa20a915ced26f 100644 (file)
--- 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)
index 2039bd01329b0eea70e45d956c3d25e13781f8e1..6ced60b4375b616c09f5b2f097ae366b3a1e6b52 100644 (file)
--- 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 7c7128d3fb4c3a98dd4dc51d7eebaba7afa7afaf..f60e52699841109c8646b67e33085bce9b94b903 100644 (file)
--- 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()