예제 #1
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)
예제 #2
0
class Player(EntityBase):
    # Initializer
    def __init__(self, speed, screen, x, y):
        # Super initializer
        super(Player, self).__init__(x, y)

        # Properties
        # Input class instance
        self.input = Input(self)

        self.screen = screen
        self.speed = speed

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}

    # Update method
    def update(self):
        # Call update on goTrait
        self.traits["goTrait"].update()

        # Check for input on input
        self.input.checkForInput()

        # Draw sprite
        self.draw()

    # Draw method
    def draw(self):
        # Draw blue circle
        pygame.draw.circle(
            self.screen, pygame.color.Color("#8080FF"),
            [int(self.rect[0]), int(self.rect[1])], 12)
예제 #3
0
class Background(EntityBase):
    # Initializer
    def __init__(self, screen, x, y):
        # Super initializer
        super(Background, self).__init__(x, y)

        # Input class instance
        self.input = Input(self)

        # Properties
        self.img = pygame.transform.rotozoom(
            pygame.image.load("imgs\grass2.png"), 0, 10)
        self.screen = screen
        self.speed = 0

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}

    # Update method
    def update(self):
        # Call update on goTrait class instance
        self.traits["goTrait"].update()

        # Check for input on input instance
        self.input.checkForInput()

        # Draw sprite
        self.draw()

    # Draw method
    def draw(self):
        # Blit self.img
        self.screen.blit(self.img, (self.rect[0], self.rect[1]))
예제 #4
0
파일: Object.py 프로젝트: thcheetah777/rpg
class Object(EntityBase):
    # Initializer
    def __init__(self, screen, x, y, color):
        # Super initializer
        super(Object, self).__init__(x, y)

        # Input class instance
        self.input = Input(self)

        self.screen = screen
        self.color = color
        self.speed = 0

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}

    # Update method
    def update(self):
        # Calls update on goTrait instance
        self.traits["goTrait"].update()
        self.input.checkForInput()
        self.draw()

    # Draw method
    def draw(self):
        # Draw red circle
        pygame.draw.circle(
            self.screen, pygame.color.Color(self.color),
            [int(self.rect[0]), int(self.rect[1])], 12)
예제 #5
0
파일: Object.py 프로젝트: thcheetah777/rpg
    def __init__(self, screen, x, y, color):
        # Super initializer
        super(Object, self).__init__(x, y)

        # Input class instance
        self.input = Input(self)

        self.screen = screen
        self.color = color
        self.speed = 0

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}
예제 #6
0
    def __init__(self, speed, screen, x, y):
        # Super initializer
        super(Player, self).__init__(x, y)

        # Properties
        # Input class instance
        self.input = Input(self)

        self.screen = screen
        self.speed = speed

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}
예제 #7
0
    def __init__(self, screen, x, y):
        # Super initializer
        super(Background, self).__init__(x, y)

        # Input class instance
        self.input = Input(self)

        # Properties
        self.img = pygame.transform.rotozoom(
            pygame.image.load("imgs\grass2.png"), 0, 10)
        self.screen = screen
        self.speed = 0

        # Traits
        self.traits = {"goTrait": goTrait(self.screen, self, self.speed)}
예제 #8
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)
예제 #9
0
def main():
    pygame.mixer.pre_init(44100, -16, 2, 2048)
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    max_frame_rate = 60

    level = Level(screen)
    dashboard = Dashboard("./img/font.png", 8, screen)
    mario = Mario(0, 0, level, screen, dashboard)
    input = Input(mario)
    clock = pygame.time.Clock()

    while (not mario.restart):
        pygame.display.set_caption("{:.2f} FPS".format(clock.get_fps()))
        level.drawLevel(mario.camera)
        dashboard.update()
        input.checkForInput()
        mario.update()
        pygame.display.update()
        clock.tick(max_frame_rate)
    mario.sound.shutdown()
    main()
예제 #10
0
    def __init__(self, x, y, level, gravity=1.25):
        super(MarioHeadless, self).__init__(x, y, gravity)
        self.input = Input(self)

        self.traits = {
            "jumpTrait": jumpTrait(self),
            "goTrait": goTraitHeadless(self),
            "bounceTrait": bounceTrait(self)
        }

        self.levelObj = level
        self.collision = Collider(self, level)
        self.EntityCollider = EntityCollider(self)
        self.restart = False
예제 #11
0
    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)
예제 #12
0
class Mario(EntityBase):
    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)

    def update(self):
        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 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
            else:
                mob.rect.x += 5
                mob.leftrightTrait.direction = 1
            mob.alive = "shellBouncing"
        elif collisionState.isColliding and mob.alive:
            self.gameOver()

    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
예제 #13
0
class Mario(EntityBase):
    def __init__(self, x, y, level, screen, dashboard, sound, gravity=1.25):
        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.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

    def update(self):
        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.sound.play_sfx(self.sound.bump)
        block.triggered = True

    def _onCollisionWithMob(self, mob, collisionState):
        if collisionState.isTop and (mob.alive is True
                                     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.isTop and mob.alive == "sleeping":
            if (mob.rect.x < self.rect.x):
                mob.leftrightTrait.direction = -1
            else:
                mob.leftrightTrait.direction = 1
            mob.alive = "shellBouncing"
        elif collisionState.isColliding and mob.alive == True:
            self.gameOver()

    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 += -1000

    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) + 2, self.rect.y + 2), 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 doRandomMove(self):
        moves = ['moveLeft', 'moveRight', 'jump', 'doNothing']
        random_move = random.choice(moves)
        if random_move == 'moveLeft':
            self.traits['goTrait'].direction = -1
        elif random_move == 'moveRight':
            self.traits['goTrait'].direction = 1
        elif random_move == 'jump':
            self.traits['jumpTrait'].start()
        elif random_move == 'doNothing':
            self.traits['goTrait'].direction = 0
예제 #14
0
class Mario(EntityBase):
    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
        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.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 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 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
                self.sound.play_sfx(self.sound.kick)
            else:
                mob.rect.x += 5
                mob.leftrightTrait.direction = 1
                self.sound.play_sfx(self.sound.kick)
        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 = True
            ent.active = False
            ent.bouncing = False
        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
예제 #15
0
class Mario(EntityBase):
    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)

    def getInputs(self):
        map = []
        input = []
        for x in range(60):
            map.append(self.CT.test(x, 11, self.level, self))
            if map[x] == 3:
                map[x] = 0

        for x in range(int(self.rect.x/32), int(self.rect.x/32) + 8):
            input.append(map[x])
        return torch.FloatTensor(input)

    def update(self):
        self.updateTraits()
        self.moveMario()
        self.camera.move()
        self.applyGravity()
        self.checkEntityCollision()
        self.output = self.brain.forward(self.getInputs())
        #print(self.output)
        self.OI.checkForInput(self.output)

    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 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
            else:
                mob.rect.x += 5
                mob.leftrightTrait.direction = 1
            mob.alive = "shellBouncing"
        elif collisionState.isColliding and mob.alive:
            self.gameOver()

    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):
        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
예제 #16
0
class Mario(EntityBase):
    def __init__(self, x, y, level, screen, dashboard, sound, gravity=1.25):
        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.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

    def update(self):
        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:
            collission = self.EntityCollider.check(ent)
            if (collission != False):
                if (ent.type == "Item"):
                    self.levelObj.entityList.remove(ent)
                    self.dashboard.points += 100
                    self.dashboard.coins += 1
                    self.sound.play_sfx(self.sound.coin)
                elif (ent.type == "Block"):
                    if (not ent.triggered):
                        self.sound.play_sfx(self.sound.bump)
                    ent.triggered = True
                elif (ent.type == "Mob"):
                    if collission == "top" and (ent.alive == True or ent.alive
                                                == "shellBouncing"):
                        self.sound.play_sfx(self.sound.stomp)
                        self.rect.bottom = ent.rect.top
                        self.bounce()
                        self.killEntity(ent)
                    elif collission == "top" and ent.alive == "sleeping":
                        self.sound.play_sfx(self.sound.stomp)
                        self.rect.bottom = ent.rect.top
                        ent.timer = 0
                        self.bounce()
                        ent.alive = False
                    elif collission and ent.alive == "sleeping":
                        if (ent.rect.x < self.rect.x):
                            ent.leftrightTrait.direction = -1
                        else:
                            ent.leftrightTrait.direction = 1
                        ent.alive = "shellBouncing"
                    elif collission and ent.alive == True:
                        self.gameOver()

    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)