Beispiel #1
0
class Asteroid():
    def __init__(self, stage, left, top, src, value):
        self.stage = stage
        image = assets.load_image(src)
        self.item = ItemImage(left, top, image)
        self.item.add_event_handler(ItemEvent.CLICK, self.handle_click)
        self.text = ItemText(left,
                             top,
                             stage.font,
                             0,
                             str(value),
                             width=self.item.get_width(),
                             height=self.item.get_height(),
                             h_align=2,
                             v_align=2)
        self.value = value

    def set_left(self, left):
        self.item.set_left(left)
        self.text.set_left(left)

    def set_top(self, top):
        self.item.set_top(top)
        self.text.set_top(top)

    def handle_click(self, item, args):
        self.stage.select(self)

    def exit(self):
        self.item.exit()
        self.item = None
        self.text.exit()
        self.text = None
 def exit(self):
     ItemImage.exit(self)
     self.click_handler = None
     self.sound_click_handler = None
     self.sound_mouse_enter_handler = None
     self.presed_image = None
     self.rollover_image = None
     self.image = None
     self.disabled_image = None
Beispiel #3
0
class Card():
    def __init__(self, stage, key, image, image_back):
        self.stage = stage
        self.key = key
        self.image = image
        self.image_back = image_back
        self.item = ItemImage(0, 0, image_back)
        self.item.add_event_handler(ItemEvent.CLICK, self.handle_click)

    def handle_click(self, item, args):
        self.stage.select(self)

    def select(self):
        self.item.set_image(self.image)

    def unselect(self):
        self.item.set_image(self.image_back)

    def exit(self):
        self.stage = None
        self.image = None
        self.image_back = None
        self.item.exit()
        self.item = None
Beispiel #4
0
class Character():
    class Frame():
        def __init__(self, image, to):
            self.image = image
            self.to = to

        def exit(self):
            self.image = None

    def load_animation(self, base, animation):
        total_time = 0
        frames = []
        for frame in animation:
            total_time += frame.duration * 1000
            frames.append(
                self.Frame(assets.load_image(base + frame.src), total_time))
        return frames

    def __init__(self,
                 left=0,
                 top=0,
                 base=None,
                 standing=None,
                 walking=None,
                 jumping=None):
        self.first_jumping = True
        self.first_walking = True

        # Load the sound
        self.footsteps_concrete_sound = assets.load_sound(
            'DGI_footsteps_concrete.ogg')
        self.jump_sound = assets.load_sound('DGI_jump.ogg')

        self.standing_still = assets.load_image(base + standing.src)
        if "wait" in standing:
            self.standing_wait = Animation(
                self.load_animation(base, [
                    DictClass({
                        'src': standing.src,
                        'duration': standing.duration
                    })
                ] + standing.wait))
        if jumping:
            self.jumping = assets.load_image(base + jumping.src)
        if walking:
            if "sideways" in walking:
                sideways = self.load_animation(base, walking.sideways)
                self.walking_sideways_left = Animation(sideways)
                self.walking_sideways_right = Animation([
                    self.Frame(frame.image.flip_h_copy(), frame.to)
                    for frame in sideways
                ])
            if "front" in walking:
                self.walking_front = Animation(
                    self.load_animation(base, walking.front))
            if "back" in walking:
                self.walking_back = Animation(
                    self.load_animation(base, walking.back))
        self.item = ItemImage(left, top, self.standing_still)

    def exit(self):
        self.item.exit()
        self.standing_wait.exit()
        self.juming = None
        self.jump_sound = None
        self.footsteps_concrete_sound = None
        self.walking_sideways_left.exit()
        self.walking_sideways_left = None
        self.walking_sideways_right.exit()
        self.walking_sideways_right = None
        self.walking_front.exit()
        self.walking_front = None
        self.walking_back.exit()
        self.walking_back = None

    def update(self, delta_time, delta_left=0, delta_top=0, jumping=False):
        if delta_left == 0 and delta_top == 0:
            self.first_jumping = True
            self.first_walking = True

            self.footsteps_concrete_sound.stop()
            self.standing_wait.update(delta_time)
            self.item.set_image(self.standing_wait.get_image())
        else:
            self.standing_wait.reset()
            if jumping:
                self.first_walking = True
                if self.first_jumping:
                    self.footsteps_concrete_sound.stop()
                    self.jump_sound.play()
                    self.first_jumping = False
                self.item.set_image(self.jumping)
            else:
                self.first_jumping = True
                if self.first_walking:
                    self.footsteps_concrete_sound.play(-1)
                    self.first_walking = False
                if delta_left < 0:
                    self.walking_sideways_right.update(delta_time)
                    self.item.set_image(
                        self.walking_sideways_right.get_image())
                    return
                if delta_left > 0:
                    self.walking_sideways_left.update(delta_time)
                    self.item.set_image(self.walking_sideways_left.get_image())
                    return
                if delta_top < 0:
                    self.walking_back.update(delta_time)
                    self.item.set_image(self.walking_back.get_image())
                    return
                self.walking_front.update(delta_time)
                self.item.set_image(self.walking_front.get_image())
Beispiel #5
0
 def exit(self):
     ItemImage.exit(self)
     self.selected_image = None
     self.rollover_image = None
     self.group = None
     self.rollover_image = None
class Invader():
    def __init__(self, left, top, min, max, velocity, good, src, points,
                 collision):
        self.left = left
        self.top = top
        self.min = min
        self.max = max
        self.velocity = velocity
        self.good = good
        image = assets.load_image(src)
        self.item = ItemImage(0, 0, image)
        self.points = points
        self.collision = collision
        if max:
            self.delta = random.choice([-1, 1])
            self.delta = random.choice([-1.5, -1, 1, 1.5])
        else:
            self.delta = 0
        if DEBUG:
            self.debug_item = ItemRect(self.get_left(),
                                       self.get_top(),
                                       self.get_width(),
                                       self.get_height(),
                                       border=(255, 255, 255))

    def set_left(self, left):
        self.min -= left / 2
        self.max += left / 2
        self.item.set_left(left)
        if DEBUG:
            self.debug_item.set_left(left + self.collision.left)

    def exit(self):
        self.item.exit()
        self.item = None
        self.collision = None

    def get_left(self):
        return self.item.get_left() + self.collision.left

    def get_top(self):
        return self.item.get_top() + self.collision.top

    def get_width(self):
        return self.item.get_width(
        ) - self.collision.left - self.collision.right

    def get_height(self):
        return self.item.get_height(
        ) - self.collision.top - self.collision.bottom

    def move(self, time, velocity):
        if self.delta:
            left = self.item.get_left()
            if left + self.delta > self.max or left + self.delta + self.item.get_width(
            ) > SCREEN_WIDTH:
                self.delta = -1
                self.delta = random.choice([-1.5, -1])
            elif left + self.delta < self.min or left + self.delta < 0:
                self.delta = 1
                self.delta = random.choice([1, 1.5])
            left += self.delta * time * velocity * self.velocity
            self.item.set_left(left)
            if DEBUG:
                self.debug_item.set_left(left + self.collision.left)
        top = self.item.get_top() + time * velocity * self.velocity
        self.item.set_top(top)
        if DEBUG:
            self.debug_item.set_top(top + self.collision.top)
class InvadersMinigame(GameStage):
    MOVE_INTERVAL = 30
    GOOD_INDICATOR_INTERVAL = 1000
    DIFFICULTY_INTERVAL = 10000
    GO_BACK_TIME = 3000

    def __init__(self, game):
        GameStage.__init__(self, game, Color('#333333'))

    def initialize(self):
        GameStage.initialize(self)
        stream = file('data/fonts.yaml', 'r')

        fonts = load(stream)
        for font in fonts:
            setattr(
                self, font,
                assets.load_font(fonts[font]['file_name'],
                                 fonts[font]['size']))

        self.data = DictClass(load(file('data/invaders.yaml')))

        self.good = self.data.start.good
        self.velocity = self.data.start.velocity
        self.interval = self.data.start.interval

        self.game_over_layer = Layer()
        image = assets.load_image(
            self.data.bad[self.game.datastore.datamodel.character].src)
        item = ItemImage(self.data.bad.left, self.data.bad.top, image)
        self.game_over_layer.add(item)
        image = assets.load_image(
            self.data.game_over[self.game.datastore.datamodel.character].src)
        item = ItemImage(self.data.game_over.left, self.data.game_over.top,
                         image)
        self.game_over_layer.add(item)

        image = assets.load_image(
            self.data.good[self.game.datastore.datamodel.character].src)
        self.good_indicator = ItemImage(self.data.good.left,
                                        self.data.good.top, image)

        self.text_indicators = []

        self.main_layer = Layer()
        self.top_layer = Layer()

        self.score = 0
        self.score_board = DictClass({})
        image = assets.load_image(self.data.score.src)
        self.score_board['skin'] = ItemImage(self.data.score.left,
                                             self.data.score.top, image)
        self.score_board['value'] = ItemText(self.data.score.left,
                                             self.data.score.top,
                                             self.font,
                                             0,
                                             str(self.score),
                                             width=image.get_width(),
                                             height=image.get_height(),
                                             h_align=2,
                                             v_align=2)
        self.top_layer.add(self.score_board.skin)
        self.top_layer.add(self.score_board.value)

        data = DictClass(load(file('data/map/common.yaml')))
        params = data.character[self.game.datastore.datamodel.character]
        params['base'] = data.character.base[
            self.game.datastore.datamodel.character].big
        self.character_animation = Character(**params)
        self.character = self.character_animation.item
        self.character.set_left(
            (SCREEN_WIDTH - self.character.get_width()) / 2)
        self.character.set_top(SCREEN_HEIGHT - self.character.get_height())
        left = self.character.get_left() + self.data.collision.left
        top = self.character.get_top() + self.data.collision.top
        width = self.character.get_width(
        ) - self.data.collision.left - self.data.collision.right
        height = self.character.get_height(
        ) - self.data.collision.top - self.data.collision.bottom
        if DEBUG:
            self.debug_character = ItemRect(left,
                                            top,
                                            width,
                                            height,
                                            border=(255, 255, 255))
        self.invaders = []

        # Load the sound
        self.item_found_sound = assets.load_sound('DGI_item_found.ogg')
        self.lose_hit_sound = assets.load_sound('DGI_lose_hit.ogg')
        self.lose_music_sound = assets.load_sound('DGI_lose_music.ogg')

    def increase_difficulty(self, key, data):
        if self.good + self.data.step.good >= self.data.min.good:
            self.good += self.data.step.good
        if self.velocity + self.data.step.velocity <= self.data.max.velocity:
            self.velocity += self.data.step.velocity
        if self.interval + self.data.step.interval >= self.data.min.interval:
            self.interval += self.data.step.interval
            self.stop_timer(1)
            self.start_timer(1, self.interval, self.create_invader)

    def create_invader(self, key, data):
        lst = [(True, self.good), (False, 1 - self.good)]
        good = w_choice(lst)
        if good:
            index = random.randint(0, len(self.data.invaders.good) - 1)
            invader = self.data.invaders.good[index]
        else:
            index = random.randint(0, len(self.data.invaders.bad) - 1)
            invader = self.data.invaders.bad[index]
        src = invader.src
        points = invader.points
        collision = invader.collision
        min = invader.min
        max = invader.max
        velocity = invader.velocity
        invader = Invader(0, 0, min, max, velocity, good, src, points,
                          collision)
        left = random.randint(
            0, SCREEN_WIDTH - invader.item.get_image().get_width())
        invader.set_left(left)
        top = -invader.item.get_image().get_height()
        invader.item.set_top(top)
        self.invaders.append(invader)

        self.main_layer.add(invader.item)
        if DEBUG:
            self.main_layer.add(invader.debug_item)

    def move_invaders(self, key, data):
        for item in self.text_indicators:
            item.set_top(item.get_top() - 2)

        for invader in self.invaders:
            invader.move(self.MOVE_INTERVAL, self.velocity)
            if (invader.item.get_top() > SCREEN_HEIGHT):
                self.remove_invader(invader)
            else:
                left = self.character.get_left() + self.data.collision.left
                top = self.character.get_top() + self.data.collision.top
                width = self.character.get_width(
                ) - self.data.collision.left - self.data.collision.right
                height = self.character.get_height(
                ) - self.data.collision.top - self.data.collision.bottom
                character = Rect(left, top, width, height)
                item = Rect(invader.get_left(), invader.get_top(),
                            invader.get_width(), invader.get_height())
                k = character.collidelist([item])
                if k != -1:
                    if invader.good:
                        self.character_animation.footsteps_concrete_sound.stop(
                        )
                        self.item_found_sound.play()
                        self.character_animation.first_walking = True

                        self.score += invader.points
                        self.score_board.value.set_text(str(self.score))
                        self.top_layer.add(self.good_indicator)
                        item = ItemText(invader.get_left(),
                                        invader.get_top(),
                                        self.font,
                                        0,
                                        "+" + str(invader.points),
                                        h_align=2,
                                        v_align=2)
                        self.text_indicators.append(item)
                        self.top_layer.add(item)
                        fade_out_item(item, True, self.GOOD_INDICATOR_INTERVAL)
                        self.start_timer(3, self.GOOD_INDICATOR_INTERVAL,
                                         self.remove_good_indicator)
                    else:
                        self.stop_timer(1)
                        self.stop_timer(2)
                        self.game_over()
                        return
                    self.remove_invader(invader)

    def remove_invader(self, invader):
        self.invaders.remove(invader)
        self.main_layer.remove(invader.item)
        invader.exit()
        if DEBUG:
            self.main_layer.remove(invader.debug_item)

    def remove_good_indicator(self, key, data):
        self.stop_timer(3)
        self.top_layer.remove(self.good_indicator)

    def prepare(self):
        self.show_board()
        self.key = None
        dialog = Intro(self, self.data.intro, self.start_game)
        dialog.start()

    def start_game(self):
        self.start_timer(1, self.interval, self.create_invader)
        self.start_timer(2, self.MOVE_INTERVAL, self.move_invaders)
        self.start_timer(4, self.DIFFICULTY_INTERVAL, self.increase_difficulty)
        self.start_timer(0, 30, self.manage_key)

    def show_board(self):
        self.main_layer.add(self.character)
        if DEBUG:
            self.main_layer.add(self.debug_character)
        self.add_layer(self.main_layer)
        self.add_layer(self.top_layer)

    def handle_event(self, e):
        if e.type == KEYDOWN:
            self.key = e.key
        if e.type == KEYUP:
            self.key = None

    def manage_key(self, key, data):
        delta = 0
        if self.key == K_LEFT:
            delta = -8
        if self.key == K_RIGHT:
            delta = 8
        left = self.character.get_left() + delta
        if 0 <= left and left <= SCREEN_WIDTH - self.character.get_width():
            self.character.set_left(left)
            if DEBUG:
                self.debug_character.set_left(left + self.data.collision.left)
            self.character_animation.update(self.MOVE_INTERVAL,
                                            delta_left=delta)

    def game_over(self):
        self.good_indicator.set_visible(False)
        self.character_animation.footsteps_concrete_sound.stop()
        self.lose_hit_sound.play()
        self.lose_music_sound.play()

        self.stop_timer(0)
        self.stop_timer(1)
        self.stop_timer(2)
        self.stop_timer(3)
        self.stop_timer(4)
        self.top_layer.remove(self.good_indicator)
        self.add_layer(self.game_over_layer)
        self.start_timer("go_back_timer", self.GO_BACK_TIME, self.go_back)

    def exit(self, other_stage):
        GameStage.exit(self, other_stage)
        self.good_indicator.exit()
        self.good_indicator = None
        self.character_animation.exit()
        self.character_animation = None
        for invader in self.invaders:
            invader.exit()
        self.invaders = None

    def go_back(self, *args, **kwargs):
        from game.stages.map import Map
        self.game.set_stage(Map(self.game))