Skip to content

Commit

Permalink
Merge pull request #101 from purry03/standardize-entity-alive
Browse files Browse the repository at this point in the history
Added attributes to entity class to make dealing with Koopa easier
  • Loading branch information
mx0c authored Feb 18, 2021
2 parents 740de8d + 4a5aa69 commit 750503d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions entities/EntityBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def __init__(self, x, y, gravity):
self.gravity = gravity
self.traits = None
self.alive = True
self.active = True
self.bouncing = False
self.timeAfterDeath = 5
self.timer = 0
self.type = ""
Expand Down
2 changes: 1 addition & 1 deletion entities/Goomba.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ def checkEntityCollision(self):
self._onCollisionWithMob(ent, collisionState)

def _onCollisionWithMob(self, mob, collisionState):
if collisionState.isColliding and mob.alive == "shellBouncing":
if collisionState.isColliding and mob.bouncing:
self.alive = False
self.sound.play_sfx(self.sound.brick_bump)
10 changes: 6 additions & 4 deletions entities/Koopa.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def __init__(self, screen, spriteColl, x, y, level, sound):
self.sound = sound

def update(self, camera):
if self.alive is True:
if self.alive and self.active:
self.updateAlive(camera)
self.checkEntityCollision()
elif self.alive == "sleeping":
elif self.alive and not self.active and not self.bouncing:
self.sleepingInShell(camera)
self.checkEntityCollision()
elif self.alive == "shellBouncing":
elif self.bouncing:
self.shellBouncing(camera)

def drawKoopa(self, camera):
Expand Down Expand Up @@ -65,6 +65,8 @@ def sleepingInShell(self, camera):
)
else:
self.alive = True
self.active = True
self.bouncing = False
self.timer = 0
self.timer += 0.1

Expand All @@ -83,6 +85,6 @@ def checkEntityCollision(self):
self._onCollisionWithMob(ent, collisionState)

def _onCollisionWithMob(self, mob, collisionState):
if collisionState.isColliding and mob.alive == "shellBouncing":
if collisionState.isColliding and mob.bouncing:
self.alive = False
self.sound.play_sfx(self.sound.brick_bump)
14 changes: 8 additions & 6 deletions entities/Mario.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


class Mario(EntityBase):
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.8):
super(Mario, self).__init__(x, y, gravity)
self.camera = Camera(self.rect, self)
self.sound = sound
Expand Down Expand Up @@ -103,18 +103,19 @@ def _onCollisionWithMob(self, mob, collisionState):
self.powerup(1)
self.killEntity(mob)
self.sound.play_sfx(self.sound.powerup)
elif collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"):
elif collisionState.isTop and (mob.alive or mob.bouncing):
self.sound.play_sfx(self.sound.stomp)
self.rect.bottom = mob.rect.top
self.bounce()
self.killEntity(mob)
elif collisionState.isTop and mob.alive == "sleeping":
elif collisionState.isTop and mob.alive and not mob.active:
self.sound.play_sfx(self.sound.stomp)
self.rect.bottom = mob.rect.top
mob.timer = 0
self.bounce()
mob.alive = False
elif collisionState.isColliding and mob.alive == "sleeping":
elif collisionState.isColliding and mob.alive and not mob.active and not mob.bouncing:
mob.bouncing = True
if mob.rect.x < self.rect.x:
mob.leftrightTrait.direction = -1
mob.rect.x += -5
Expand All @@ -123,7 +124,6 @@ def _onCollisionWithMob(self, mob, collisionState):
mob.rect.x += 5
mob.leftrightTrait.direction = 1
self.sound.play_sfx(self.sound.kick)
mob.alive = "shellBouncing"
elif collisionState.isColliding and mob.alive and not self.invincibilityFrames:
if self.powerUpState == 0:
self.gameOver()
Expand All @@ -144,7 +144,9 @@ def killEntity(self, ent):
else:
ent.timer = 0
ent.leftrightTrait.speed = 1
ent.alive = "sleeping"
ent.alive = True
ent.active = False
ent.bouncing = False
self.dashboard.points += 100

def gameOver(self):
Expand Down
6 changes: 3 additions & 3 deletions traits/jump.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class JumpTrait:
def __init__(self, entity):
self.vertical_speed = -12
self.verticalSpeed = -12
self.jumpHeight = 120
self.entity = entity
self.initalHeight = 384
self.deaccelerationHeight = self.jumpHeight - ((self.vertical_speed*self.vertical_speed)/(2*self.entity.gravity))
self.deaccelerationHeight = self.jumpHeight - ((self.verticalSpeed*self.verticalSpeed)/(2*self.entity.gravity))

def jump(self, jumping):
if jumping:
if not self.entity.inAir and not self.entity.inJump: # redundant check
self.entity.sound.play_sfx(self.entity.sound.jump)
self.entity.vel.y = self.vertical_speed
self.entity.vel.y = self.verticalSpeed
self.entity.inAir = True
self.initalHeight = self.entity.rect.y
self.entity.inJump = True
Expand Down

0 comments on commit 750503d

Please sign in to comment.