Skip to content

Commit

Permalink
fixed #99
Browse files Browse the repository at this point in the history
  • Loading branch information
mx0c committed Jan 29, 2021
1 parent 1a2a967 commit 740de8d
Showing 1 changed file with 186 additions and 185 deletions.
371 changes: 186 additions & 185 deletions entities/Mario.py
Original file line number Diff line number Diff line change
@@ -1,185 +1,186 @@
import pygame

from classes.Animation import Animation
from classes.Camera import Camera
from classes.Collider import Collider
from classes.EntityCollider import EntityCollider
from classes.Input import Input
from classes.Sprites import Sprites
from entities.EntityBase import EntityBase
from entities.Mushroom import RedMushroom
from traits.bounce import bounceTrait
from traits.go import GoTrait
from traits.jump import JumpTrait
from classes.Pause import Pause

spriteCollection = Sprites().spriteCollection
smallAnimation = Animation(
[
spriteCollection["mario_run1"].image,
spriteCollection["mario_run2"].image,
spriteCollection["mario_run3"].image,
],
spriteCollection["mario_idle"].image,
spriteCollection["mario_jump"].image,
)
bigAnimation = Animation(
[
spriteCollection["mario_big_run1"].image,
spriteCollection["mario_big_run2"].image,
spriteCollection["mario_big_run3"].image,
],
spriteCollection["mario_big_idle"].image,
spriteCollection["mario_big_jump"].image,
)


class Mario(EntityBase):
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
super(Mario, self).__init__(x, y, gravity)
self.camera = Camera(self.rect, self)
self.sound = sound
self.input = Input(self)
self.inAir = False
self.inJump = False
self.powerUpState = 0
self.invincibilityFrames = 0
self.traits = {
"jumpTrait": JumpTrait(self),
"goTrait": GoTrait(smallAnimation, screen, self.camera, self),
"bounceTrait": bounceTrait(self),
}

self.levelObj = level
self.collision = Collider(self, level)
self.screen = screen
self.EntityCollider = EntityCollider(self)
self.dashboard = dashboard
self.restart = False
self.pause = False
self.pauseObj = Pause(screen, self, dashboard)

def update(self):
if self.invincibilityFrames > 0:
self.invincibilityFrames -= 1
self.updateTraits()
self.moveMario()
self.camera.move()
self.applyGravity()
self.checkEntityCollision()
self.input.checkForInput()

def moveMario(self):
self.rect.y += self.vel.y
self.collision.checkY()
self.rect.x += self.vel.x
self.collision.checkX()

def checkEntityCollision(self):
for ent in self.levelObj.entityList:
collisionState = self.EntityCollider.check(ent)
if collisionState.isColliding:
if ent.type == "Item":
self._onCollisionWithItem(ent)
elif ent.type == "Block":
self._onCollisionWithBlock(ent)
elif ent.type == "Mob":
self._onCollisionWithMob(ent, collisionState)

def _onCollisionWithItem(self, item):
self.levelObj.entityList.remove(item)
self.dashboard.points += 100
self.dashboard.coins += 1
self.sound.play_sfx(self.sound.coin)

def _onCollisionWithBlock(self, block):
if not block.triggered:
self.dashboard.coins += 1
self.sound.play_sfx(self.sound.bump)
block.triggered = True

def _onCollisionWithMob(self, mob, collisionState):
if isinstance(mob, RedMushroom) and mob.alive:
self.powerup(1)
self.killEntity(mob)
self.sound.play_sfx(self.sound.powerup)
elif collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"):
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":
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":
if mob.rect.x < self.rect.x:
mob.leftrightTrait.direction = -1
mob.rect.x += -5
self.sound.play_sfx(self.sound.kick)
else:
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()
elif self.powerUpState == 1:
self.powerUpState = 0
self.traits['goTrait'].updateAnimation(smallAnimation)
x, y = self.rect.x, self.rect.y
self.rect = pygame.Rect(x, y + 32, 32, 32)
self.invincibilityFrames = 60
self.sound.play_sfx(self.sound.pipe)

def bounce(self):
self.traits["bounceTrait"].jump = True

def killEntity(self, ent):
if ent.__class__.__name__ != "Koopa":
ent.alive = False
else:
ent.timer = 0
ent.alive = "sleeping"
self.dashboard.points += 100

def gameOver(self):
srf = pygame.Surface((640, 480))
srf.set_colorkey((255, 255, 255), pygame.RLEACCEL)
srf.set_alpha(128)
self.sound.music_channel.stop()
self.sound.music_channel.play(self.sound.death)

for i in range(500, 20, -2):
srf.fill((0, 0, 0))
pygame.draw.circle(
srf,
(255, 255, 255),
(int(self.camera.x + self.rect.x) + 16, self.rect.y + 16),
i,
)
self.screen.blit(srf, (0, 0))
pygame.display.update()
self.input.checkForInput()
while self.sound.music_channel.get_busy():
pygame.display.update()
self.input.checkForInput()
self.restart = True

def getPos(self):
return self.camera.x + self.rect.x, self.rect.y

def setPos(self, x, y):
self.rect.x = x
self.rect.y = y

def powerup(self, powerupID):
if self.powerUpState == 0:
if powerupID == 1:
self.powerUpState = 1
self.traits['goTrait'].updateAnimation(bigAnimation)
self.rect = pygame.Rect(self.rect.x, self.rect.y-32, 32, 64)
self.invincibilityFrames = 20
import pygame

from classes.Animation import Animation
from classes.Camera import Camera
from classes.Collider import Collider
from classes.EntityCollider import EntityCollider
from classes.Input import Input
from classes.Sprites import Sprites
from entities.EntityBase import EntityBase
from entities.Mushroom import RedMushroom
from traits.bounce import bounceTrait
from traits.go import GoTrait
from traits.jump import JumpTrait
from classes.Pause import Pause

spriteCollection = Sprites().spriteCollection
smallAnimation = Animation(
[
spriteCollection["mario_run1"].image,
spriteCollection["mario_run2"].image,
spriteCollection["mario_run3"].image,
],
spriteCollection["mario_idle"].image,
spriteCollection["mario_jump"].image,
)
bigAnimation = Animation(
[
spriteCollection["mario_big_run1"].image,
spriteCollection["mario_big_run2"].image,
spriteCollection["mario_big_run3"].image,
],
spriteCollection["mario_big_idle"].image,
spriteCollection["mario_big_jump"].image,
)


class Mario(EntityBase):
def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
super(Mario, self).__init__(x, y, gravity)
self.camera = Camera(self.rect, self)
self.sound = sound
self.input = Input(self)
self.inAir = False
self.inJump = False
self.powerUpState = 0
self.invincibilityFrames = 0
self.traits = {
"jumpTrait": JumpTrait(self),
"goTrait": GoTrait(smallAnimation, screen, self.camera, self),
"bounceTrait": bounceTrait(self),
}

self.levelObj = level
self.collision = Collider(self, level)
self.screen = screen
self.EntityCollider = EntityCollider(self)
self.dashboard = dashboard
self.restart = False
self.pause = False
self.pauseObj = Pause(screen, self, dashboard)

def update(self):
if self.invincibilityFrames > 0:
self.invincibilityFrames -= 1
self.updateTraits()
self.moveMario()
self.camera.move()
self.applyGravity()
self.checkEntityCollision()
self.input.checkForInput()

def moveMario(self):
self.rect.y += self.vel.y
self.collision.checkY()
self.rect.x += self.vel.x
self.collision.checkX()

def checkEntityCollision(self):
for ent in self.levelObj.entityList:
collisionState = self.EntityCollider.check(ent)
if collisionState.isColliding:
if ent.type == "Item":
self._onCollisionWithItem(ent)
elif ent.type == "Block":
self._onCollisionWithBlock(ent)
elif ent.type == "Mob":
self._onCollisionWithMob(ent, collisionState)

def _onCollisionWithItem(self, item):
self.levelObj.entityList.remove(item)
self.dashboard.points += 100
self.dashboard.coins += 1
self.sound.play_sfx(self.sound.coin)

def _onCollisionWithBlock(self, block):
if not block.triggered:
self.dashboard.coins += 1
self.sound.play_sfx(self.sound.bump)
block.triggered = True

def _onCollisionWithMob(self, mob, collisionState):
if isinstance(mob, RedMushroom) and mob.alive:
self.powerup(1)
self.killEntity(mob)
self.sound.play_sfx(self.sound.powerup)
elif collisionState.isTop and (mob.alive or mob.alive == "shellBouncing"):
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":
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":
if mob.rect.x < self.rect.x:
mob.leftrightTrait.direction = -1
mob.rect.x += -5
self.sound.play_sfx(self.sound.kick)
else:
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()
elif self.powerUpState == 1:
self.powerUpState = 0
self.traits['goTrait'].updateAnimation(smallAnimation)
x, y = self.rect.x, self.rect.y
self.rect = pygame.Rect(x, y + 32, 32, 32)
self.invincibilityFrames = 60
self.sound.play_sfx(self.sound.pipe)

def bounce(self):
self.traits["bounceTrait"].jump = True

def killEntity(self, ent):
if ent.__class__.__name__ != "Koopa":
ent.alive = False
else:
ent.timer = 0
ent.leftrightTrait.speed = 1
ent.alive = "sleeping"
self.dashboard.points += 100

def gameOver(self):
srf = pygame.Surface((640, 480))
srf.set_colorkey((255, 255, 255), pygame.RLEACCEL)
srf.set_alpha(128)
self.sound.music_channel.stop()
self.sound.music_channel.play(self.sound.death)

for i in range(500, 20, -2):
srf.fill((0, 0, 0))
pygame.draw.circle(
srf,
(255, 255, 255),
(int(self.camera.x + self.rect.x) + 16, self.rect.y + 16),
i,
)
self.screen.blit(srf, (0, 0))
pygame.display.update()
self.input.checkForInput()
while self.sound.music_channel.get_busy():
pygame.display.update()
self.input.checkForInput()
self.restart = True

def getPos(self):
return self.camera.x + self.rect.x, self.rect.y

def setPos(self, x, y):
self.rect.x = x
self.rect.y = y

def powerup(self, powerupID):
if self.powerUpState == 0:
if powerupID == 1:
self.powerUpState = 1
self.traits['goTrait'].updateAnimation(bigAnimation)
self.rect = pygame.Rect(self.rect.x, self.rect.y-32, 32, 64)
self.invincibilityFrames = 20

0 comments on commit 740de8d

Please sign in to comment.