def score_miss(track_dict, frame, velocity): """Iterate through all tracks and check any missed circles. Arguments: track_dict: A dict with values being tracks. frame: The frame rate. velocity: The velocity. Returns: A boolean whether there is a miss """ # check arguments if frame <= 0 or velocity <= 0: report_error('Invalid frame rate or velocity') score = 0 # loop through all tracks for track in track_dict.values(): # update perform flag of the track track.update_perform(PERFORM_DURATION * frame) # find any missed circle if calculate_accuracy(track, frame, velocity) < MISS: score += MISS_SCORE track.set_miss() # remove missed circle track.remove_circle() return score
def prepare_game(): """Initiate the game and return the length of music.""" music_length = get_music_length(parameters.music) # music should be longer than 30 secs at least if music_length < 30 or music_length > 1000: report_error('The music is too long or too short') display_text('Generating the game...', screen, DEFAULT_SIZE)
def render_all_tracks(track_dict, key_imgs, circle_img, screen): """Render all tracks. Each track has its key and circles. Arguments: track_dict: A dict with keys being index and values being tracks. key_imgs: The list of images of the key. The order should match track_dict. circle_img: The image of the circles. screen: The game screen that the track will be rendered on. """ # validate arguments if len(track_dict) != len(key_imgs): report_error('Keys and key_imgs do not match') for track, key_img in zip(track_dict.values(), key_imgs): render_track(track, key_img, circle_img, screen)
def __init__(self, sequence, shuffle=False): """Class constructor of Sequence. Arguments: sequence: The sequence of track indexes in a pattern. shuffle: A bool whether to shuffle the sequence. """ self.sequence = sequence self.length = len(sequence) if self.length <= 0: report_error('Empty pattern sequence') self.initial = randint(0, self.length - 1) if shuffle: random.shuffle(self.sequence) self.reset_index()
def render_text(text, position, screen, style, color='black'): """Render text to the screen. Arguments: text: The message to be rendered. position: The position of the text in the form of a tuple. screen: The game screen that the track will be rendered on. style: The style of the text, 'score', 'combo', or 'perform'. color: The color of the text. """ # validate arguments if style not in styles: report_error('No such text style') textsurface = styles[style].render(text, False, pygame.Color(color)) screen.blit(textsurface, position)
def render_text_center(text, screen, style, color='black'): """Render text to the center of the screen. Arguments: text: The message to be rendered. screen: The game screen that the track will be rendered on. style: The style of the text, 'score', 'combo', or 'perform'. color: The color of the text. """ # validate arguments if style not in styles: report_error('No such text style') textsurface = styles[style].render(text, False, pygame.Color(color)) position = textsurface.get_rect(center=(screen.get_width() / 2, screen.get_height() / 2)) screen.blit(textsurface, position)
def score_press(event, track_dict, mode, frame, velocity): """Iterate through all tracks and check any missed circles. Arguments: event: The key_down event. track_dict: A dict with values being tracks. mode: The key set of the game. frame: The frame rate. velocity: The velocity. Returns: A boolean whether there is a valid click """ # check arguments if frame <= 0 or velocity <= 0: report_error('Invalid frame rate or velocity') # get the pressed key index = mode.get(event.key, 0) # return 0 for invalid key pressing if index == 0: return 0 # get corresponding track track = track_dict[index] # calculate accuracy accuracy = abs(calculate_accuracy(track, frame, velocity)) # set score and perform score = 0 if accuracy < PERFECT: track.set_perfect() score = PERFECT_SCORE elif accuracy < GOOD: track.set_good() score = GOOD_SCORE elif accuracy < BAD: track.set_bad() score = BAD_SCORE # remove circle if valid if score != 0: track.remove_circle() return score
# game initiation, reading music files files = os.listdir(MUSIC_FOLDER) music_files = [] for filename in files: if filename.endswith('.mp3'): music_files.append(filename) # reading background img files files = os.listdir(IMAGE_FOLDER) background_images = [] for filename in files: if filename.startswith('background'): background_images.append(filename) if len(music_files) < 1: report_error('There is no valid music file in the music folder') if len(background_images) < 1: report_error('Missing background image files') parameters = Game.GameParameters(music_files[0], background_images[0]) screen = pygame.display.set_mode(DEFAULT_SIZE) def display_text(text, screen, size): """Display text and rerender the screen.""" background = load_img('index.jpg', size) render_background(background, screen) render_text_center(text, screen, 'center') pygame.display.update() # functions of widget to set parameters def set_music(_, selected_music):