thing.position.x = pos.x + self.width
elif vel.x < 0:
thing.position.x = pos.x - thing.width
- else:
- if vel.x > 0:
- pos.x = thing.position.x - self.width
- vel.x = min(vel.x, 0)
- elif vel.x < 0:
- pos.x = thing.position.x + thing.width
- vel.x = max(vel.x, 0)
+ if vel.x > 0:
+ pos.x = thing.position.x - self.width
+ vel.x = min(vel.x, 0)
+ elif vel.x < 0:
+ pos.x = thing.position.x + thing.width
+ vel.x = max(vel.x, 0)
collision_rect = self.get_collision_rect(pos)
self.on_ground = False
if area.is_colliding(thing):
area.signal(thing)
if collision_rect.colliderect(thing.get_collision_rect()):
+ # print(thing, pos, vel, thing.position, thing.velocity)
if vel.y > 0:
pos.y = thing.position.y - self.height
vel.y = min(vel.y, 0)
from PhysicsBody import PhysicsBody
from EndScreen import EndScreen
+
class Game:
def __init__(self):
- self.collision_layer = {"world":set(),"player": set(), "enemy":set(), "pet":set()}
+ 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.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.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, SCREEN_HEIGHT * 3 / 4 - 25),self.collision_layer["world"])]
+ 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
if self.player.dead:
self.level = self.scene.level
- for i,enemy in enumerate(self.enemies):
+ for i, enemy in enumerate(self.enemies):
enemy.update(delta, self.player)
if enemy.dead:
- self.enemies[i] = PhysicsBody(enemy.position,enemy.velocity,enemy.width,enemy.height,enemy.colour,enemy.collision_layer,enemy.collision_mask)
+ self.enemies[i] = PhysicsBody(enemy.position, enemy.velocity, enemy.width, enemy.height, enemy.colour,
+ enemy.collision_layer, enemy.collision_mask)
self.collision_layer["enemy"].remove(enemy)
self.collision_layer["enemy"].add(self.enemies[i])
for block in self.blocks:
block.update(delta)
-
# self.pet.update(delta, self.player, self.camera_pos)
def draw(self, surf):
if self.player.dead:
self.scene.update()
self.scene.draw()
+
+ # Debug Lines. DO NOT CROSS THEM!
+ pg.draw.line(surf, (255, 0, 0), -Setup.camera_offset, pg.Vector2(SCREEN_WIDTH, -Setup.camera_offset.y), 10)
+ pg.draw.line(surf, (255, 0, 0), -Setup.camera_offset, pg.Vector2(-Setup.camera_offset.x, SCREEN_HEIGHT), 10)
# self.pet.draw(surf)
# Apply gravity
self.velocity.y += self.gravity
- 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.copy(), delta)
+ # print(self.position)
def move_and_collide(self, pos, vel, delta):
pos.x += vel.x * delta
continue
if collision_rect.colliderect(thing.get_collision_rect()):
if thing.movable:
- thing.velocity.x = vel.x
+ if vel.x > 0:
+ thing.position.x = pos.x + self.width
+ elif vel.x < 0:
+ thing.position.x = pos.x - thing.width
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
if thing == self:
continue
if collision_rect.colliderect(thing.get_collision_rect()):
- if thing.movable:
- 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