예제 #1
0
class ColorDemo(Arbapp):
    generators = [gen_random_flashing, gen_sweep_async, gen_sweep_rand, ]

    def __init__(self, parser, animations):
        Arbapp.__init__(self, parser)
        config = animations[self.args.type]
        self.durations = [int(config['dur_min']*config['rate']), int(config['dur_max']*config['rate'])]
        self.rate = Rate(config['rate'])
        if config['colors'][-1]!=config['colors'][0]:
            config['colors'].append(config['colors'][0])
        self.colors = config['colors']
        self.generator = self.generators[config['generator_id']]

    def run(self):
        # Construct all pixel generators
        generators = [[None for w in range(self.width)] for h in range(self.height)]
        for h in range(self.height):
            for w in range(self.width):
                duration = random.randrange(0, self.durations[1]-self.durations[0])
                generators[h][w] = self.generator(self.durations[0], int(2./self.rate.sleep_dur), duration, self.colors)

        # Browse all pixel generators at each time
        while True:
            self.model.lock()
            for h in range(self.height):
                for w in range(self.width):
                    try:
                        color = generators[h][w].next()
                    except StopIteration:
                        pass
                    else:
                        self.model.set_pixel(h, w, color)
            self.model.unlock()
            self.rate.sleep()
예제 #2
0
    def run(self):

        # Update the screen every second.
        rate = Rate(1.0)

        tick_second = False

        while True:
            self.model.lock()

            # Get the current time.
            now = datetime.datetime.today()
            hour = now.hour
            minute = now.minute

            # Extract the digits of each number in order to draw them
            # separately.
            hour_digits = self.extract_digits(hour)
            minute_digits = self.extract_digits(minute)

            # Display digits on the screen.
            self.draw_row(0, hour_digits)
            self.draw_row(1, minute_digits)

            # Flash the separator every two seconds.
            self.flash_separator(tick_second)
            tick_second = not tick_second

            self.model.unlock()
            rate.sleep()
예제 #3
0
 def run(self):
     # Update the screen every second.
     rate = Rate(2.0)
     for y in range(self.width):
         for x in range(self.height):
             with self.model:
                 self.model.set_pixel(x, y, self.PIXEL_COLOR)
             rate.sleep()
예제 #4
0
파일: Pong.py 프로젝트: titimoby/arbapps
 def run(self):
     rate = Rate(5)
     while True:
         self.process_events()
         self.apply_command()
         self.render()
         self.step_forward()
         rate.sleep()
예제 #5
0
파일: Bounces.py 프로젝트: titimoby/arbapps
class Bounces(Arbapp):

    def __init__(self, parser, rate):
        Arbapp.__init__(self, parser)
        self.balls = []
        self.rate = Rate(rate)

        def rand():
            return choice([-1, 1])*uniform(1./rate, 10./rate)

        for ball in range(4):
            self.balls.append(Ball(ball, randint(0, self.height-1), randint(0, self.width-1),
                                   self.height, self.width,
                                   Ball.colors[ball%len(Ball.colors)],
                                   rand(), rand()))

        # Motion control via Leap Motion
        self.swipe = [None]
        self.swipe_lock = Lock()
        self.leap_listener = SampleListener(self.swipe, self.swipe_lock)
        self.controller = Leap.Controller()
        self.controller.add_listener(self.leap_listener)

    def close(self, reason='unknown'):
        Arbapp.close(self, reason)
        self.controller.remove_listener(self.leap_listener)

    def render(self):
        self.model.lock()
        self.model.set_all('black')
        with self.swipe_lock:
            for ball in self.balls:
                self.model.set_pixel(ball.x, ball.y, ball.color)
                if self.swipe[0] is not None:
                    # mapping axes (height, width) of Arbalet on axes (x, z) of Leap Motion
                    x_speed_boost = self.swipe[0].direction[0] * self.swipe[0].speed / 500.
                    y_speed_boost = self.swipe[0].direction[2] * self.swipe[0].speed / 500.
                    ball.x_speed += x_speed_boost
                    ball.y_speed += y_speed_boost
            self.swipe[0] = None
        self.model.unlock()

    def run(self):
        while True:
            for ball in self.balls:
                ball.step_forward()
            self.render()
            self.rate.sleep()
예제 #6
0
 def __init__(self, parser, animations):
     Arbapp.__init__(self, parser)
     config = animations[self.args.type]
     self.durations = [int(config['dur_min']*config['rate']), int(config['dur_max']*config['rate'])]
     self.rate = Rate(config['rate'])
     if config['colors'][-1]!=config['colors'][0]:
         config['colors'].append(config['colors'][0])
     self.colors = config['colors']
     self.generator = self.generators[config['generator_id']]
예제 #7
0
    def __init__(self, num_lanes, path, level, speed):
        Arbapp.__init__(self)
        self.num_lanes = num_lanes
        self.score = 0
        self.speed = float(speed)
        self.rate = Rate(self.speed)
        self.grid = [['background']*num_lanes for h in range(self.height)] # The coming notes (last line included even if it will overwritten by the bottom bar)
        self.bar = ['idle']*num_lanes # The bottom bar, idle = not pressed, hit = pressed during a note, pressed = pressed outside a note

        # Threads creation and starting
        self.renderer = Renderer(self.model, self.grid, self.bar, self.height, num_lanes, self.width)
        self.reader = SongReader(path, num_lanes, level, speed)
        self.sound = SoundManager(path)
        self.hits = UserHits(self.num_lanes)
예제 #8
0
파일: Bounces.py 프로젝트: titimoby/arbapps
    def __init__(self, parser, rate):
        Arbapp.__init__(self, parser)
        self.balls = []
        self.rate = Rate(rate)

        def rand():
            return choice([-1, 1])*uniform(1./rate, 10./rate)

        for ball in range(4):
            self.balls.append(Ball(ball, randint(0, self.height-1), randint(0, self.width-1),
                                   self.height, self.width,
                                   Ball.colors[ball%len(Ball.colors)],
                                   rand(), rand()))

        # Motion control via Leap Motion
        self.swipe = [None]
        self.swipe_lock = Lock()
        self.leap_listener = SampleListener(self.swipe, self.swipe_lock)
        self.controller = Leap.Controller()
        self.controller.add_listener(self.leap_listener)
예제 #9
0
class LightsHero(Arbapp):
    def __init__(self, num_lanes, path, level, speed):
        Arbapp.__init__(self)
        self.num_lanes = num_lanes
        self.score = 0
        self.speed = float(speed)
        self.rate = Rate(self.speed)
        self.grid = [['background']*num_lanes for h in range(self.height)] # The coming notes (last line included even if it will overwritten by the bottom bar)
        self.bar = ['idle']*num_lanes # The bottom bar, idle = not pressed, hit = pressed during a note, pressed = pressed outside a note

        # Threads creation and starting
        self.renderer = Renderer(self.model, self.grid, self.bar, self.height, num_lanes, self.width)
        self.reader = SongReader(path, num_lanes, level, speed)
        self.sound = SoundManager(path)
        self.hits = UserHits(self.num_lanes)

    def next_line(self):
        # Delete the last line leaving the grid
        # Note : The bottom bar will overwrite the last line but the latter needs to be kept to draw the bottom bar
        for l in range(self.height-1, 0, -1):
            for w in range(self.num_lanes):
                self.grid[l][w] = self.grid[l-1][w]

        # Ask for a new line to the song reader and fill the top of the grid with it
        new_line = self.reader.read()
        for lane in range(self.num_lanes):
            self.grid[0][lane] = new_line[lane]

    def process_user_hits(self):
        """
        Read user inputs, update the bottom bar with key states and warn the UserHits class to update the score
        """
        for lane in range(self.num_lanes):
            must_press = self.grid[self.height-1][lane] == 'active' or self.grid[self.height-1][lane] == 'bump'
            pressed = self.hits.get_pressed_keys()[lane]
            if must_press and pressed:
                status = 'hit'
            elif pressed:
                status = 'pressed'
            else:
                status = 'idle'
            self.bar[lane] = status

            # warn the user hits class whether the note has to be played
            self.hits.set_note(lane, self.grid[self.height-1][lane] in ['bump', 'active'])

    def display_score(self):
        levels = [15, 40, 60, 80, 90, 101]
        sentences = ["did you really play?", "you need to practice...", "I'm pretty sure you can do better...",
                    "that's a fair score!", "awesome, you're a master!", "incredible, did you cheat?"]
        colors = ['darkred', 'orange', 'gold', 'yellowgreen', 'green', 'white']
        score = int((100.*self.hits.score)/self.hits.max_score)

        for i, level in enumerate(levels):
            if score<level:
                sentence = sentences[i]
                color = colors[i]
                break

        print "You scored", score, '% with', self.hits.score, 'hits over', self.hits.max_score

        if self.hits.score>0:
            self.model.write("You scored {}%, {}".format(score, sentence), color)

    def run(self):
        countdown = self.height # Countdown triggered after Midi's EOF
        start = time()

        # We loop while the end countdown is not timed out
        # it starts decreasing only when EOF is returned by the song reader
        while countdown>0:
            self.next_line()
            self.process_user_hits()
            self.renderer.update_view()

            if self.reader.eof:
                countdown -= 1

            # delayed sound playing while the first notes are reaching the bottom bar
            if not self.sound.started and time()-start > (self.height-2)/self.speed:
                self.sound.start()

            self.rate.sleep()
        self.display_score()
예제 #10
0
class LightsHero(Arbapp):
    def __init__(self, num_lanes, path, level, speed):
        Arbapp.__init__(self)
        self.num_lanes = num_lanes
        self.score = 0
        self.speed = float(speed)
        self.rate = Rate(self.speed)
        self.grid = [['background']*num_lanes for h in range(self.height)] # The coming notes (last line included even if it will overwritten by the bottom bar)
        self.bar = ['idle']*num_lanes # The bottom bar, idle = not pressed, hit = pressed during a note, pressed = pressed outside a note

        # Threads creation and starting
        self.renderer = Renderer(self.model, self.grid, self.bar, self.height, num_lanes, self.width)
        self.reader = SongReader(path, num_lanes, level, speed)
        self.sound = SoundManager(path)
        self.hits = UserHits(self.num_lanes)

    def next_line(self):
        # Delete the last line leaving the grid
        # Note : The bottom bar will overwrite the last line but the latter needs to be kept to draw the bottom bar
        for l in range(self.height-1, 0, -1):
            for w in range(self.num_lanes):
                self.grid[l][w] = self.grid[l-1][w]

        # Ask for a new line to the song reader and fill the top of the grid with it
        new_line = self.reader.read()
        for lane in range(self.num_lanes):
            self.grid[0][lane] = new_line[lane]

    def process_user_hits(self):
        """
        Read user inputs, update the bottom bar with key states and warn the UserHits class to update the score
        """
        for lane in range(self.num_lanes):
            must_press = self.grid[self.height-1][lane] == 'active' or self.grid[self.height-1][lane] == 'bump'
            pressed = self.hits.get_pressed_keys()[lane]
            if must_press and pressed:
                status = 'hit'
            elif pressed:
                status = 'pressed'
            else:
                status = 'idle'
            self.bar[lane] = status

            # warn the user hits class whether the note has to be played
            self.hits.set_note(lane, self.grid[self.height-1][lane] in ['bump', 'active'])


    def run(self):
        countdown = self.height # Countdown triggered after Midi's EOF
        start = time()

        # We loop while the end countdown is not timed out
        # it starts decreasing only when EOF is returned by the song reader
        while countdown>0:
            self.next_line()
            self.process_user_hits()
            self.renderer.update_view()

            if self.reader.eof:
                countdown -= 1

            # delayed sound playing while the first notes are reaching the bottom bar
            if not self.sound.started and time()-start > (self.height-2)/self.speed:
                self.sound.start()

            self.rate.sleep()

        print "You scored", int((100.*self.hits.score)/self.hits.max_score), '% with', self.hits.score, 'hits over', self.hits.max_score