예제 #1
0
    def update(self, entity, interval):
#        distx = Globals.PLAYER.posx - entity.posx
#        disty = Globals.PLAYER.posy - entity.posy
#        dist = math.sqrt(distx * distx + disty * disty)
#        if dist <= 50:
        if not Globals.PLAYER is None and\
                pygame.sprite.collide_rect(entity, Globals.PLAYER):
            entity.kill()
            Globals.MINI_SUNS = Globals.MINI_SUNS + 1
            Globals.MINI_SUNS_INLVL = Globals.MINI_SUNS_INLVL + 1
            Globals.SCORE = Globals.SCORE + 1000
            sunID = Globals.sunPos()[(entity.rect.x, entity.rect.y)]
            Globals.sunTr()[sunID] = False
예제 #2
0
def loadMap(cLevel):
    Globals.initSunTracker()

    global key
    global tiles
    global tileSize
    global playerPos
    global tileBuckets
    global world_width

    mapFile = "img/" + cLevel + "/map"
    keyFile = "img/" + cLevel + "/key"

    groups["tiles"].empty()
    groups["tilesObs"].empty()
    groups["spawners"].empty()

    lresources = []
    for resource in ImageManager.levelRes:
        lresources.append(resource)
    for resource in lresources:
        if resource == "one" or resource == "two" or\
                resource == "three" or resource == "four":
            ImageManager.unloadSet(resource)

    ImageManager.loadSet(cLevel)

    key = []
    tiles = {}
    tileSize = 0
    k = open(keyFile)
    y = 0
    x = 0
    for line in k:
        if y == 0:
            iString = ""
            for c in line:
                if c != '\n':
                    iString = iString + c
            tileSize = int(iString)
        else:
            key.append(line)
            temp = 0
            for c in line:
                temp += 1
            if temp > x:
                x = temp
        y += 1
    k.close()

    tileSheet = ImageManager.levelRes[cLevel]["tiles.png"]
    for j in range(y - 1):
        for i in range(x - 1):
            tiles[(i, j)] = tileSheet.subsurface(
                pygame.Rect(i * tileSize, j * tileSize, tileSize, tileSize))

    f = open(mapFile)

    y = 0
    sunID = 0
    for line in f:
        x = 0
        for c in line:
            if c == 'p':
                playerPos = (x * tileSize, y * tileSize)
            elif c == '*':
                Globals.WIN_POS = x * tileSize
            elif c == '-':  # monster 0
                makeMonsterSpawner(x * tileSize, y * tileSize, 0, cLevel)
            elif c == '_':  # monster 1
                makeMonsterSpawner(x * tileSize, y * tileSize, 1, cLevel)
            elif c == '=':  # monster 2
                makeMonsterSpawner(x * tileSize, y * tileSize, 2, cLevel)
            elif c == '+':  # monster 3
                makeMonsterSpawner(x * tileSize, y * tileSize, 3, cLevel)
            elif c == '~':  # sun
                if Globals.SUN_TRACKER[Globals.CURRENT_LEVEL][sunID]:
                    sun = Entity.Entity()
                    sun.componentList.append(CSun.CSun())
                    sun.image = ImageManager.levelRes["effects"]["sun.png"]
                    sun.rect = sun.image.get_rect()
                    sun.rect.x = x * tileSize
                    sun.rect.y = y * tileSize
                    sun.posx = sun.rect.x
                    sun.posy = sun.rect.y
                    groups["enemies"].add(sun)
                    Globals.sunPos()[(sun.rect.x, sun.rect.y)] = sunID
                sunID += 1
            elif c != '\n' and c != ' ':
                block = Entity.Entity()
                block.image = tiles[findKeyPos(c)]
                block.rect = block.image.get_rect()
                block.rect.left = x * tileSize
                block.rect.top = y * tileSize
                if (c >= 'A' and c <= 'Z') or\
                        (c >= '0' and c <= '9'):
                    groups["tilesObs"].add(block)
                else:
                    groups["tiles"].add(block)
            x += 1
        y += 1
    Globals.BOTTOM = y * tileSize
#    block = Entity.Entity()
#    block.image = pygame.Surface((30, 600))
#    block.rect = pygame.Rect((-30, 600, 30, 600))
#    groups["tilesObs"].add(block)

#    entityBuckets = []
    tileBuckets = []
    xmax = (x) * tileSize + cell_size
    ymax = (y) * tileSize + cell_size
    world_width = xmax
    for y in range(ymax / cell_size):
        for x in range(xmax / cell_size):
            tileBuckets.append(Bucket.Bucket(x * cell_size, y * cell_size))
#            entityBuckets.append(Bucket.Bucket(x * cell_size, y * cell_size))

    for tE in groups["tilesObs"]:
        tileBuckets[hashpos(tE.rect.x, tE.rect.y)].add(tE)
        tileBuckets[hashpos(tE.rect.x + tE.rect.width, tE.rect.y)].add(tE)
        tileBuckets[hashpos(tE.rect.x, tE.rect.y + tE.rect.height)].add(tE)
        tileBuckets[hashpos(
            tE.rect.x + tE.rect.width, tE.rect.y + tE.rect.height)].add(tE)