コード例 #1
0
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)

        self.logo = cocos.sprite.Sprite('crossin-logo.png')
        self.logo.position = 550, 400
        self.add(self.logo, 99999)

        # init voice
        self.NUM_SAMPLES = 1000  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX()
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        for i in range(100):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=self.NUM_SAMPLES)

        self.schedule(self.update)
コード例 #2
0
ファイル: game.py プロジェクト: zybing/ppx
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)
        # init voice
        self.NUM_SAMPLES = 1000
        par = {"image": "black.png"}
        self.voicebar = Sprite(**par)
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)
        self.label = cocos.text.RichLabel('3 LIVES',
                                          font_name='Times New Roman',
                                          font_size=32,
                                          anchor_x='center',
                                          anchor_y='center',
                                          color=(255, 0, 0, 255))

        self.label.position = 330, 400

        self.add(self.label)

        self.ppx = PPX()
        self.add(self.ppx)

        # layer order according the layer added order
        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        self.maxx = 0
        for i in range(30):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height
            if i == 29:
                self.maxx = b.x

        # open sound input
        pa = PyAudio()
        sampling_rate = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=sampling_rate,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)

        self.schedule(self.update)
コード例 #3
0
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, WIDTH, HEIGHT)
        pygame.mixer.init()
        self.cloud = cocos.sprite.Sprite('back.PNG')
        self.cloud.scale_x = 0.38
        self.cloud.scale_y = 0.45
        self.cloud.position = 300, 240
        self.add(self.cloud)
        self.gameover = None

        self.score = 0  # record score
        self.txt_score = cocos.text.Label(u'Score:0',
                                          font_name=FONTS,
                                          font_size=16,
                                          color=BLACK)
        self.txt_score.position = 500, 440
        self.add(self.txt_score, 99999)

        self.top = '', 0
        self.top_notice = cocos.text.Label(u'',
                                           font_name=FONTS,
                                           font_size=18,
                                           color=BLACK)
        self.top_notice.position = 400, 410
        self.add(self.top_notice, 99999)

        self.name = ''

        # init voice
        self.NUM_SAMPLES = 2048  # the size of memeory
        self.LEVEL = 1500  # save the sound selve

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX(self)
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        self.last_block = 0, 100
        for i in range(5):
            b = Block(self)
            self.floor.add(b)
            self.pitch_pic(b)
            pos = b.x + b.width, b.height

        # input the voice
        pa = PyAudio()
        SAMPLING_RATE = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=SAMPLING_RATE,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)
        self.stream.stop_stream()

        pygame.music.load('intro.wav'.encode())
        pygame.music.play(1)

        self.schedule(self.update)
コード例 #4
0
class VoiceGame(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, WIDTH, HEIGHT)
        pygame.mixer.init()
        self.cloud = cocos.sprite.Sprite('back.PNG')
        self.cloud.scale_x = 0.38
        self.cloud.scale_y = 0.45
        self.cloud.position = 300, 240
        self.add(self.cloud)
        self.gameover = None

        self.score = 0  # record score
        self.txt_score = cocos.text.Label(u'Score:0',
                                          font_name=FONTS,
                                          font_size=16,
                                          color=BLACK)
        self.txt_score.position = 500, 440
        self.add(self.txt_score, 99999)

        self.top = '', 0
        self.top_notice = cocos.text.Label(u'',
                                           font_name=FONTS,
                                           font_size=18,
                                           color=BLACK)
        self.top_notice.position = 400, 410
        self.add(self.top_notice, 99999)

        self.name = ''

        # init voice
        self.NUM_SAMPLES = 2048  # the size of memeory
        self.LEVEL = 1500  # save the sound selve

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX(self)
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        self.last_block = 0, 100
        for i in range(5):
            b = Block(self)
            self.floor.add(b)
            self.pitch_pic(b)
            pos = b.x + b.width, b.height

        # input the voice
        pa = PyAudio()
        SAMPLING_RATE = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=SAMPLING_RATE,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)
        self.stream.stop_stream()

        pygame.music.load('intro.wav'.encode())
        pygame.music.play(1)

        self.schedule(self.update)

    def pitch_pic(self, b):
        if b.num == 16:
            sprite = cocos.sprite.Sprite('sixteenth_note.png')
            sprite.scale = 0.1
            sprite.position = b.x - sprite.width, b.y
            self.floor.add(sprite)
        elif b.num == 8:
            sprite = cocos.sprite.Sprite('eighth_note.png')
            sprite.scale = 0.1
            sprite.position = b.x - sprite.width, b.y

            self.floor.add(sprite)
        elif b.num == 4:
            sprite = cocos.sprite.Sprite('quarter_note.png')
            sprite.scale = 0.1
            sprite.position = b.x - sprite.width, b.y

            self.floor.add(sprite)
        elif b.num == 2:
            sprite = cocos.sprite.Sprite('half_note.png')
            sprite.scale = 0.1
            sprite.position = b.x - sprite.width, b.y

            self.floor.add(sprite)
        elif b.num == 1:
            sprite = cocos.sprite.Sprite('whole_note.png')
            sprite.scale = 0.1
            sprite.position = b.x - sprite.width, b.y
            self.floor.add(sprite)

    def on_mouse_press(self, x, y, buttons, modifiers):
        pass

    def collide(self):
        px = self.ppx.x - self.floor.x
        for b in self.floor.get_children():
            if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                if self.ppx.y < b.height + b.y:
                    self.ppx.land(b.height + b.y)
                    break

    def update(self, dt):
        # input num_smaples
        if self.stream.is_stopped():
            self.stream.start_stream()
        string_audio_data = self.stream.read(self.NUM_SAMPLES)
        k = max(struct.unpack('2048h', string_audio_data))
        # print k
        self.voicebar.scale_x = k / 10000.0
        if k > 3000:
            if not self.ppx.dead:
                self.floor.x -= min((k / 20.0), 150) * dt
        if k > 8000:
            self.ppx.jump((k - 8000) / 25.0)
        self.floor.x -= self.ppx.velocity * dt
        self.collide()
        self.top_notice.x -= 80 * dt
        if self.top_notice.x < -700:
            self.top_notice.x = 700

    def reset(self):
        self.floor.x = 0
        self.last_block = 0, 100
        for b in self.floor.get_children():
            if isinstance(b, Block):
                b.reset()
            else:
                self.floor.remove(b)
        for b in self.floor.get_children():
            if isinstance(b, Block):
                self.pitch_pic(b)
        self.score = 0
        self.txt_score.element.text = u'Score:0'
        self.ppx.reset()
        if self.gameover:
            self.remove(self.gameover)
            self.gameover = None
        self.stream.start_stream()
        self.resume_scheduler()
        pygame.music.play(1)

        #self.pause_scheduler()
        #self.restart()
    def restart(self):
        cocos.director.director.replace(
            FadeTRTransition(first_scene, duration=2))

    def end_game(self):
        self.stream.stop_stream()
        self.pause_scheduler()
        # self.unschedule(self.update)
        self.gameover = Gameover(self)
        self.add(self.gameover, 100000)

    def show_top(self):
        self.remove(self.gameover)
        self.gameover = None

    def add_score(self):
        self.score += 10
        self.txt_score.element.text = u'Score:%d' % self.score
コード例 #5
0
ファイル: game.py プロジェクト: zybing/ppx
class VoiceGame(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)
        # init voice
        self.NUM_SAMPLES = 1000
        par = {"image": "black.png"}
        self.voicebar = Sprite(**par)
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)
        self.label = cocos.text.RichLabel('3 LIVES',
                                          font_name='Times New Roman',
                                          font_size=32,
                                          anchor_x='center',
                                          anchor_y='center',
                                          color=(255, 0, 0, 255))

        self.label.position = 330, 400

        self.add(self.label)

        self.ppx = PPX()
        self.add(self.ppx)

        # layer order according the layer added order
        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        self.maxx = 0
        for i in range(30):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height
            if i == 29:
                self.maxx = b.x

        # open sound input
        pa = PyAudio()
        sampling_rate = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=sampling_rate,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)

        self.schedule(self.update)

    # prevent ppx is collided by block

    def collide(self):
        px = self.ppx.x - self.floor.x
        if px > self.maxx:
            label = cocos.text.RichLabel('CONGRATULATION!!!',
                                         font_name='Times New Roman',
                                         font_size=32,
                                         anchor_x='center',
                                         anchor_y='center',
                                         color=(255, 0, 0, 255))
            label.position = 330, 400
            self.setlabel(label)
            for b in self.floor.get_children():
                if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                    if self.ppx.y < b.height:
                        if b._change:
                            if self.ppx.y > b.last_y - 30:
                                self.ppx.y = b.height
                            b._change = False
                        else:
                            self.ppx.land(b.height)
                        break
        else:
            for b in self.floor.get_children():
                if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                    if self.ppx.y < b.height:
                        if b._change:
                            if self.ppx.y > b.last_y - 30:
                                self.ppx.y = b.height
                            b._change = False
                        else:
                            self.ppx.land(b.height)
                        break

    def update(self, dt):
        # 读入NUM_SAMPLES个取样
        string_audio_data = self.stream.read(self.NUM_SAMPLES)
        k = max(struct.unpack('1000h', string_audio_data))
        # print k
        self.voicebar.scale_x = k / 10000.0
        if k > 2000:
            self.floor.x -= min((k / 20.0), 250) * dt
        if k > 6000:
            self.floor.x -= min((k / 20.0), 250) * dt
            self.ppx.jump((k - 6000) / 1000.0)
        self.collide()

    def reset(self):
        self.floor.x = 0

    def setlabel(self, nlabel):
        self.remove(self.label)
        self.label = nlabel
        self.add(self.label)
コード例 #6
0
class VoiceGame(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, 800, 600)

        self.logo = cocos.sprite.Sprite('crossin-logo.png')
        self.logo.position = 550, 400
        self.add(self.logo, 99999)

        # init voice
        self.NUM_SAMPLES = 1000  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX()
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        pos = 0, 100
        for i in range(100):
            b = Block(pos)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=self.NUM_SAMPLES)

        self.schedule(self.update)

    def on_mouse_press(self, x, y, buttons, modifiers):
        pass

    def collide(self):
        px = self.ppx.x - self.floor.x
        for b in self.floor.get_children():
            if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                if self.ppx.y < b.height:
                    self.ppx.land(b.height)
                    break

    def update(self, dt):
        # 读入NUM_SAMPLES个取样
        string_audio_data = self.stream.read(self.NUM_SAMPLES)
        k = max(struct.unpack('1000h', string_audio_data))
        # print k
        self.voicebar.scale_x = k / 10000.0
        if k > 3000:
            self.floor.x -= min((k / 20.0), 150) * dt
        if k > 8000:
            self.ppx.jump((k - 8000) / 1000.0)
        self.collide()

    def reset(self):
        self.floor.x = 0
コード例 #7
0
    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, WIDTH, HEIGHT)
        pygame.mixer.init()

        self.gameover = None
        self.billboard = None

        self.score = 0  #记录分数
        self.txt_score = cocos.text.Label(u'分数:0',
                                          font_name=FONTS,
                                          font_size=24,
                                          color=BLACK)
        self.txt_score.position = 500, 440
        self.add(self.txt_score, 99999)

        self.top = '', 0
        self.top_notice = cocos.text.Label(u'',
                                           font_name=FONTS,
                                           font_size=18,
                                           color=BLACK)
        self.top_notice.position = 400, 410
        self.add(self.top_notice, 99999)

        self.name = ''

        # init voice
        self.NUM_SAMPLES = 2048  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX(self)
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        self.last_block = 0, 100
        for i in range(5):
            b = Block(self)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=SAMPLING_RATE,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)
        self.stream.stop_stream()

        pygame.music.load('bgm.wav')
        pygame.music.play(-1)

        self.schedule(self.update)
コード例 #8
0
class VoiceGame(cocos.layer.ColorLayer):
    is_event_handler = True

    def __init__(self):
        super(VoiceGame, self).__init__(255, 255, 255, 255, WIDTH, HEIGHT)
        pygame.mixer.init()

        self.gameover = None
        self.billboard = None

        self.score = 0  #记录分数
        self.txt_score = cocos.text.Label(u'分数:0',
                                          font_name=FONTS,
                                          font_size=24,
                                          color=BLACK)
        self.txt_score.position = 500, 440
        self.add(self.txt_score, 99999)

        self.top = '', 0
        self.top_notice = cocos.text.Label(u'',
                                           font_name=FONTS,
                                           font_size=18,
                                           color=BLACK)
        self.top_notice.position = 400, 410
        self.add(self.top_notice, 99999)

        self.name = ''

        # init voice
        self.NUM_SAMPLES = 2048  # pyAudio内部缓存的块的大小
        self.LEVEL = 1500  # 声音保存的阈值

        self.voicebar = Sprite('black.png', color=(0, 0, 255))
        self.voicebar.position = 20, 450
        self.voicebar.scale_y = 0.1
        self.voicebar.image_anchor = 0, 0
        self.add(self.voicebar)

        self.ppx = PPX(self)
        self.add(self.ppx)

        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        self.last_block = 0, 100
        for i in range(5):
            b = Block(self)
            self.floor.add(b)
            pos = b.x + b.width, b.height

        # 开启声音输入
        pa = PyAudio()
        SAMPLING_RATE = int(
            pa.get_device_info_by_index(0)['defaultSampleRate'])
        self.stream = pa.open(format=paInt16,
                              channels=1,
                              rate=SAMPLING_RATE,
                              input=True,
                              frames_per_buffer=self.NUM_SAMPLES)
        self.stream.stop_stream()

        pygame.music.load('bgm.wav')
        pygame.music.play(-1)

        self.schedule(self.update)

    def on_mouse_press(self, x, y, buttons, modifiers):
        pass

    def collide(self):
        px = self.ppx.x - self.floor.x
        for b in self.floor.get_children():
            if b.x <= px + self.ppx.width * 0.8 and px + self.ppx.width * 0.2 <= b.x + b.width:
                if self.ppx.y < b.height:
                    self.ppx.land(b.height)
                    break

    def update(self, dt):
        # 读入NUM_SAMPLES个取样
        if self.stream.is_stopped():
            self.stream.start_stream()
        string_audio_data = self.stream.read(self.NUM_SAMPLES)
        k = max(struct.unpack('2048h', string_audio_data))
        # print k
        self.voicebar.scale_x = k / 10000.0
        if k > 3000:
            if not self.ppx.dead:
                self.floor.x -= min((k / 20.0), 150) * dt
        if k > 8000:
            self.ppx.jump((k - 8000) / 25.0)
        self.floor.x -= self.ppx.velocity * dt
        self.collide()
        self.top_notice.x -= 80 * dt
        if self.top_notice.x < -700:
            self.top_notice.x = 700

    def reset(self):
        self.floor.x = 0
        self.last_block = 0, 100
        for b in self.floor.get_children():
            b.reset()
        self.score = 0
        self.txt_score.element.text = u'分数:0'
        self.ppx.reset()
        if self.gameover:
            self.remove(self.gameover)
            self.gameover = None
        if self.billboard:
            self.remove(self.billboard)
            self.billboard = None
        self.stream.start_stream()
        self.resume_scheduler()
        pygame.music.play(-1)
        if self.top[0] and self.top[1]:
            notice = u'%s 刚刚以 %d 分刷新了今日最佳!' % self.top
            self.top_notice.element.text = notice
            self.top_notice.x = 800

    def end_game(self):
        self.stream.stop_stream()
        self.pause_scheduler()
        # self.unschedule(self.update)
        self.gameover = Gameover(self)
        self.add(self.gameover, 100000)

    def show_top(self):
        self.remove(self.gameover)
        self.gameover = None
        self.billboard = Billboard(self)
        self.add(self.billboard, 100001)

    def add_score(self):
        self.score += 1
        self.txt_score.element.text = u'分数:%d' % self.score