NAME

Game::Floppy::Terminal - Floppy in a terminal

VERSION

Version 0.10

SYNOPSIS

use Game::Floppy::Terminal;

my $terminal = Game::Floppy::Terminal->new(seed => 'anything');
my $result = $terminal->start;        # returns; never exits

exit Game::Floppy::Terminal->run(@ARGV);   # which is all of bin/floppy

DESCRIPTION

The playable game on two filehandles, twenty frames a second. It draws the field, reads the keys and ticks the engine; the rules are Game::Floppy's and nothing here knows them. It is a separate module so the engine can be loaded by anything without pulling a terminal in.

The field is 516 by 400 pixels whatever the window: the view scales it to a grid of cells that keeps the field's shape (a cell is about twice as tall as it is wide, so 20 rows take 52 columns), at most 25 rows. A pipe is drawn to the cell its lip reaches, with a half block where it reaches half a cell, and the bird is the cells its circle covers. What you see is what collides.

The one wait in the loop is a timed read of the keyboard: a key ends it early and is acted on, and the deadline passing is the tick. A tick that is late fires once and the schedule restarts (Game::Terminal::Kit::Ticker).

A game lasts seconds. After a death the last frame stays on the screen with its sentence; space plays again and quits remain quits. The best score of the run is kept.

The keys need Term::ReadKey, which is recommended and not required. Without it, or without a terminal, the game cannot be played by hand and says so; --watch still works anywhere because it reads nothing.

PROPERTIES

in, out

The handles, read-write, defaulting to STDIN and STDOUT.

rows, cols

The view, in cells. Left out, the view is fitted to the window.

seed

The game. Left out, a fresh one is made.

level, watch

The bot's level (1 to 3) and whether it, not you, flies.

quiet, ascii

Draw no frames; use plain characters instead of block glyphs.

ansi, interactive

Given to new only. ansi forces colour on or off, and NO_COLOR in the environment beats it. interactive forces the redrawn-in-place frame on or off; the default follows whether out is a terminal.

max_ticks

Stop after that many ticks; 0, the default, plays to the end.

game, ticker

The Game::Floppy being played and its Game::Terminal::Kit::Ticker, built in new unless given.

keysource, clock, sleeper, columns, lines

For tests. keysource is a code reference called with the seconds to wait that returns one character, or undefined when none came in time; clock returns seconds; sleeper is called with seconds to wait when there is nothing to read; columns and lines stand in for the window's size.

pending

Characters to be read before any others, an array reference.

keyboard, screen

The Game::Terminal::Kit::Keys the keys are read through and the Game::Terminal::Kit::Screen the frames are drawn through, built in new from the handles unless given.

frames, refused, games, best, stopped, refusal

After start: frames drawn, flaps refused, games played, the best score, how it stopped (empty for a death, quit, capped, narrow or keys) and the sentence saying why it could not start.

METHODS

start

Plays until a quit, a cap, or a death that is not followed by space, and returns the last game's Game::Floppy::Result; undefined when it could not start, with refusal saying why. Never calls exit.

options

my $fields = Game::Floppy::Terminal->options;

The options a person would set before a game, in the order a launcher shows them. Each is a hash reference with a name, a type and a help, and min and max or choices where the type has them. A new list is returned each time.

level

A whole number from 1 to 3: the bot's level, for watch.

watch

A switch: watch the bot fly.

rows

A whole number from 10 to 25: the view in cells; the default fits the window.

seed

Text: the same seed and the same flaps are the same game.

run

exit Game::Floppy::Terminal->run(@ARGV);

The program. Parses the options, plays, and returns the exit status: 0 when a game ended or was quit, 2 for a bad option or a game that could not start. --help goes to STDOUT; the usage after a bad option, and every refusal, to STDERR.

view

The view's columns and rows.

too_narrow, cannot_play

The sentence for a window that cannot hold the view, or undefined; the sentence for a game with no way to read keys.

keys_available, enter_raw, leave_raw, raw, read_key

Whether keys can be read; reading a key at a time, giving the terminal back with its cursor, and whether a key at a time is how it is being read now; one key by name within the wait, the names being Game::Terminal::Kit::Keys's.

board_lines, frame_lines, status_line, legend

The field as lines, the whole frame, and its two text lines.

ansi, interactive, usage

Whether colour and in-place redrawing are on; the usage text.

AUTHOR

LNATION, <email at lnation.org>

LICENSE AND COPYRIGHT

This software is Copyright (c) 2024 by LNATION.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)