Exemplo n.º 1
0
class EffectRender(object):
    
    mEffects = None
    
    def __init__(self, camera):
        self.mEffects = []
        self.mCamera = camera
        self.blood = Animation(Resources.getInstance().mBlood, 5, 1, 0, self.mCamera.getScaledSize(1,1), False, False)
            
    def render(self, delta):
        remove = []

        for fx in self.mEffects:
            if fx.isAlive():
                for particle in fx.particles:
                    if particle.alive:
                        viewpos = self.mCamera.getViewCoords(particle.position)
                        random.seed(particle.mSize.x)
                        self.blood.freeze(random.randint(0, 4), 0)
                        self.blood.setSize(self.mCamera.getScaledSize(particle.mSize.x, particle.mSize.y))
                        self.blood.draw(delta, viewpos)
            else:
                remove.append(fx)
        
        for fx in remove:
            self.mEffects.remove(fx)
    
    def addFx(self, fx):
        self.mEffects.append(fx)
Exemplo n.º 2
0
class LevelTimeScreen(BaseMenuScreen):

    def __init__(self, game, levelInt, currentTime = None):
        Id.getInstance().resetId()

        self.__mCrypt = Crypt()
        self.mCurrentTime = currentTime

        self.mLevelInt = levelInt
        self.mButtons = []
        self.mTime = Time(self.__readPlayerTime())

        if currentTime != None:
            super(LevelTimeScreen, self).__init__(game, False)
            self.__initializeFromGame()
        else:
            super(LevelTimeScreen, self).__init__(game)
            self.__initializeFromMenu()

        self.medallions = Animation(Resources.getInstance().mMedallions, 3, 3, 0, self.mCamera.getScaledSize(3, 3), False, False)
        self.mLevelTimes = [Time(x) for x in self.__readLevelTimes()]
        self.mButtons.append(Button("back", 0.5, 8.5, b2Vec2(2,1), lambda: self.mGame.setScreen(screen.LevelScreen.LevelScreen(self.mGame))))

        self.achivedMedallion = self.__calculateMedallion()

    def __initializeFromMenu(self):
        self.mButtons.append(Button("play", 13.5, 8.5, b2Vec2(2,1), lambda: self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt))))

    def __initializeFromGame(self):
        if self.mLevelInt < Level.Level.countLevels():
            self.mButtons.append(Button("next", 13.5, 8.5, b2Vec2(2,1), lambda: self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt+1))))
        else:
            self.mButtons.append(Button("end", 13.5, 8.5, b2Vec2(2,1), lambda: self.mGame.setScreen(screen.EndScreen.EndScreen(self.mGame))))

        self.mButtons.append(Button("retry", 11, 8.5, b2Vec2(2,1), lambda: self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt))))

        if self.mCurrentTime.isFaster(self.mTime):
            self.mTime = self.mCurrentTime
            found = False

            data = None
            try:
                with open(Resources.getInstance().resource_path("assets/state/time.json"), "rb") as readstate:
                    decryptedData = self.__mCrypt.decrypt(readstate.read())
                    data = json.loads(decryptedData)

                    for time in data:
                        if time["ID"] == str(self.mLevelInt):
                            found = True
                            time["TIME"] = self.mCurrentTime.toString()

                    if not found:
                        data.append({"ID":"%s" % str(self.mLevelInt), "TIME":"%s" % self.mCurrentTime.toString()})
            except Exception:
                pass

            if data == None:
                data = '[{"ID":"%s", "TIME":"%s"}]' % (str(self.mLevelInt), self.mCurrentTime.toString())

            with open(Resources.getInstance().resource_path("assets/state/time.json"), "wb+") as writestate:
                writestate.write(self.__mCrypt.encrypt(json.dumps(data)))


    def __calculateMedallion(self):
        if self.mTime.isFaster(self.mLevelTimes[0]):
            return b2Vec2(0,0)
        elif self.mTime.isFaster(self.mLevelTimes[1]):
            return b2Vec2(1,0)
        elif self.mTime.isFaster(self.mLevelTimes[2]):
            return b2Vec2(2,0)
        else:
            return b2Vec2(0,2)

    def __readPlayerTime(self):
        pTime = None
        found = False

        try:
            with open(Resources.getInstance().resource_path("assets/state/time.json"), "rb") as state:
                decryptedData = self.__mCrypt.decrypt(state.read())
                pTime = json.loads(decryptedData)

            for time in pTime:
                if time["ID"] == str(self.mLevelInt):
                    pTime = time["TIME"]
                    found = True
                    break
        except Exception:
            with open(Resources.getInstance().resource_path("assets/state/time.json"), "wb+") as writer:
                writer.write(self.__mCrypt.encrypt("[]"))

        if not found:
            pTime = "00:00:00"

        return pTime

    def __readLevelTimes(self):
        times = []
        parser = self.__mCrypt.dectryptParser(self.mLevelInt)
        times.append(parser.get("time", "gold"))
        times.append(parser.get("time", "silver"))
        times.append(parser.get("time", "bronze"))
        return times

    def update(self, delta):
        BaseMenuScreen.update(self, delta)

    def render(self, delta):
        Pgl.app.surface.fill((67,80,129))

        #header
        title = self.titleFont.render("level %s" % str(self.mLevelInt), 0, (255,255,255))
        size = self.titleFont.size("level %s" % str(self.mLevelInt))
        titlepos = self.mCamera.getViewCoords(b2Vec2(self.modelsize.x / 2.0, self.modelsize.y / 6))
        Pgl.app.surface.blit(title, (titlepos.x - size[0] / 2.0, titlepos.y - size[1] / 2.0))

        #current runningtime (if comming from gamescreen)
        if self.mCurrentTime != None:
            current = Resources.getInstance().getScaledFont(self.mCamera.mScale.x / 1.5).render("Current time:", 0, (255, 255, 255))
            currentsize = Resources.getInstance().getScaledFont(self.mCamera.scale.x / 1.5).size("Current time:")
            ctime = Resources.getInstance().getScaledFont(self.mCamera.scale.x / 1.5).render(self.mCurrentTime.toString(), 0, (255,74,20))
            currentpos = self.mCamera.getViewCoords(b2Vec2(5, 3.3))
            ctimepos = self.mCamera.getViewCoords(b2Vec2(5.5, 3.3))
            Pgl.app.surface.blit(current, currentpos)
            Pgl.app.surface.blit(ctime, (ctimepos.x + currentsize[0], ctimepos.y))

        #besttime
        best = Resources.getInstance().getScaledFont(self.mCamera.scale.x / 1.5).render("Best time:", 0, (255, 255, 255))
        bestsize = Resources.getInstance().getScaledFont(self.mCamera.scale.x / 1.5).size("Best time:")
        time = Resources.getInstance().getScaledFont(self.mCamera.scale.x / 1.5).render(self.mTime.toString(), 0, (255,74,20))
        bestpos = self.mCamera.getViewCoords(b2Vec2(5, 3.8))
        timepos = self.mCamera.getViewCoords(b2Vec2(5.5, 3.8))
        Pgl.app.surface.blit(best, bestpos)
        Pgl.app.surface.blit(time, (timepos.x + bestsize[0], timepos.y))

        #buttons
        btnToDraw = self.menubutton
        for btn in self.mButtons:
            viewpos = self.mCamera.getViewCoords(b2Vec2(btn.x, btn.y))
            btnToDraw.setSize(self.mCamera.getScaledSize(btn.size.x, btn.size.y))

            color = None
            if btn.mActive:
                btnToDraw.freeze(1, 0)
                color = (255,255,255)
            else:
                btnToDraw.freeze(0, 0)
                color = (141,60,1)

            btnToDraw.draw(delta, viewpos)

            btntxt = self.screenFont.render(str(btn.mText), 0, color)
            size = self.screenFont.size(str(btn.mText))
            txtpos = self.mCamera.getViewCoords(b2Vec2(btn.x + btn.size.x / 2 - (size[0] / self.mCamera.scale.x) / 2.0, btn.y + btn.size.y / 2 - (size[1] / self.mCamera.scale.y) / 2.0))
            Pgl.app.surface.blit(btntxt, (txtpos.x, txtpos.y))

        #small medallions
        for x in range(len(self.mLevelTimes)):
            self.medallions.freeze(x, 1)

            timetxt = self.infoFont.render(self.mLevelTimes[x].toString(), 0 , (255,255,255))
            pos = self.mCamera.getViewCoords(b2Vec2(9, 5+x))
            Pgl.app.surface.blit(timetxt, (pos.x, pos.y))

            self.medallions.setSize(self.mCamera.getScaledSize(1,1))
            self.medallions.draw(delta, self.mCamera.getViewCoords(b2Vec2(8,4.6+x)))

        self.medallions.freeze(self.achivedMedallion.x, self.achivedMedallion.y)
        self.medallions.setSize(self.mCamera.getScaledSize(3,3))
        self.medallions.draw(delta, self.mCamera.getViewCoords(b2Vec2(4.9,4.6)))
        self.arrow.draw()

    def mouseClick(self, pos):
        xy = self.mCamera.getModelCoords(b2Vec2(pos[0], pos[1]))

        for btn in self.mButtons:
            if btn.rect.collidepoint(xy):
                btn.mAction()

    def mouseOver(self, pos):
        xy = self.mCamera.getModelCoords(b2Vec2(pos[0], pos[1]))

        for btn in self.mButtons:
            btn.mActive = False
            if btn.rect.collidepoint(xy):
                btn.mActive = True

    def keyInput(self, key):
        if key == pygame.K_r:
            self.__quickRetry()
        elif key == pygame.K_SPACE:
            self.__quickGame()

    def __quickRetry(self):
        if self.mCurrentTime != None:
            self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt))

    def __quickGame(self):
        if self.mCurrentTime == None:
            self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt))
        else:
            if self.mLevelInt < Level.Level.countLevels():
                self.mGame.setScreen(screen.GameScreen.GameScreen(self.mGame, self.mLevelInt+1))
            else:
                self.mGame.setScreen(screen.EndScreen.EndScreen(self.mGame))
Exemplo n.º 3
0
class EnemyRender(object):
    
    def __init__(self, camera, enemies):
        self.mCamera = camera
        self.levelUpdate(enemies)
        self.spikebox = Animation(Resources.getInstance().mSpikeBox, 1, 1, 0, self.mCamera.getScaledSize(1,1), False)
        self.spike = Animation(Resources.getInstance().mSpike, 1, 1, 0, self.mCamera.getScaledSize(1,1), False)
        self.laser = Animation(Resources.getInstance().mLasermount, 1, 1, 0, self.mCamera.getScaledSize(1,1), False, False)
        
        
    def render(self, delta):
        
        for e in self.mEnemies:
            if self.mCamera.isInFrustum(e.position.x, e.position.y) or isinstance(e, Laser):
                toDraw = None
                size = b2Vec2(e.size.x, e.size.y)
                       
                if isinstance(e, SpikeBox):
                    toDraw = self.spikebox
                elif isinstance(e, Spike):
                    toDraw = self.spike      
                    
                    if e.mFacing == Facing.UP:
                        toDraw.rotate(0)
                        size.Set(1, 0.5)
                        
                    if e.mFacing == Facing.DOWN:
                        toDraw.rotate(180)
                        size.Set(1, 0.5)
                        
                    if e.mFacing == Facing.LEFT:
                        toDraw.rotate(90)
                        size.Set(0.5, 1)
                                  
                    if e.mFacing == Facing.RIGHT:
                        toDraw.rotate(-90)
                        size.Set(0.5, 1)
                        
                        
                elif isinstance(e, Saw):
                    toDraw = self.mSawAnimations[e.mId]
                
                elif isinstance(e, Laser):
                    toDraw = None
                    spos = self.mCamera.getViewCoords(e.mStartPos)
                    epos = self.mCamera.getViewCoords(e.mEndPos)
                    random.seed(e.mId)
                    offset = 2-math.cos((delta * 1.5))
                                     
                    if e.mWarming:
                        pygame.draw.aaline(Pgl.app.surface, (217,115,118), spos * offset, epos * offset, 1)
                    
                    if e.isActive():
                        pygame.draw.line(Pgl.app.surface, (104,28,31), spos * offset, epos * offset, 7)
                        pygame.draw.line(Pgl.app.surface, (158,29,34), spos * offset, epos * offset, 3)
                        pygame.draw.aaline(Pgl.app.surface, (196,196,196), spos * offset, epos * offset, 1)
                    
                    self.laser.setSize(self.mCamera.getScaledSize(0.5,0.5))
                    
                    rot = e.mStartRot
                    
                    if e.mStartFacing == Facing.RIGHT: rot = 270-rot
                    elif e.mStartFacing == Facing.LEFT: rot = 90-rot
                    
                    self.laser.rotate(rot)
                    self.laser.draw(delta, b2Vec2(spos.x - self.laser.getSize().x / 2.0, spos.y - self.laser.getSize().y / 2.0))
                    self.laser.rotate(rot-180)
                    self.laser.draw(delta, b2Vec2(epos.x - self.laser.getSize().x / 2.0, epos.y - self.laser.getSize().y / 2.0))
                    
                
                if toDraw != None:
                    viewpos = self.mCamera.getViewCoords(b2Vec2(e.position.x - size.x/2.0, e.position.y - size.y/2.0))
                    toDraw.setSize(self.mCamera.getScaledSize(size.x, size.y))
                    toDraw.draw(delta, viewpos)
    
    def levelUpdate(self, enemies):
        self.mSawAnimations = {}
        
        for e in enemies:
            if isinstance(e, Saw):
                self.mSawAnimations[e.mId] = Animation(Resources.getInstance().mSaw, 2, 1, min(0.3/e.speed, 0.2), self.mCamera.getScaledSize(1,1))
        
        self.mEnemies = enemies
                
        
Exemplo n.º 4
0
class PlayerRender(object):
    
    def __init__(self, camera, player):
        self.mCamera = camera
        self.mPlayer = player
        self.playerAnimation = Animation(Resources.getInstance().mPxl, 4, 2, 0.5, self.mCamera.getScaledSize(1,1))
        
    
    def render(self, delta):
        if self.mPlayer.alive:
            size = self.mPlayer.size
             
            viewpos = self.mCamera.getViewCoords(b2Vec2(self.mPlayer.position.x - self.mPlayer.size.x/2.0, self.mPlayer.position.y - self.mPlayer.size.y/2.0))
            self.playerAnimation.setSize(self.mCamera.getScaledSize(size.x, size.y))
            
            if self.mPlayer.mPlayerState == PlayerState.IDLE:
                self.playerAnimation.freeze(0)
            elif self.mPlayer.mPlayerState == PlayerState.FALLING:
                self.playerAnimation.freeze(0, 1)
            else:
                self.playerAnimation.gotoRow(0)
                self.playerAnimation.continueAnimation()
    
            self.__flipAndRotate()
            
            self.playerAnimation.draw(delta, viewpos)
            
                
    def __flipAndRotate(self):
        #Down
        if self.mPlayer.mBodyDirection == GravityDirection.DOWN:
            self.playerAnimation.rotate(0)
            
            if self.mPlayer.mFacing == Facing.RIGHT:
                if self.playerAnimation.flippedX() == True:
                    self.playerAnimation.flipX()
            else:
                if self.playerAnimation.flippedX() == False:
                    self.playerAnimation.flipX()
        
        #Up
        if self.mPlayer.mBodyDirection == GravityDirection.UP:
            self.playerAnimation.rotate(180)
            
            if self.mPlayer.mFacing == Facing.RIGHT:
                if self.playerAnimation.flippedX() == False:
                    self.playerAnimation.flipX()
            else:
                if self.playerAnimation.flippedX() == True:
                    self.playerAnimation.flipX()
                
        #Right
        if self.mPlayer.mBodyDirection == GravityDirection.RIGHT:
            self.playerAnimation.rotate(90)
            
            if self.mPlayer.mFacing == Facing.RIGHT:
                if self.playerAnimation.flippedX() == True:
                    self.playerAnimation.flipX()
            else:
                if self.playerAnimation.flippedX() == False:
                    self.playerAnimation.flipX()
                    
        #Left
        if self.mPlayer.mBodyDirection == GravityDirection.LEFT:
            self.playerAnimation.rotate(270)
            
            if self.mPlayer.mFacing == Facing.RIGHT:
                if self.playerAnimation.flippedX() == True:
                    self.playerAnimation.flipX()
            else:
                if self.playerAnimation.flippedX() == False:
                    self.playerAnimation.flipX()