コード例 #1
0
ファイル: 9ntd.py プロジェクト: offmeplz/9game
    def __init__(self, level):
        enters =[(0, i) for i in xrange(GAME_Y_SIZE)]
        exits = [(GAME_X_SIZE - 1, i) for i in xrange(GAME_Y_SIZE)]
        self.field = Field(GAME_X_SIZE, GAME_Y_SIZE, enters, exits)

        self.creeps = pygame.sprite.Group()
        self.towers = pygame.sprite.Group()
        self.missles = pygame.sprite.Group()
        self.corpse = pygame.sprite.Group()
        self.messages = pygame.sprite.Group()
        self.ticks_elapsed = 0
        self.sell_factor = level.SELL_FACTOR

        self.curwaves = set()
        self.creepwave = Starter(
                level.CREEP_WAVES,
                self.start_wave, start=False)

        self.money = level.INIT_MONEY
        self.lives = level.LIVES
        self.wave_index = 0
コード例 #2
0
ファイル: 9ntd.py プロジェクト: offmeplz/9game
class World(object):
    def __init__(self, level):
        enters =[(0, i) for i in xrange(GAME_Y_SIZE)]
        exits = [(GAME_X_SIZE - 1, i) for i in xrange(GAME_Y_SIZE)]
        self.field = Field(GAME_X_SIZE, GAME_Y_SIZE, enters, exits)

        self.creeps = pygame.sprite.Group()
        self.towers = pygame.sprite.Group()
        self.missles = pygame.sprite.Group()
        self.corpse = pygame.sprite.Group()
        self.messages = pygame.sprite.Group()
        self.ticks_elapsed = 0
        self.sell_factor = level.SELL_FACTOR

        self.curwaves = set()
        self.creepwave = Starter(
                level.CREEP_WAVES,
                self.start_wave, start=False)

        self.money = level.INIT_MONEY
        self.lives = level.LIVES
        self.wave_index = 0

    def start_wave(self, wavedict):
        wave = Starter(wavedict['creeps'], self.spawn_creep)
        self.curwaves.add(wave)
        self.wave_index += 1

    def release_creeps(self):
        self.creepwave.start()

    def creeps_released(self):
        return self.creepwave.started

    def updatewaves(self, ticks):
        fordelete = []
        for wave in self.curwaves:
            if not wave.alive():
                fordelete.append(wave)
            else:
                wave.update(ticks)
        for wave in fordelete:
            self.curwaves.remove(wave)

    def add_creep(self, creep):
        creep.add([self.creeps])

    def get_money(self):
        return self.money

    def get_lives(self):
        return self.lives

    def sell_tower(self, tower):
        sizes = (tower.size, tower.size)
        topleft = util.placeintrect(tower.g_pos, sizes)
        cells = util.iterpoints(topleft, sizes)
        self.field.clearon(*cells)

        money = int(self.sell_factor * tower.cost)
        message = '+%d' % money
        self.money += money
        self.messages.add(Message(message, 1, tower.g_pos, GOLD_COLOR))

        tower.kill()

    def build_tower(self, tower_cls, pos):
        if tower_cls.cost > self.get_money():
            raise BuildError, "Not enough money"
        sizes = (tower_cls.size, tower_cls.size)
        topleft = util.placeintrect(pos, sizes)
        canbuild = all(
                self.field.canbuildon(p)
                for p in util.iterpoints(topleft, sizes))
        
        if canbuild:
            self.add_tower(topleft, tower_cls)
        else:
            raise BuildError, "Can't build here"

    def add_tower(self, pos, cls):
        tower = cls(pos, self)

        if pygame.sprite.spritecollideany(tower, self.creeps):
            raise BuildError, "Can't build on creeps"
        
        buildcells = list(util.iterpoints(pos, (cls.size, cls.size)))
        self.field.buildon(*buildcells)

        # check if we block creeps
        block_creeps = False
        for creep in self.creeps:
            if tuple(creep.current_cell()) not in self.field.dir_field:
                block_creeps = True
                break
        for e in self.field._enters:
            if e not in self.field.dir_field:
                block_creeps = True
                break
        if block_creeps:
            self.field.clearon(*buildcells)
            raise BuildError, "Blocking"

        tower.add([self.towers])
        for creep in self.creeps:
            creep.forget_way()
        self.money -= tower.cost

    def update(self, ticks):
        self.ticks_elapsed += ticks
        self.creepwave.update(ticks)
        self.updatewaves(ticks)

        self.towers.update(ticks)
        self.creeps.update(ticks)
        self.missles.update(ticks)
        self.messages.update(ticks)

    def draw(self, surface):
        self.creeps.draw(surface)
        self.missles.draw(surface)

    def spawn_creep(self, creep=None):
        if creep is None:
            creep = Creep(health=3, speed=2)
        creep_pos = random.choice(list(self.field._enters))
        creep.place(creep_pos, self.field, self.oncreepexit, self.oncreepdeath)
        self.add_creep(creep)

    def oncreepexit(self, creep):
        self.lives -= 1
        self.messages.add(Message('-1', 1, creep.g_pos, LIFE_COLOR))

    def oncreepdeath(self, creep):
        self.money += creep.money
        self.corpse.add(Blood(creep))
        message = '+%d' % creep.money
        self.messages.add(Message(message, 1, creep.g_pos, GOLD_COLOR))