Skip to content

Commit

Permalink
Merge pull request #1 from magisterka-ppp/Collisions
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
Matrer authored Dec 21, 2020
2 parents 57c8aa1 + 8d595bc commit 62b816f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
8 changes: 7 additions & 1 deletion ground.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ def __init__(self):
self.platforms = [
Rect(0, 700, 500, 20),
Rect(210, 300, 200, 100),
Rect(550, 500, 200, 100),
Rect(550, 200, 200, 100),
Rect(550, 700, 100, 20),
Rect(1000, 700, 100, 20),
Rect(0, 0, 10, 720)]
self.color = (0, 155, 0)

def draw(self, screen):
for platform in self.platforms:
pygame.draw.rect(screen, self.color, platform)
pygame.draw.rect(screen, self.color, Rect(
platform.x,
platform.y,
platform.width,
platform.height))

64 changes: 33 additions & 31 deletions player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ class Player(object):
def __init__(self):
self.grounded = False
self.max_y_vel = 20
self.drag = 0.85
self.drag = 0.8
self.gravity = 6
self.collision_box = Rect(20, 0, 80, 120)
self.bounds = Rect(20, 0, 80, 120)
self.color = (155, 155, 0)
self.vel = Vector2(0, 0)
self.acc = Vector2(0, 0)

def draw(self, screen):
pygame.draw.rect(screen, self.color, self.collision_box)
#pygame.draw.rect(screen, (155, 0, 0), self.get_feet())
#pygame.draw.rect(screen, (155, 0, 0), self.get_head())
#pygame.draw.rect(screen, (155, 0, 0), self.get_right())
#pygame.draw.rect(screen, (155, 0, 0), self.get_left())
pygame.draw.rect(screen, self.color,
Rect(self.bounds.x,
self.bounds.y,
self.bounds.width,
self.bounds.height))

def add_force(self, force):
self.acc = force
Expand All @@ -40,17 +40,13 @@ def logic(self, game):
self.limit_fall_speed()

self.vel.x *= self.drag
self.collision_box.x += round(self.vel.x, 0)




self.bounds.x += round(self.vel.x, 0)

def limit_fall_speed(self):
if self.vel.y < self.max_y_vel:
self.collision_box.y += self.vel.y
self.bounds.y += self.vel.y
else:
self.collision_box.y += self.max_y_vel
self.bounds.y += self.max_y_vel

def collisions(self, colliders):
self.collisions_head(colliders)
Expand All @@ -63,8 +59,16 @@ def collisions_head(self, colliders):
for collider in colliders:
if head.colliderect(collider):
self.vel.y = 0
if right.colliderect(collider) or left.colliderect(collider):
self.bounds.y = self.bounds.y - (self.bounds.y - (collider.y + collider.height))

if right.colliderect(collider):
self.vel.x = 0
self.bounds.x = self.bounds.x - self.bounds.width - (self.bounds.x - collider.x)

if left.colliderect(collider):
self.vel.x = 0
self.bounds.x = self.bounds.x - (self.bounds.x - (collider.x + collider.width))


def collisions_feet(self, colliders):
feet = self.get_feet()
Expand All @@ -74,39 +78,37 @@ def collisions_feet(self, colliders):
if feet.colliderect(collider) and self.vel.y > 0.0:
self.grounded = True
self.vel.y = 0
self.collision_box.y = collider.y - self.collision_box.height
self.bounds.y = collider.y - self.bounds.height

def get_feet(self):
return Rect(self.collision_box.x,
self.collision_box.y + self.collision_box.height,
self.collision_box.width,
return Rect(self.bounds.x,
self.bounds.y + self.bounds.height,
self.bounds.width,
self.max_y_vel)

def get_head(self):
velocity = 0
if self.vel.y < 0:
velocity = self.vel.y/2
return Rect(self.collision_box.x,
self.collision_box.y + velocity,
self.collision_box.width,
velocity = self.vel.y
return Rect(self.bounds.x,
self.bounds.y + velocity,
self.bounds.width,
-velocity)

def get_right(self):
velocity = 0
if self.vel.x > 0:
velocity = self.vel.x

return Rect(self.collision_box.x + self.collision_box.width,
self.collision_box.y,
return Rect(self.bounds.x + self.bounds.width,
self.bounds.y,
velocity,
self.collision_box.height)
self.bounds.height)

def get_left(self):
velocity = 0
if self.vel.x < 0:
velocity = self.vel.x

return Rect(self.collision_box.x + velocity,
self.collision_box.y,
return Rect(self.bounds.x + velocity,
self.bounds.y,
-velocity,
self.collision_box.height)
self.bounds.height)

0 comments on commit 62b816f

Please sign in to comment.