示例#1
0
def game_loop():
    """ Main game loop, currently scenes are not support, so every part (main menu, game over,...) is separate """
    # Stop music, since we can enter the game loop from another screen with music
    Sound.stop_all_sounds()
    Sound.play_sound('Ouroboros')
    # Set palette
    Palette.set_palette(Palette.GAME_PALETTE)
    # Set resolution
    set_resolution(80, 35)
    # Setup data for game loop
    fb_width = 80
    fb_height = 30
    fb = framebuffer.make_framebuffer(fb_width, fb_height)
    frame = 0
    last_time = time.time()
    # Spawn frame counter
    frame_counter = 0

    # Setup sprite manager, add rodney
    sm = framebuffer.SpriteManager()
    sm.add_sprite('rodney', 6, 10, 'rodney')
    # Create bg
    bg = sprites.bg
    bg_counter = 0
    # Game loop
    while True:
        current_time = time.time()

        if current_time - last_time > 0.1:
            last_time = current_time

            framebuffer.draw_background(bg_counter // 10, bg, fb)

            sm.advance_ai(fb, frame)
            Spawning.check_spawns(Spawning.SPAWN_LIST_STAGE_1, frame_counter, sm)

            sm.check_collision(fb)
            # Normally we would have fancy stuff to detect a game state change
            # but we simply check if rodney is still alive and then let his ai method decide on what to do
            try:
                rodney = sm.get_unique_sprite('rodney')
                if 'ai_quit_to_main_menu' in rodney.info:
                    return 'main_menu'
                if 'ai_game_won' in rodney.info:
                    return 'end_screen'
            except KeyError:
                return 'game_over'
            sm.draw_all(frame, fb)

            framebuffer.print_framebuffer(fb)
            # Advance framenumber, wrap around at 60
            # 60 is quite divisible (2, 3, 4, 5, 6, 10) which allows for many different loopable animations
            # animation state is a global state not a per-sprite state
            frame = (frame + 1) % 60
            # Advance bg counter
            bg_counter += 1
            frame_counter += 1
            # HUD
            #print('RODNEY: %s ' % (Fore.RED + ('#' * sm.get_unique_sprite('rodney').info['health']) + Fore.RESET), end="")
            # TODO dangerous, if rodney get's removed we get an exception, but currently it's filtered above
            print('________________________________________________________________________________', end='')
            print('RODNEY HEALTH: %i   ' % sm.get_unique_sprite('rodney').info['health'], end='\n')
            for sprite in sm.sprites:
                if sprite.unique_sprite_name == 'razmi':
                    razmi = sprite
                    blocks = int((razmi.info['health'] / razmi.info['health_max']) * 10)
                    print('RAVIOLI: %s                          ' % (Fore.RED + ('\u2588' * blocks + Fore.RESET)), end='')
            # Reset position and flush all output
            reset_console_position()
            sys.stdout.flush()
示例#2
0
def main_menu():
    # Window size
    window_x = 41
    window_y = 20
    # Clear screen
    os.system('cls')
    # Stop music, start music
    Sound.stop_all_sounds()
    Sound.play_sound('BlipStream')
    # Set palette
    Palette.set_palette(Palette.GAME_PALETTE)
    # Set resolution
    set_resolution(window_x, window_y)
    # Setup framebuffer
    fb = framebuffer.make_framebuffer(window_x, window_y-1)
    # Reset drawing position
    reset_console_position()
    # Setup variables for game loop
    frame = 0
    current_time = time.time()
    last_time = current_time
    # Add sprites
    sm = framebuffer.SpriteManager()
    sm.add_sprite('press_any_key', 10, 5)
    sm.add_sprite('credits', 0, 15)
    sm.add_sprite('rodney', 4, 3)
    sm.add_sprite('razmi', 27, 0)
    while True:
        current_time = time.time()

        if current_time - last_time > 0.1:
            last_time = current_time
            # Clear framebuffer
            framebuffer.clear_framebuffer(fb)
            # Draw sprites to framebuffer
            sm.draw_all(frame, fb)
            # Increase frame counter
            frame = (frame + 1) % 60
            # Draw framebuffer
            framebuffer.print_framebuffer(fb)
            # Reset position and flush all output
            reset_console_position()
            sys.stdout.flush()

            # Handling is done here instead of a sprite ai because no ai calls are made (only animation is needed)
            if msvcrt.kbhit():
                parsed_chars = set()
                while msvcrt.kbhit():
                    input_char = msvcrt.getch()
                    input_int = struct.unpack('B', input_char)[0]
                    if input_int in parsed_chars:
                        continue
                    parsed_chars.add(input_int)
                    # If ESC is pressed
                    if input_char == b'\x1b':
                        # Exit game
                        sys.exit()
                    else:
                        # Consume the rest of the characters
                        while msvcrt.kbhit():
                            msvcrt.getch()
                        # Start game (see main)
                        return