Пример #1
0
class Effect(object):
    """Effects are sounds effect loaded in memory

    Example ::

        shot = Effect('bullet.wav')
        shot.play() # Play right now
        sprite.do(shot.action)
    """

    def __init__(self, filename):
        """Initialize the effect

        :Parameters:
            `filename` : fullpath
                path of a WAV or Ogg audio file
        """
        if audio._working:
            self.sound = Sound(filename)
        else:
            self.sound = None
        self.action = actions.PlayAction(self.sound)

    def play(self):
        if audio._working:
            self.sound.play()
Пример #2
0
class Effect(object):
    """Effects are sounds effect loaded in memory

    Example ::

        shot = Effect('bullet.wav')
        shot.play() # Play right now
        sprite.do(shot.action)
    """
    def __init__(self, filename):
        """Initialize the effect

        :Parameters:
            `filename` : fullpath
                path of a WAV or Ogg audio file
        """
        if audio._working:
            self.sound = Sound(filename)
        else:
            self.sound = None
        self.action = actions.PlayAction(self.sound)

    def play(self):
        if audio._working:
            self.sound.play()
Пример #3
0
Файл: llama.py Проект: mre/llama
class Game(ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(Game, self).__init__(0, 80, 125, 0)

        self.block_pic = "assets/img/heart.png"
        for pos in [(100, 100), (540, 380), (540, 100), (100, 380)]:
            self.add(Block(pos[0], pos[1], self.block_pic))
        self.lama = Lama(WIDTH / 2, 100)
        self.add(self.lama)

        cell = self.lama.width * 1.25
        self.collman = cm.CollisionManagerGrid(0, 640, 0, 480, cell, cell)

        self.pressed = defaultdict(int)
        self.schedule(self.update)

        self.kiss_sound = Sound("assets/sound/kiss.ogg")
        self.jump_sound = Sound("assets/sound/jump.ogg")

    def on_key_press(self, k, m):
        self.pressed[k] = 1

    def on_key_release(self, k, m):
        self.pressed[k] = 0

    def update(self, dt):
        self.collman.clear()
        for _, node in self.children:
            self.collman.add(node)
        for other in self.collman.iter_colliding(self.lama):
            self.remove(other)
            self.lama.powerup()
            self.kiss_sound.play()

        if len(self.children) <= 1:
            self.show_finish_screen()

        dx = (self.pressed[key.RIGHT] - self.pressed[key.LEFT]) * 250 * dt
        dy = 0
        if self.lama.on_ground():
            if self.pressed[key.SPACE]:
                self.jump_sound.play()
                dy = 150
        else:
            dy -= 300 * dt
        self.lama.move(dx, dy)

    def show_finish_screen(self):
        background = Final()
        director.replace(Scene(background))
Пример #4
0
class Audiolayer(Layer):
    def __init__(self):
        mixer.init()
        super(Audiolayer, self).__init__()
        self.path = os.getcwd()
        os.chdir(self.path[0:-4])
        self.sound = Sound('audio/test.ogg')
        self.sound.play(-1)

    def on_exit(self):
        os.chdir(self.path)
        mixer.pause()

    def on_enter(self):
        mixer.unpause()
class CocosView(object):

    DIALOGUE, GET_CHOICE, WAIT, WAIT_INPUT, IDLE = range(5)
    WIDTH = 800
    HEIGHT = 600
    TITLE = "NMFe Standalone Player"
    ACTOR_FONT_SIZE = 20
    DIALOGUE_FONT_SIZE = 16
    ADVANCE_NEXT_KEY = "n"
    FONT = "Sans Serif"
    DIALOGUE_DELAY = 0.035

    def __init__(self):
        self._actor_label = None
        self._dialogue_box = None
        self._scene = None
        self._audio = None
        self._background = None
        self._ui_layer = None
        self._image_layer = None
        self._menu = None
        self._choice = None
        self._delete_queue = []
        self._display_queue = []
        self._dialogue_dirty = False
        self._dialogue_buffer = []
        self._avatars = {}
        self._speaker = None
        self._state = CocosView.IDLE
        self._start_time = -1
        self._duration = -1
        self._update_speaker = False

    def display_dialogue(self, dialogue):
        self._state = CocosView.DIALOGUE
        self._dialogue_dirty = True
        self._dialogue_buffer = list(dialogue)

    def set_speaker(self, speaker):
        self._speaker = speaker
        self._update_speaker = True

    def get_speaker(self):
        return self._speaker

    def display_background(self, background_path, transition):
        if self._background is not None:
            self._delete_queue.append(self._background)
        image = pyglet.image.load(background_path)
        self._background = cocos.sprite.Sprite(image)
        self._background.position = (CocosView.WIDTH / 2, CocosView.HEIGHT / 2)
        self._display_queue.append((self._background, -1))

    def clear_background(self):
        self._delete_queue.append(self._background)
        self._background = None

    def play_audio(self, audio_path):
        if self._audio is not None:
            self.stop_audio()
        self._audio = Sound(audio_path)
        self._audio.play(-1)

    def stop_audio(self):
        if self._audio is not None:
            self._audio.stop()
            self._audio = None

    def display_avatar(self, avatar_path):
        old_position = None
        already_displaying = False
        if self._avatars.has_key(self.get_speaker()):
            already_displaying = True
            current_avatar = self._avatars[self.get_speaker()]
            old_position = current_avatar.position
            self.remove_avatar(self.get_speaker())
        image = pyglet.image.load(avatar_path)
        sprite = cocos.sprite.Sprite(image)
        self._avatars[self.get_speaker()] = sprite
        if old_position:
            old_x = old_position[0]
            if old_x < CocosView.WIDTH / 2:
                self.set_avatar_position(self.get_speaker(), "left")
            elif old_x > CocosView.WIDTH / 2:
                self.set_avatar_position(self.get_speaker(), "right")
            else:
                self.set_avatar_position(self.get_speaker(), "center")
        else:
            sprite.position = (CocosView.WIDTH / 2, sprite.height / 2)
            if already_displaying:
                self._display_queue.append((sprite, 0))

    def remove_avatar(self, avatar):
        self._delete_queue.append(self._avatars[avatar])
        del self._avatars[avatar]

    def set_avatar_position(self, avatar, position):
        sprite = self._avatars[avatar]
        if position == "left":
            sprite.position = (sprite.width / 2, sprite.height / 2)
        elif position == "center":
            sprite.position = (CocosView.WIDTH / 2, sprite.height / 2)
        elif position == "right":
            sprite.position = (CocosView.WIDTH - sprite.width / 2, sprite.height / 2)
        if sprite not in self._image_layer:
            self._display_queue.append((sprite, 0))

    def wait(self, duration):
        self._check_delete_queue()
        self._duration = duration
        self._start_time = time.time()
        self._state = CocosView.WAIT

    def restore_context(self, commands):
        self._check_delete_queue()
        self.clear_background()
        self.stop_audio()
        for command in commands:
            command.execute()

    def display_choices(self, choices):
        self._state = CocosView.GET_CHOICE
        self._refresh_menu()
        choice_items = []
        for index, text in enumerate(choices):
            # counting in NMF starts at one, not zero, so we have to add one
            # to make up the difference
            callback = lambda: self._set_choice(index + 1)
            choice_items.append(cocos.menu.MenuItem(text, callback))
        self._menu.create_menu(choice_items)
        self._show_menu()

    def get_selected_choice(self):
        return self._choice

    def mainloop(self, runtime):
        mixer.init()
        director.init(width=CocosView.WIDTH, height=CocosView.HEIGHT, caption=CocosView.TITLE)
        self._runtime = runtime
        self._init_interface()
        self._scene.schedule(self._update)
        self._scene.schedule_interval(self._render_dialogue, CocosView.DIALOGUE_DELAY)
        director.run(self._scene)

    def _update(self, _):
        if self._runtime.can_step() is False and self._state is CocosView.IDLE:
            pyglet.app.exit()
        elif self._state is CocosView.IDLE:
            command = self._runtime.step()
            command.execute()
        elif self._state is CocosView.WAIT:
            current_time = time.time()
            if current_time - self._start_time >= self._duration:
                self._state = CocosView.IDLE

    def _render_dialogue(self, _):
        if self._dialogue_dirty:
            self._dialogue_dirty = False
            self._dialogue_box.element.text = ""
        if self._state is CocosView.DIALOGUE:
            self._check_delete_queue()
            self._check_display_queue()
            if len(self._dialogue_buffer) == 0:
                self._state = CocosView.WAIT_INPUT
            else:
                self._dialogue_box.element.text += self._dialogue_buffer.pop(0)
            if self._update_speaker:
                if self._speaker is None:
                    self._actor_label.element.text = ""
                else:
                    self._actor_label.element.text = self._speaker
                    self._update_speaker = False

    def _advance_request(self):
        if self._state is not CocosView.GET_CHOICE:
            if len(self._dialogue_buffer) > 0:
                self._dialogue_box.element.text += ''.join(self._dialogue_buffer)
                self._dialogue_buffer = []
            else:
                self._state = CocosView.IDLE

    def _check_delete_queue(self):
        while self._delete_queue:
            sprite = self._delete_queue.pop(0)
            sprite.kill()

    def _check_display_queue(self):
        while self._display_queue:
            sprite, z_value = self._display_queue.pop(0)
            self._image_layer.add(sprite, z=z_value)

    def _refresh_menu(self):
        if self._menu is not None:
            self._menu.kill()
        self._menu = cocos.menu.Menu()
        self._menu.font_item["font_size"] = 16
        self._menu.font_item_selected["font_size"] = 24

    def _hide_menu(self):
        if self._menu in self._scene:
            self._menu.kill()

    def _show_menu(self):
        self._scene.add(self._menu)

    def _set_choice(self, choice_index):
        self._choice = choice_index
        self._hide_menu()
        self._state = CocosView.IDLE

    def _init_interface(self):
        self._scene = cocos.scene.Scene()
        self._image_layer = cocos.layer.Layer()
        self._scene.add(self._image_layer)
        self._ui_layer = CocosUILayer()
        self._ui_layer.advance_request = self._advance_request
        self._actor_label = cocos.text.Label(font_name=CocosView.FONT,
                                            font_size=CocosView.ACTOR_FONT_SIZE,
                                            anchor_x="left",
                                            color=(100, 100, 150, 255))
        self._actor_label.position = (20, 120)
        self._ui_layer.add(self._actor_label)
        self._dialogue_box = cocos.text.Label(font_name=CocosView.FONT,
                                             font_size=CocosView.DIALOGUE_FONT_SIZE,
                                             anchor_x="left",
                                             color=(50, 50, 50, 255),
                                             width=CocosView.WIDTH - 100,
                                             multiline=True)
        self._dialogue_box.position = (20, 80)
        self._ui_layer.add(self._dialogue_box)
        self._scene.add(self._ui_layer)
Пример #6
0
class TBoard(cocos.layer.Layer):
    is_event_handler = True

    def __init__(self):

        super(TBoard, self).__init__()
        self.schedule(self.step)
        self.DM = Donut_M()
        self.sprites_list = dict()
        self.points = 0
        self.running_sprites = []
        self.label = self.update_score()
        self.draw_initiation()
        self.good_song = Sound("sounds/good.wav")
        self.bad_song = Sound("sounds/bad.wav")
        #x = cocos.text.Label('Wynik: 0', (100, 650), color = (255, 255, 255, 255), font_size = 20)
        #self.add(x, z=1)

        #Label('whatever', (200, 200))
        #self.czarownica()
        #random_sprite = self.sprites_list[random.choice(list(self.sprites_list.keys()))]
        #print(random_sprite)
        #self.move_sprite(random_sprite)

    def update_score(self):
        text = 'Wynik: %s' % (self.points)
        x = cocos.text.Label(text, (100, 650),
                             color=(255, 255, 255, 255),
                             font_size=20)
        self.add(x, z=1)
        return x

    def add_gif(self):
        path = 'images/source.gif'
        sprite = cocos.sprite.Sprite(pyglet.image.load_animation(path))
        sprite.scale = 0.05 * 4  #10 pikseli * cos
        sprite.position = 100, 100
        self.add(sprite, z=1)

    def runAnimationInLoop(self):
        sched = Scheduler()
        sched.start()
        sched.add_interval_job(self.moveby_sprite, seconds=10)
        sched.shutdown()

    def moveby_sprite(self, spriteobj):
        #print(spriteobj)
        #self.DM.change_donut_pos
        #while True:
        x, y = spriteobj.position
        self.move_to_kill(spriteobj, x, y)
        #x, y = spriteobj.position
        #new_x, new_y = self.DM.change_donut_pos((x, y))
        #delta_x = new_x - x
        #delta_y = new_y - y
        #spriteobj.do(MoveBy((delta_x, delta_y), 10))
        #self.add(spriteobj)
        #self.poprawa()


#        stan = True
#        while stan:
#            stan = new_x - x <delta_x
#self.moveby_sprite(spriteobj)

#print('czeka')
#print('dojechalem')

#threading.Timer(11.0, self.moveby_sprite).start()

#def poprawa(self, spriteobj):
#if
#self.moveby_sprite(spriteobj)
#spriteobj.stop()
#spriteobj.do(MoveBy((delta_x, delta_y), 20))
#        odl = np.sqrt(delta_x**2 + delta_y**2)
#        t = odl*0.08*10
#        #d_x, d_y = 0, 0
#        d_x = delta_x/int(t)
#        d_y = delta_y/int(t)
#        for i in range(int(t)):
#            #spriteobj.position = d_x, y
#            spriteobj.do(MoveBy((d_x, d_y), 1/(10*int(t))))
#            self.add(spriteobj)

#spriteobj.do(MoveBy((x, y), 5))
#self.add(spriteobj)
#pass

    def czarownica(self):
        for spriteobj in self.running_sprites:
            self.moveby_sprite(spriteobj)

    def move_to_kill(self, spriteobj, ax=1, ay=1):
        work = MoveBy((ax, ay), 1)
        for i in range(120):
            #x, y = spriteobj.position
            #new_x, new_y = self.DM.change_donut_pos((x, y))
            #print('moved to kill', new_x, new_y)
            #print('byl', x, y)
            #dx = new_x - x
            #dy = new_y - y
            dx = random.choice(range(100, 1300))
            dy = random.choice(range(100, 600))
            run_time = random.randint(5, 10)
            work += MoveTo((dx, dy), run_time)
            #spriteobj.rotate(90)
        spriteobj.do(work)
        self.add(spriteobj)

        #self.moveby_sprite(spriteobj)

    def draw_initiation(self):
        for donut_coordinates in self.DM.full_grid_list:
            self.draw_donut(donut_coordinates)

    def draw_donut(self, donut_coordinates):
        #random picture
        picture_number = random.randint(1, 6)
        center_x, center_y = donut_coordinates
        #path = 'images/p%s.png' % (picture_number)
        path = 'images/mysz.gif'
        path = pyglet.image.load_animation(path)
        sprite = cocos.sprite.Sprite(path)
        sprite.scale = 1.1  #10 pikseli * cos
        sprite.position = center_x, center_y
        self.add(sprite, z=1)
        my_name = 'p%s' % (picture_number)
        print(my_name, 'position at', center_x, center_y)
        dict_key = 'a(%s,%s)' % (center_x, center_y)
        self.sprites_list[dict_key] = sprite
        self.running_sprites += [sprite]
        self.moveby_sprite(sprite)
        return sprite
        #self.moveby_sprite(sprite)

    def step(self, dt):
        pass

    def onClicked(self):

        hitted = self.DM.check_click(self.posx, self.posy,
                                     self.running_sprites)
        #print('hitted', hitted)
        if hitted:
            print('trafiono')
            self.good_song.play()
            #center_x, center_y = hitted[1]
            #dict_key = 'a(%s,%s)' %(center_x, center_y)
            #print('gdzie', dict_key)
            #to_kill = self.sprites_list[dict_key]
            #print('kill', to_kill)
            #self.remove(self.sprites_list[dict_key])
            #self.draw_donut(hitted[0])
            #spriteobj.stop()
            #self.remove(hitted[1])
            self.points += 1
            print(self.points)
            new_donut = self.draw_donut(hitted[0])
            x, y = new_donut.position
            self.moveby_sprite(new_donut)  #, x, y)#hitted[1], x, y)
            print(self.label)
            self.remove(self.label)
            self.label = self.update_score()

            #self.move_to_kill(hitted[1])
        else:
            print('pudlo')
            self.bad_song.play()
        #self.czarownica()

    def on_mouse_press(self, x, y, buttons, modifiers):
        try:
            self.posx, self.posy = cocos.director.director.get_virtual_coordinates(
                x, y)

            self.onClicked()

        except Exception as e:
            print(e)