Пример #1
0
def spawner(sm, sprite, fb, global_frame_number: int):
    # TODO currently this is the boss ai and not a generic ai, tag it that way
    if 'ai_music_started' not in sprite.info:
        Sound.stop_all_sounds()
        Sound.play_sound('Rhinoceros')
        sprite.info['ai_music_started'] = True
    fb_width = len(fb[0])
    fb_height = len(fb)
    sprite.info['ai_last_move'] += 1
    # Rodney position or 0
    rodney_y = 0
    for other_sprite in sm.sprites:
        if other_sprite.unique_sprite_name == 'rodney':
            rodney_y = other_sprite.info['y']


    # Spawn random enemies, pretty much a placeholder ai
    if sprite.info['ai_last_move'] >= 10:
        random_enemy = random.randint(0, 100)
        if 0 < random_enemy < 30:
            sm.add_sprite('projectile1_l', sprite.info['x'] - 7, rodney_y + 1)
        elif 30 <= random_enemy < 75:
            sm.add_sprite('fishmaster', sprite.info['x'] - 7, random.randint(0, fb_height-1))
        elif 75 <= random_enemy < 85:
            sm.add_sprite('spawner_rain', sprite.info['x'] - 7, random.randint(0, fb_height-11)+10)
        elif 85 <= random_enemy < 95:
            sm.add_sprite('splitter', sprite.info['x'] - 7, random.randint(0, fb_height//2-1)+fb_height//2)
        elif 95 <= random_enemy <= 100:
            sm.add_sprite('health', random.randint(0, 30), 0)
        sprite.info['ai_last_move'] = 0
Пример #2
0
def game_over():
    # Stop music
    Sound.stop_all_sounds()
    # Set resolution (in charcters, not pixels)
    reset_console_position()
    fb = framebuffer.make_framebuffer(80, 35)
    framebuffer.print_framebuffer(fb)
    reset_console_position()
    #print('You lost, returning to main menu')
    time.sleep(1)
    consume_all_keyboard_events()
Пример #3
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()
Пример #4
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