def _playback_recording(self): path = os.path.join(self.datapath, 'output.ogg') if self._uid is not None: dsobject = self._search_for_audio_note(self._uid) if dsobject is not None: path = dsobject.file_path aplay.play(path)
def _show_time(self): self.level = 0 self._all_clear() x = int(self._width / 4.) y = int(self._height / 8.) for i in range(len(str(self.score))): self._sticky_cards[i].move((x, y)) self._sticky_cards[i].set_layer(LOCO_LAYER) self._sticky_cards[i].set_label(str(self.score)[i]) x += int(self._loco_dim[0] / 2.) self.score = 0 self._parent.fullscreen() aplay.play(os.path.join(self._path, 'sounds', 'sonar.ogg')) GLib.timeout_add(5000, self.new_game, True)
def _test(self, easter_egg=False): ''' Test to see if we estimated correctly ''' if self._expert: delta = self.ball.width() / 6 else: delta = self.ball.width() / 3 x = self.ball.ball_x() + self.ball.width() / 2 f = int(self._fraction * self.bar.width()) self.bar.mark.move((int(f - self.bar.mark_width() / 2), int(self.bar.bar_y() + self._mark_offset(f)))) if self._challenges[self._n][2] == 0: # label the column spr = Sprite(self._sprites, 0, 0, self.blank_graphic) spr.set_label(self._label) spr.move((int(self._n * 27), 0)) spr.set_layer(-1) self._challenges[self._n][2] += 1 if x > f - delta and x < f + delta: spr = Sprite(self._sprites, 0, 0, self.smiley_graphic) self._correct += 1 aplay.play(self._path_to_success) else: spr = Sprite(self._sprites, 0, 0, self.frown_graphic) aplay.play(self._path_to_failure) spr.move((int(self._n * 27), int(self._challenges[self._n][2] * 27))) spr.set_layer(-1) # after enough correct answers, up the difficulty if self._correct == len(self._challenges) * 2: self._challenge += 1 if self._challenge < len(CHALLENGES): for challenge in CHALLENGES[self._challenge]: self._challenges.append(challenge) else: self._expert = True self.count += 1 self._dx = 0. # stop horizontal movement between bounces
def _animate(self): ''' A little Easter Egg just for fun. ''' if self._new_bounce: self._dy = self._ddy * (1 - STEPS) / 2 # initial step size self._new_bounce = False self._current_frame = 0 self._frame_counter = 0 self.ball.move_frame(self._current_frame, (self.ball.ball_x(), self.ball.ball_y())) self.ball.move_ball((self.ball.ball_x(), self._height)) aplay.play(self._path_to_bubbles) if self._accelerometer: fh = open(ACCELEROMETER_DEVICE) string = fh.read() xyz = string[1:-2].split(',') self._dx = float(xyz[0]) / 18. fh.close() else: self._dx = uniform(-int(DX * self._scale), int(DX * self._scale)) self.ball.move_frame_relative(self._current_frame, (int(self._dx), int(self._dy))) self._dy += self._ddy self._frame_counter += 1 self._current_frame = self.ball.next_frame(self._frame_counter) if self.ball.frame_y(self._current_frame) >= self.ball_y_max: # hit the bottom self.ball.move_ball((self.ball.ball_x(), self.ball_y_max)) self.ball.hide_frames() self._test(easter_egg=True) self._new_bounce = True self._defer_bounce(BOUNCE_PAUSE) else: GLib.timeout_add(STEP_PAUSE, self._animate)
def _play_target_sound(self): if self._activity.mode in ['letter', 'find by letter']: aplay.play(os.path.join( self._sounds_path, self._media_data[self.target][1]).strip()) elif self._activity.mode == 'picture': aplay.play(os.path.join( self._sounds_path, self._media_data[self.target][1]).strip()) GLib.timeout_add(1000, aplay.play, os.path.join( self._sounds_path, self._media_data[self.target][0]).strip()) else: aplay.play(os.path.join( self._sounds_path, self._media_data[self.target][0]).strip()) self.timeout = None
def _keypress_cb(self, area, event): ''' Keypress ''' # Games 4, 5, and 6 use the keyboard if self.level not in [4, 5, 6]: return True k = Gdk.keyval_name(event.keyval) if self._waiting_for_enter: if k == 'Return': self._waiting_for_enter = False self._panel.hide() self._counter += 1 self._correct = 0 GLib.timeout_add(1000, self.new_game, False) return if k in NOISE_KEYS or k in WHITE_SPACE: return True if self.level == 6 and self._waiting_for_delete: if k in ['BackSpace', 'Delete']: self._waiting_for_delete = False self._sticky_cards[self._correct].set_label_color('white') self._sticky_cards[self._correct].set_label(MSGS[ self._counter][self._sticky_cards[self._correct].type]) self._panel.hide() self._panel.set_label_color('black') return if k[0:5] == 'dead_': self._dead_key = k[5:] return if self.level == 6: n = len(MSGS[self._counter]) else: n = self._counter + 1 if self.level == 6: i = self._correct if self._dead_key is not None: k = DEAD_DICTS[DEAD_KEYS.index(self._dead_key)][k] self._dead_key = None elif k in PUNCTUATION: k = PUNCTUATION[k] elif k in SPECIAL: k = SPECIAL[k] elif len(k) > 1: return True if self._sticky_cards[i].labels[0] == k: self._sticky_cards[i].set_label_color('blue') self._sticky_cards[i].set_label(k) self._correct += 1 else: self._sticky_cards[i].set_label_color('red') self._sticky_cards[i].set_label(k) self._panel.set_label_color('red') self._panel.set_label(ALERTS[1]) self._panel.set_layer(PANEL_LAYER) self._waiting_for_delete = True aplay.play(os.path.join(self._path, 'sounds', 'glass.ogg')) else: for i in range(n): if self._sticky_cards[i].labels[0] == k: self._sticky_cards[i].set_label('') self._sticky_cards[i].hide() break # Test for end condition if self.level == 6 and \ self._correct == len(MSGS[self._counter]) - \ MSGS[self._counter].count(' '): c = 0 for i in range(len(MSGS[self._counter])): if MSGS[self._counter][i] == ' ': continue elif MSGS[self._counter][i] != self._sticky_cards[c].labels[0]: return True c += 1 self._panel.set_label(ALERTS[0]) self._panel.set_layer(PANEL_LAYER) self._waiting_for_enter = True aplay.play(os.path.join(self._path, 'sounds', 'drip.ogg')) return else: for i in range(n): if len(self._sticky_cards[i].labels[0]) > 0: return True self._counter += 1 self._correct = 0 GLib.timeout_add(1000, self.new_game, False)
def new_game(self, first_time): ''' Start a new game at the current level. ''' self._first_time = first_time self._clicked = False # It may be time to advance to the next level. if (self.level == 6 and self._counter == len(MSGS)) or \ self._counter > 4: self._first_time = True self.level += 1 self._counter = 0 self._correct = 0 self._pause = 200 if self.level == len(self._backgrounds): self._show_time() return self._all_clear() if self._first_time: # Every game starts by putting up a panel with instructions # The panel disappears on mouse movement self._panel.set_label(LABELS[self.level]) self._panel.set_layer(PANEL_LAYER) aplay.play(os.path.join(self._path, 'sounds', 'drip.ogg')) self._timer_reset() if self.level == 0: # Choose a random location for the Loco self._loco_quadrant += int(uniform(1, 4)) self._loco_quadrant %= 4 x, y = self._quad_to_xy(self._loco_quadrant) aplay.play(os.path.join(self._path, 'sounds', 'bark.ogg')) self._loco_cards[0].move((x, y)) self._loco_pos = (x, y) elif self.level == 1: aplay.play(os.path.join(self._path, 'sounds', 'glass.ogg')) elif self.level == 2: aplay.play(os.path.join(self._path, 'sounds', 'glass.ogg')) # Place some Locos on the canvas for i in range(self._counter + 1): self._loco_quadrant += int(uniform(1, 4)) self._loco_quadrant %= 4 x, y = self._quad_to_xy(self._loco_quadrant) self._sticky_cards[i].move((x, y)) self._sticky_cards[i].type = 'loco' self._sticky_cards[i].set_layer(LOCO_LAYER) elif self.level == 3: aplay.play(os.path.join(self._path, 'sounds', 'bark.ogg')) # Place some Locos on the left-side of the canvas for i in range(self._counter + 1): self._loco_quadrant = int(uniform(2, 4)) x, y = self._quad_to_xy(self._loco_quadrant) self._sticky_cards[i].move((x, y)) self._sticky_cards[i].type = 'loco' self._sticky_cards[i].set_layer(LOCO_LAYER) elif self.level == 4: # Place some Locos on the canvas with letters as labels # Just lowercase for i in range(self._counter + 1): self._loco_quadrant = int(uniform(0, 4)) x, y = self._quad_to_xy(self._loco_quadrant) self._sticky_cards[i].move((x, y)) self._sticky_cards[i].type = 'loco' self._sticky_cards[i].set_layer(LOCO_LAYER) self._sticky_cards[i].set_label(ALPHABETLC[int( uniform(0, len(ALPHABETLC)))]) elif self.level == 5: # Place some Locos on the canvas with letters as labels # Uppercase for i in range(self._counter + 1): self._loco_quadrant = int(uniform(0, 4)) x, y = self._quad_to_xy(self._loco_quadrant) self._sticky_cards[i].move((x, y)) self._sticky_cards[i].type = 'loco' self._sticky_cards[i].set_layer(LOCO_LAYER) self._sticky_cards[i].set_label(ALPHABETUC[int( uniform(0, len(ALPHABETUC)))]) elif self.level == 6: x = 0 y = 0 c = 0 for i in range(len(MSGS[self._counter])): if MSGS[self._counter][i] == ' ': y += self._loco_dim[1] x = 0 else: self._sticky_cards[c].move((x, y)) self._sticky_cards[c].type = i self._sticky_cards[c].set_layer(LOCO_LAYER) self._sticky_cards[c].set_label(MSGS[self._counter][i]) c += 1 x += int(self._loco_dim[0] / 2.) if self.level in [0, 1]: self._loco_quadrant += int(uniform(1, 4)) self._loco_quadrant %= 4 x, y = self._quad_to_xy(self._loco_quadrant) if self.level == 0: self._move_loco(x, y, 0) else: self._taunt(x, y, 0)