Example #1
0
class Goomba(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Goomba, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([self.spriteCollection.get("goomba-1").image,
                                    self.spriteCollection.get("goomba-2").image])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.type = "Mob"
        self.dashboard = level.dashboard

    def update(self, camera):
        if(self.alive):
            self.applyGravity()
            self.screen.blit(
                self.animation.image,
                (self.rect.x + camera.x,
                 self.rect.y))
            self.animation.update()
            self.leftrightTrait.update()
        else:
            if(self.timer == 0):
                self.textPos = vec2D(self.rect.x + 3, self.rect.y)
            if(self.timer < self.timeAfterDeath):
                self.textPos.y += -0.5
                self.dashboard.drawText(
                    "100", self.textPos.x + camera.x, self.textPos.y, 8)
                self.screen.blit(self.spriteCollection.get(
                    "goomba-flat").image, (self.rect.x + camera.x, self.rect.y))
            else:
                self.alive = None
            self.timer += 0.1
Example #2
0
class Goomba(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Goomba, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([
            self.spriteCollection.get("goomba-1").image,
            self.spriteCollection.get("goomba-2").image
        ])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.type = "Mob"

    def update(self, camera):
        if (self.alive):
            self.applyGravity()
            self.screen.blit(self.animation.image,
                             (self.rect.x + camera.pos.x * 32, self.rect.y))
            self.animation.update()
            self.leftrightTrait.update()
        else:
            if (self.timer < self.timeAfterDeath):
                self.screen.blit(
                    self.spriteCollection.get("goomba-flat").image,
                    (self.rect.x + camera.pos.x * 32, self.rect.y))
            else:
                self.alive = None
            self.timer += 0.1
Example #3
0
class Lacy(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Lacy, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation(
            [
                self.spriteCollection.get("lacy-1").image,
                self.spriteCollection.get("lacy-2").image,
            ]
        )
        self.screen = screen
        self.leftrightTrait = LacyFight(self, level)
        self.type = "Mob"
        self.inAir = False
        self.inJump = False
        self.dashboard = level.dashboard
        self.lives = 3
        self.level = level
        self.immuneTimer = 0

    def update(self, camera):
        self.immuneTimer += 1
        if self.lives < 1:
            self.drawFlatGoomba(camera)
        if self.alive:
            self.applyGravity()
            self.drawLacy(camera)
            self.leftrightTrait.update()
        else:
            self.onDead(camera)

    def drawLacy(self, camera):
        self.screen.blit(self.animation.image, (self.rect.x + camera.x - 16, self.rect.y - 32))
        self.animation.update()

    def onDead(self, camera):
        if self.timer == 0:
            self.setPointsTextStartPosition(self.rect.x + 3, self.rect.y)
        if self.timer < self.timeAfterDeath and self.immuneTimer > 50:
            self.lives = self.lives - 1
            self.immuneTimer = 0
            self.level.mario.setPos(self.level.mario.rect.x, 3*32)
        else:
            self.alive = None
        self.timer += 0.1

    def drawFlatGoomba(self, camera):
        self.level.mario.sound.music_channel.stop()
        self.level.mario.sound.play_sfx(self.level.mario.sound.clear)
        sleep(6.5)
        self.level.mario.restart = True

    def setPointsTextStartPosition(self, x, y):
        self.textPos = vec2D(x, y)

    def movePointsTextUpAndDraw(self, camera):
        self.textPos.y += -0.5
        self.dashboard.drawText("100", self.textPos.x + camera.x, self.textPos.y, 8)
Example #4
0
 def __init__(self, screen, spriteColl, x, y, level):
     super(Goomba, self).__init__(y, x - 1, 1.25)
     self.spriteCollection = spriteColl
     self.animation = Animation([self.spriteCollection.get("goomba-1").image,
                                 self.spriteCollection.get("goomba-2").image])
     self.screen = screen
     self.leftrightTrait = LeftRightWalkTrait(self, level)
     self.type = "Mob"
     self.dashboard = level.dashboard
Example #5
0
 def __init__(self, screen, spriteColl, x, y, level):
     super(Koopa, self).__init__(y - 1, x, 1.25)
     self.spriteCollection = spriteColl
     self.animation = Animation([self.spriteCollection.get("koopa-1").image,
                                 self.spriteCollection.get("koopa-2").image])
     self.screen = screen
     self.leftrightTrait = LeftRightWalkTrait(self, level)
     self.timer = 0
     self.timeAfterDeath = 35
     self.type = "Mob"
     self.dashboard = level.dashboard
Example #6
0
 def __init__(self, screen, spriteColl, x, y, level, sound):
     super(RedMushroom, self).__init__(y, x - 1, 1.25)
     self.spriteCollection = spriteColl
     self.animation = Animation([
         self.spriteCollection.get("mushroom").image,
     ])
     self.screen = screen
     self.leftrightTrait = LeftRightWalkTrait(self, level)
     self.type = "Mob"
     self.dashboard = level.dashboard
     self.collision = Collider(self, level)
     self.EntityCollider = EntityCollider(self)
     self.levelObj = level
     self.sound = sound
Example #7
0
class Goomba(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Goomba, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([
            self.spriteCollection.get("goomba-1").image,
            self.spriteCollection.get("goomba-2").image,
        ])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.type = "Mob"
        self.dashboard = level.dashboard

    def update(self, camera):
        if self.alive:
            self.applyGravity()
            self.drawGoomba(camera)
            self.leftrightTrait.update()
        else:
            self.onDead(camera)

    def drawGoomba(self, camera):
        self.screen.blit(self.animation.image,
                         (self.rect.x + camera.x, self.rect.y))
        self.animation.update()

    def onDead(self, camera):
        if self.timer == 0:
            self.setPointsTextStartPosition(self.rect.x + 3, self.rect.y)
        if self.timer < self.timeAfterDeath:
            self.movePointsTextUpAndDraw(camera)
            self.drawFlatGoomba(camera)
        else:
            self.alive = None
        self.timer += 0.1

    def drawFlatGoomba(self, camera):
        self.screen.blit(
            self.spriteCollection.get("goomba-flat").image,
            (self.rect.x + camera.x, self.rect.y),
        )

    def setPointsTextStartPosition(self, x, y):
        self.textPos = vec2D(x, y)

    def movePointsTextUpAndDraw(self, camera):
        self.textPos.y += -0.5
        self.dashboard.drawText("100", self.textPos.x + camera.x,
                                self.textPos.y, 8)
Example #8
0
class RedMushroom(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level, sound):
        super(RedMushroom, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([
            self.spriteCollection.get("mushroom").image,
        ])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.type = "Mob"
        self.dashboard = level.dashboard
        self.collision = Collider(self, level)
        self.EntityCollider = EntityCollider(self)
        self.levelObj = level
        self.sound = sound

    def update(self, camera):
        if self.alive:
            self.applyGravity()
            self.drawRedMushroom(camera)
            self.leftrightTrait.update()
            self.checkEntityCollision()
        else:
            self.onDead(camera)

    def drawRedMushroom(self, camera):
        self.screen.blit(self.animation.image,
                         (self.rect.x + camera.x, self.rect.y))
        self.animation.update()

    def onDead(self, camera):
        if self.timer == 0:
            self.setPointsTextStartPosition(self.rect.x + 3, self.rect.y)
        if self.timer < self.timeAfterDeath:
            self.movePointsTextUpAndDraw(camera)
        else:
            self.alive = None
        self.timer += 0.1

    def setPointsTextStartPosition(self, x, y):
        self.textPos = Vec2D(x, y)

    def movePointsTextUpAndDraw(self, camera):
        self.textPos.y += -0.5
        self.dashboard.drawText("100", self.textPos.x + camera.x,
                                self.textPos.y, 8)

    def checkEntityCollision(self):
        pass
Example #9
0
 def __init__(self, screen, spriteColl, x, y, level):
     super(Sal, self).__init__(y, x - 1, 1.25)
     self.spriteCollection = spriteColl
     self.animation = Animation([
         self.spriteCollection.get("sal-1").image,
         self.spriteCollection.get("sal-2").image,
     ])
     self.screen = screen
     self.leftrightTrait = SalFight(self, level)
     self.type = "Mob"
     self.inAir = False
     self.inJump = False
     self.dashboard = level.dashboard
     self.lives = 4
     self.level = level
     self.immuneTimer = 0
Example #10
0
    def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
        super(Mario, self).__init__(x, y, gravity)
        self.spriteCollection = Sprites().spriteCollection
        self.camera = Camera(self.rect, self)
        self.sound = sound
        self.input = Input(self)
        self.inAir = False
        self.inJump = False
        self.animation = Animation(
            [
                self.spriteCollection["mario_run1"].image,
                self.spriteCollection["mario_run2"].image,
                self.spriteCollection["mario_run3"].image,
            ],
            self.spriteCollection["mario_idle"].image,
            self.spriteCollection["mario_jump"].image,
        )

        self.traits = {
            "jumpTrait": jumpTrait(self),
            "goTrait": goTrait(self.animation, 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)
Example #11
0
    def __init__(self, numero, pj, name):
        self.n = numero
        self.balas = []
        self.maxHp = 100
        self.name = name
        self.nameRender = pygame.font.Font("fonts/Pixeled.ttf",
                                           13).render(name, 0, (0, 0, 0))
        self.pj = pj
        self.currentHp = self.maxHp
        self.direction = [0, 0]
        self.lastDirection = [0, 0]
        self.lastPos = [0, 0]
        self.moving = False
        self.state = "normal"
        self.time = 0

        #image stuff----------------------------------------------
        #cargar imagenes y animaciones comunes a todos
        nImageStr = str(self.pj)
        self.fotogramaEnAnimacion = 0
        self.currentAnimation = None
        self.nextAnimations = []
        self.animationStillRight = Animation("StillRight", 2, 200, nImageStr)
        self.animationStillLeft = Animation("StillLeft", 2, 200, nImageStr,
                                            self.animationStillRight, 1,
                                            0)  #la left es igual que la right
        self.imageDead = load_image("graphics/pjs/dead.png", True)

        #cargar imagenes y animaciones especificas de cada uno
        if self.pj == 1:
            self.animationChargeAttackRight = Animation(
                "ChargeAttackRight", 8, 62.5, nImageStr)
            self.animationChargeAttackLeft = Animation(
                "ChargeAttackLeft", 8, 62.5, nImageStr,
                self.animationChargeAttackRight, 1, 0)

        if self.pj == 3:
            self.animationChargeAttackRight = Animation(
                "ChargeAttackRight", 5, 100, nImageStr)
            self.animationChargeAttackLeft = Animation(
                "ChargeAttackLeft", 5, 100, nImageStr,
                self.animationChargeAttackRight, 1, 0)

        if self.pj == 4 or self.pj == 2:
            self.animationUltiRight = Animation("UltiRight", 2, 200, nImageStr)
            self.animationUltiLeft = Animation("UltiLeft", 2, 200, nImageStr,
                                               self.animationUltiRight, 1, 0)

        #right quieto: 1
        #left quieto: 2
        #animacion right quieto: 3
        #animacion left quieto:4

        self.setAnimation(self.animationStillRight)
        self.image = self.animationStillRight[0]
        self.rect = self.image.get_rect()
Example #12
0
 def loadSprites(self, urlList):
     resDict = {}
     for url in urlList:
         with open(url) as jsonData:
             data = json.load(jsonData)
             mySpritesheet = Spritesheet(data['spriteSheetURL'])
             dic = {}
             if (data['type'] == "background"):
                 for sprite in data['sprites']:
                     try:
                         colorkey = sprite['colorKey']
                     except KeyError:
                         colorkey = None
                     dic[sprite['name']] = Sprite(
                         mySpritesheet.image_at(sprite['x'], sprite['y'],
                                                sprite['scalefactor'],
                                                colorkey),
                         sprite['collision'], None, sprite['redrawBg'])
                 resDict.update(dic)
                 continue
             elif data['type'] == "animation":
                 for sprite in data['sprites']:
                     images = []
                     for image in sprite['images']:
                         images.append(
                             mySpritesheet.image_at(
                                 image['x'],
                                 image['y'],
                                 image['scale'],
                                 colorkey=sprite['colorKey']))
                     dic[sprite['name']] = Sprite(
                         None,
                         None,
                         animation=Animation(images,
                                             deltaTime=sprite["deltaTime"]))
                 resDict.update(dic)
                 continue
             elif data['type'] == "character" or data['type'] == "item":
                 for sprite in data['sprites']:
                     try:
                         colorkey = sprite['colorKey']
                     except KeyError:
                         colorkey = None
                     dic[sprite['name']] = Sprite(
                         mySpritesheet.image_at(sprite['x'],
                                                sprite['y'],
                                                sprite['scalefactor'],
                                                colorkey,
                                                True,
                                                xTileSize=data['size'][0],
                                                yTileSize=data['size'][1]),
                         sprite['collision'])
                 resDict.update(dic)
                 continue
     return resDict
Example #13
0
    def __init__(self, x, y, level, screen, dashboard, sound, gravity=0.75):
        super(Mario, self).__init__(x, y, gravity)
        self.x = x
        self.spriteCollection = Sprites().spriteCollection
        self.CT = CollisionTester()
        self.camera = Camera(self.rect, self)
        self.sound = sound
        self.level = level
        self.OI = Input(self)
        self.closest_mob = None
        self.closest_object = None
        self.output =0
        self.inAir = False
        self.brain = Model().share_memory()
        self.fitness = 0

        self.animation = Animation(
            [
                self.spriteCollection["mario_run1"].image,
                self.spriteCollection["mario_run2"].image,
                self.spriteCollection["mario_run3"].image,
            ],
            self.spriteCollection["mario_idle"].image,
            self.spriteCollection["mario_jump"].image,
        )

        self.traits = {
            "jumpTrait": jumpTrait(self),
            "goTrait": goTrait(self.animation, 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)
Example #14
0
    def __init__(self, x, y, level, screen, dashboard, gravity=1.25):
        super(Mario, self).__init__(x, y, gravity)
        self.spriteCollection = Sprites().spriteCollection
        self.camera = Camera(self.rect, self)
        self.sound = Sound()

        self.animation = Animation([
            self.spriteCollection["mario_run1"].image,
            self.spriteCollection["mario_run2"].image,
            self.spriteCollection["mario_run3"].image
        ], self.spriteCollection["mario_idle"].image,
                                   self.spriteCollection["mario_jump"].image)

        self.traits = {
            "jumpTrait": jumpTrait(self),
            "goTrait": goTrait(self.animation, 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
Example #15
0
 def loadSprites(self, urlList):
     resDict = {}
     for url in urlList:
         with open(url) as jsonData:
             data = json.load(jsonData)
             mySpritesheet = Spritesheet(data["spriteSheetURL"])
             dic = {}
             if data["type"] == "background":
                 for sprite in data["sprites"]:
                     try:
                         colorkey = sprite["colorKey"]
                     except KeyError:
                         colorkey = None
                     dic[sprite["name"]] = Sprite(
                         mySpritesheet.image_at(
                             sprite["x"],
                             sprite["y"],
                             sprite["scalefactor"],
                             colorkey,
                         ),
                         sprite["collision"],
                         None,
                         sprite["redrawBg"],
                     )
                 resDict.update(dic)
                 continue
             elif data["type"] == "animation":
                 for sprite in data["sprites"]:
                     images = []
                     for image in sprite["images"]:
                         images.append(
                             mySpritesheet.image_at(
                                 image["x"],
                                 image["y"],
                                 image["scale"],
                                 colorkey=sprite["colorKey"],
                             )
                         )
                     dic[sprite["name"]] = Sprite(
                         None,
                         None,
                         animation=Animation(images, deltaTime=sprite["deltaTime"]),
                     )
                 resDict.update(dic)
                 continue
             elif data["type"] == "character" or data["type"] == "item":
                 for sprite in data["sprites"]:
                     try:
                         colorkey = sprite["colorKey"]
                     except KeyError:
                         colorkey = None
                     try:
                         xSize = sprite['xsize']
                         ySize = sprite['ysize']
                     except KeyError:
                         xSize, ySize = data['size']
                     dic[sprite["name"]] = Sprite(
                         mySpritesheet.image_at(
                             sprite["x"],
                             sprite["y"],
                             sprite["scalefactor"],
                             colorkey,
                             True,
                             xTileSize=xSize,
                             yTileSize=ySize,
                         ),
                         sprite["collision"],
                     )
                 resDict.update(dic)
                 continue
     return resDict
Example #16
0
class Goomba(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level, sound):
        super(Goomba, self).__init__(y, x - 1, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation(
            [
                self.spriteCollection.get("goomba-1").image,
                self.spriteCollection.get("goomba-2").image,
            ]
        )
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.type = "Mob"
        self.dashboard = level.dashboard
        self.collision = Collider(self, level)
        self.EntityCollider = EntityCollider(self)
        self.levelObj = level
        self.sound = sound
        self.textPos = Vec2D(0, 0)

    def update(self, camera):
        if self.alive:
            self.applyGravity()
            self.drawGoomba(camera)
            self.leftrightTrait.update()
            self.checkEntityCollision()
        else:
            self.onDead(camera)

    def drawGoomba(self, camera):
        self.screen.blit(self.animation.image, (self.rect.x + camera.x, self.rect.y))
        self.animation.update()

    def onDead(self, camera):
        if self.timer == 0:
            self.setPointsTextStartPosition(self.rect.x + 3, self.rect.y)
        if self.timer < self.timeAfterDeath:
            self.movePointsTextUpAndDraw(camera)
            self.drawFlatGoomba(camera)
        else:
            self.alive = None
        self.timer += 0.1

    def drawFlatGoomba(self, camera):
        self.screen.blit(
            self.spriteCollection.get("goomba-flat").image,
            (self.rect.x + camera.x, self.rect.y),
        )

    def setPointsTextStartPosition(self, x, y):
        self.textPos = Vec2D(x, y)

    def movePointsTextUpAndDraw(self, camera):
        self.textPos.y += -0.5
        self.dashboard.drawText("100", self.textPos.x + camera.x, self.textPos.y, 8)
    
    def checkEntityCollision(self):
        for ent in self.levelObj.entityList:
            collisionState = self.EntityCollider.check(ent)
            if collisionState.isColliding:
                if ent.type == "Mob":
                    self._onCollisionWithMob(ent, collisionState)

    def _onCollisionWithMob(self, mob, collisionState):
        if collisionState.isColliding and mob.alive == "shellBouncing":
            self.alive = False
            self.sound.play_sfx(self.sound.brick_bump)
Example #17
0
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,
)

Example #18
0
class Koopa(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Koopa, self).__init__(y - 1, x, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([
            self.spriteCollection.get("koopa-1").image,
            self.spriteCollection.get("koopa-2").image
        ])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.timer = 0
        self.timeAfterDeath = 35
        self.type = "Mob"

    def update(self, camera):
        if (self.alive == True):
            self.updateAlive(camera)
        elif self.alive == "sleeping":
            self.sleepingInShell(camera)
        elif self.alive == "shellBouncing":
            self.shellBouncing(camera)
        elif self.alive == False:
            self.die(camera)

    def drawKoopa(self, camera):
        if self.leftrightTrait.direction == -1:
            self.screen.blit(
                self.animation.image,
                (self.rect.x + camera.pos.x * 32, self.rect.y - 32))
        else:
            self.screen.blit(
                pygame.transform.flip(self.animation.image, True, False),
                (self.rect.x + camera.pos.x * 32, self.rect.y - 32))

    def shellBouncing(self, camera):
        self.leftrightTrait.speed = 4
        self.applyGravity()
        self.animation.image = self.spriteCollection.get("koopa-hiding").image
        self.drawKoopa(camera)
        self.leftrightTrait.update()

    def die(self, camera):
        if (self.timer < self.timeAfterDeath):
            self.vel.y -= 0.5
            self.rect.y += self.vel.y
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.pos.x * 32, self.rect.y - 32))
        else:
            self.vel.y += 0.3
            self.rect.y += self.vel.y
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.pos.x * 32, self.rect.y - 32))
            if (self.timer > 500):
                #delete entity
                self.alive = None
        self.timer += 6

    def sleepingInShell(self, camera):
        if (self.timer < self.timeAfterDeath):
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.pos.x * 32, self.rect.y - 32))
        else:
            self.alive = True
            self.timer = 0
        self.timer += 0.1

    def updateAlive(self, camera):
        self.applyGravity()
        self.drawKoopa(camera)
        self.animation.update()
        self.leftrightTrait.update()
Example #19
0
class Koopa(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level):
        super(Koopa, self).__init__(y - 1, x, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation([
            self.spriteCollection.get("koopa-1").image,
            self.spriteCollection.get("koopa-2").image,
        ])
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.timer = 0
        self.timeAfterDeath = 35
        self.type = "Mob"
        self.dashboard = level.dashboard
        self.collision = Collider(self, level)
        self.EntityCollider = EntityCollider(self)
        self.levelObj = level

    def update(self, camera):
        if self.alive == True:
            self.updateAlive(camera)
            self.checkEntityCollision()
        elif self.alive == "sleeping":
            self.sleepingInShell(camera)
            self.checkEntityCollision()
        elif self.alive == "shellBouncing":
            self.shellBouncing(camera)
        elif self.alive == False:
            self.die(camera)

    def drawKoopa(self, camera):
        if self.leftrightTrait.direction == -1:
            self.screen.blit(self.animation.image,
                             (self.rect.x + camera.x, self.rect.y - 32))
        else:
            self.screen.blit(
                pygame.transform.flip(self.animation.image, True, False),
                (self.rect.x + camera.x, self.rect.y - 32),
            )

    def shellBouncing(self, camera):
        self.leftrightTrait.speed = 4
        self.applyGravity()
        self.animation.image = self.spriteCollection.get("koopa-hiding").image
        self.drawKoopa(camera)
        self.leftrightTrait.update()

    def die(self, camera):
        if self.timer == 0:
            self.textPos = vec2D(self.rect.x + 3, self.rect.y - 32)
        if self.timer < self.timeAfterDeath:
            self.textPos.y += -0.5
            self.dashboard.drawText("100", self.textPos.x + camera.x,
                                    self.textPos.y, 8)
            self.vel.y -= 0.5
            self.rect.y += self.vel.y
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.x, self.rect.y - 32),
            )
        else:
            self.vel.y += 0.3
            self.rect.y += self.vel.y
            self.textPos.y += -0.5
            self.dashboard.drawText("100", self.textPos.x + camera.x,
                                    self.textPos.y, 8)
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.x, self.rect.y - 32),
            )
            if self.timer > 500:
                # delete entity
                self.alive = None
        self.timer += 6

    def sleepingInShell(self, camera):
        if self.timer < self.timeAfterDeath:
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.x, self.rect.y - 32),
            )
        else:
            self.alive = True
            self.timer = 0
        self.timer += 0.1

    def updateAlive(self, camera):
        self.applyGravity()
        self.drawKoopa(camera)
        self.animation.update()
        self.leftrightTrait.update()

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

    def _onCollisionWithMob(self, mob, collisionState):
        if collisionState.isColliding and mob.alive == "shellBouncing":
            self.alive = False
Example #20
0
class Koopa(EntityBase):
    def __init__(self, screen, spriteColl, x, y, level, sound):
        super(Koopa, self).__init__(y - 1, x, 1.25)
        self.spriteCollection = spriteColl
        self.animation = Animation(
            [
                self.spriteCollection.get("koopa-1").image,
                self.spriteCollection.get("koopa-2").image,
            ]
        )
        self.screen = screen
        self.leftrightTrait = LeftRightWalkTrait(self, level)
        self.timer = 0
        self.timeAfterDeath = 35
        self.type = "Mob"
        self.dashboard = level.dashboard
        self.collision = Collider(self, level)
        self.EntityCollider = EntityCollider(self)
        self.levelObj = level
        self.sound = sound

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

    def drawKoopa(self, camera):
        if self.leftrightTrait.direction == -1:
            self.screen.blit(
                self.animation.image, (self.rect.x + camera.x, self.rect.y - 32)
            )
        else:
            self.screen.blit(
                pygame.transform.flip(self.animation.image, True, False),
                (self.rect.x + camera.x, self.rect.y - 32),
            )

    def shellBouncing(self, camera):
        self.leftrightTrait.speed = 4
        self.applyGravity()
        self.animation.image = self.spriteCollection.get("koopa-hiding").image
        self.drawKoopa(camera)
        self.leftrightTrait.update()

    def sleepingInShell(self, camera):
        if self.timer < self.timeAfterDeath:
            self.screen.blit(
                self.spriteCollection.get("koopa-hiding").image,
                (self.rect.x + camera.x, self.rect.y - 32),
            )
        else:
            self.alive = True
            self.active = True
            self.bouncing = False
            self.timer = 0
        self.timer += 0.1

    def updateAlive(self, camera):
        self.applyGravity()
        self.drawKoopa(camera)
        self.animation.update()
        self.leftrightTrait.update()

    def checkEntityCollision(self):
        for ent in self.levelObj.entityList:
            if ent is not self:
                collisionState = self.EntityCollider.check(ent)
                if collisionState.isColliding:
                    if ent.type == "Mob":
                        self._onCollisionWithMob(ent, collisionState)

    def _onCollisionWithMob(self, mob, collisionState):
        if collisionState.isColliding and mob.bouncing:
            self.alive = False
            self.sound.play_sfx(self.sound.brick_bump)