NAME
Game::Snake - the classic snake game, as an engine
VERSION
Version 0.13
SYNOPSIS
use Game::Snake;
my $game = Game::Snake->new(seed => 'anything');
$game->turn('up');
while ($game->status ne 'finished') {
my $step = $game->tick;
last if $step->{died};
}
print $game->result->stringify, "\n";
Or, from a shell:
snake
snake --raylib
DESCRIPTION
A snake on a grid, heading one of four ways, eating to grow and dying at a wall or its own body. This class is the game and nothing else: it prints nothing, reads nothing, sleeps never and calls rand never. Every placement of food comes from the seed, so the same seed and the same turns are the same game twice.
The terminal game is Game::Snake::Terminal and the window is Game::Snake::Raylib. They are separate modules so that this one can be loaded by anything without pulling either in.
The board is cols by rows cells, x from 0 at the left and y from 0 at the top. The snake starts start_length long, heading left, its head a little left of centre and its body to the right.
METHODS
new
my $game = Game::Snake->new(
cols => 40,
rows => 28,
seed => 'anything',
start_length => 3,
food_score => 10,
start_speed => 2,
);
Every argument has a default, shown. A board is at least six by six. The seed is any string; the default seed plays the same game every time, so a program that wants a different game each run makes its own seed.
Arguments are read by name, and a name this class does not know is dropped in silence: new(colz => 9) builds the default board. Spell them carefully.
turn
$game->turn('up') or warn $game->error->message;
Queues a heading, one of up, down, left and right. Two headings may wait at once: the first is what the next tick follows, and a third press replaces the second. A heading the snake already has is refused as same_direction, the opposite of it as reverse, and any turn on a finished game as not_active. Returns 1, or 0 with "error" set. A word that is not a direction is a programmer error and dies.
tick
my $step = $game->tick;
Moves the snake one cell. Returns a hash with moved, ate, grew and died, the last undefined or one of wall, body and full. The snake grows the tick after it eats, and the tail leaves its cell before the head arrives, so a head may follow the tail into the cell it is leaving. The speed rises by one each time the score reaches double what it was at the last rise. A board with no free cell left ends the game as full. A tick on a finished game moves nothing.
segments
for my $segment (@{ $game->segments }) {
printf "%d,%d %s\n", $segment->{x}, $segment->{y}, $segment->{dir};
}
The snake head first, each cell with its x, y, the direction dir it is heading, bend (the heading of the cell in front when that differs, else 0) and last, 1 on the tail. The tail's direction points at the cell in front of it, so a renderer may draw it from dir alone.
at
my $what = $game->at($x, $y);
head, body, tail, food or undefined.
free_cells
Every cell the snake is not on, as [x, y] pairs in row-major order.
food
The cell the food is on, as [x, y], or undefined once the board is full.
cols, rows, seed, start_length, food_score, start_speed
The arguments new was given, or their defaults.
length, heading, status, score, speed, ticks, growing
How long the snake is, which way it is heading, ready, active or finished, the score, cells a second, ticks taken, and how many cells of growth are owed from meals not yet grown into. While growing is above zero the tail stays where it is on the next tick.
error
The Game::Snake::Error from the last refused turn, or undefined after a turn that was taken.
result
A Game::Snake::Result once the game has finished, else undefined.
quit
Finishes the game with the cause quit. Returns 1, or 0 if it had already finished.
reset
Puts the snake back at the start with the same seed, so the same game can be played again.
AUTHOR
LNATION, <email at lnation.org>
BUGS
Please report any bugs or feature requests to bug-game-snake at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-Snake. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Game::Snake
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
Search CPAN
LICENSE AND COPYRIGHT
This software is Copyright (c) 2024 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)