Beispiel #1
0
    def refresh(self):
        if resManager.getVar("SOUND") == True:
            strtmp = "Sound: ON"
        else:
            resManager.setVar("SOUND", False)
            strtmp = "Sound: OFF"

        self.listbox.getByIdx(1).setText(strtmp)
 def setShootDelay(self, seconds=None):
     if seconds != None:
         if seconds == 0:
             self.shootDelay = 0
             self.enabled = False
         else:
             self.shootDelay = ceil(seconds * resManager.getVar("FPS"))
             
     self.tickCount = self.shootDelay
Beispiel #3
0
 def update(self):
     if self.listbox.peek():
         signal = self.listbox.poll()
         if signal == "l":
             return loadstate.LoadState
         elif signal == "s":
             resManager.setVar("SOUND", not resManager.getVar("SOUND"))
             self.refresh()
         elif signal == "e":
             resManager.setVar("FINISHED", True)
Beispiel #4
0
    def __init__(self, root, freely=True):
        self.root = root
        self.viewport = resManager.getVar("SIZE")
        self.half = (self.viewport[0] // 2, self.viewport[1] // 2)
        self.limit = (max(
            self.root.tilesize[0] * self.root.size[0] - self.viewport[0], 0),
                      max(
                          self.root.tilesize[1] * self.root.size[1] -
                          self.viewport[1], 0))

        self.setAt([0, 0])
        self.tracking = None
        self.setFreely(freely)
Beispiel #5
0
    def center(self,
               horizontally=True,
               vertically=False,
               xOffset=0,
               yOffset=0):
        viewport = resManager.getVar("SIZE")
        size = self.getSize()
        newPos = self.pos
        if horizontally:
            newPos[0] = viewport[0] / 2 - size[0] / 2 + xOffset
        if vertically:
            newPos[1] = viewport[1] / 2 - size[1] / 2 + yOffset

        self.setPos(newPos)
Beispiel #6
0
    def create(self):
        self.ctx = pygame.display.get_surface()

        mapFile = resManager.getVar("MAPFILE")

        request = resManager.loadJSONFile(mapFile)
        if request[0] == resManager.DIE:
            resManager.setVar("ERROR_INFO", "unable to load map file")
            return errorstate.ErrorState

        data = request[1]

        try:
            self.world = world.World(data["world"]["map"],
                                     data["world"]["background"],
                                     data["world"]["gravity"])

            if self.world.getBlockAt(
                    data["player"]
                ["position"]) in world.WorldBlock.GroupContainers.SOLIDS:
                raise Exception  #Error, player can't be inside a solid block

            realPosition = [
                data["player"]["position"][0] * self.world.tilesize[0],
                data["player"]["position"][1] * self.world.tilesize[1]
            ]
            self.player = player.Player(
                self.world, realPosition,
                (data["player"]["speed"], data["player"]["jumpspeed"]),
                data["player"]["lifes"], data["player"]["maxLifes"],
                data["player"]["maxScore"])

            self.enemyFactory = enemyfactory.EnemyFactory(
                self.world, self.player)
            self.enemyFactory.createEnemies(data["enemies"])

            self.world.create()
            self.world.camera.setFreely(data["camera"]["freely"])
            self.world.camera.track(self.player)
        except Exception:
            resManager.setVar(
                "ERROR_INFO",
                "invalid map file")  #Oops, we've got trouble loading this
            return errorstate.ErrorState

        self.quit = False
Beispiel #7
0
    def create(self):
        self.ctx = pygame.display.get_surface()
        self.ctx.fill((255, 50, 50))

        font = resManager.loadSystemFont(None, 30)
        self.texts = [
            Text("Ooops! Something gone wrong (but don't worry!)", font,
                 [0, 150]),
            Text("The error says: {}".format(resManager.getVar("ERROR_INFO")),
                 font, [0, 190]),
            Text("Press any key to return", font, [0, 230])
        ]
        for text in self.texts:
            text.center()
            text.draw(self.ctx)

        pygame.display.update()
        self.restart = False
Beispiel #8
0
def main():
    #Global constants
    resManager.setVar("SIZE", (720, 480))
    resManager.setVar("FPS", 60)
    resManager.setVar("TITLE", "PySquarePlatform")
    resManager.setVar("SOUND", True)
    resManager.setVar("FINISHED", False)

    pygame.init()
    pygame.display.set_mode(resManager.getVar("SIZE"), pygame.DOUBLEBUF)
    ctx = pygame.display.get_surface()
    pygame.display.set_caption(resManager.getVar("TITLE"))
    pygame.mixer.set_num_channels(64)

    icon = pygame.Surface((32, 32))
    icon.fill(pygame.Color(0, 0, 0))
    pygame.display.set_icon(icon)

    #Load data here
    resManager.loadImgs([
        ("bg-default", "img/bg-default.png"),
        ("bg-dark", "img/bg-dark.png"),
        ("coin", "img/coin.png"),
        ("hud-heart", "img/hud-heart.png"),
        ("hud-coin", "img/hud-coin.png"),
        ("ground-spikes", "img/ground-spikes.png"),
        ("flag", "img/flag.png")
    ])
    resManager.loadSounds([
        ("jump", "sound/jump.ogg"),
        ("break", "sound/break.ogg"),
        ("listbox_move", "sound/listbox_move.ogg"),
        ("listbox_signal", "sound/listbox_signal.ogg")
    ])
    resManager.createSyncAnimation("coin", resManager.getImg("coin"), 20, 24, ticksPerFrame=5, colorKey=(255, 255, 255))
    resManager.createImgSurface("ground-spikes", resManager.getImg("ground-spikes"), colorKey=(255, 255, 255))
    resManager.createImgSurface("flag", resManager.getImg("flag"), colorKey=(0, 255, 0))
    
    state = changeState(menustate.MenuState)
    
    clock = pygame.time.Clock()
    FPS = resManager.getVar("FPS")

    while not resManager.getVar("FINISHED"):
        while pygame.event.peek():
            e = pygame.event.poll()
            state.handle(e)
            if e.type == pygame.QUIT:
                resManager.setVar("FINISHED", True)

        resManager.updateSyncAnimations()
        newStateClass = state.update()
        if newStateClass == None:
            state.draw()
        else:
            state.destroy()
            state = changeState(newStateClass)
            
        clock.tick(FPS)

    pygame.quit()