| 1 | 1 | ▶ | use 5.38.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use experimental 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Game::Floppy 0.08; | ||
| 5 | class Game::Floppy { | ||
| 6 | our $VERSION = 0.08; | ||
| 7 | 1 | ▶ | use Raylib::App; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Raylib::FFI; | ||
| 9 | use Raylib::Color; | ||
| 10 | use Raylib::Keyboard; | ||
| 11 | use Game::Floppy::Bird; | ||
| 12 | use Game::Floppy::Tube; | ||
| 13 | use Game::Floppy::Sprite; | ||
| 14 | |||
| 15 | field $floppy_radius : param = 24; | ||
| 16 | field $tubes_width : param = 80; | ||
| 17 | field $width : param = 516; | ||
| 18 | field $height : param = 400; | ||
| 19 | field $fps : param = 20; | ||
| 20 | |||
| 21 | field $frame; | ||
| 22 | field $game_over = 0; | ||
| 23 | field $pause = 0; | ||
| 24 | field $score = 0; | ||
| 25 | field $hi_score = 0; | ||
| 26 | field $floppy; | ||
| 27 | field @tubes; | ||
| 28 | |||
| 29 | method run { | ||
| 30 | my $app = Raylib::App->window( $width, $height, 'Floppy' ); | ||
| 31 | $app->fps($fps); | ||
| 32 | $floppy = Game::Floppy::Bird->new( | ||
| 33 | x => 80, | ||
| 34 | y => $height / 2 - $floppy_radius, | ||
| 35 | width => $floppy_radius, | ||
| 36 | height => $floppy_radius | ||
| 37 | ); | ||
| 38 | |||
| 39 | my $background = Game::Floppy::Sprite->new( | ||
| 40 | image => 'resources/background-day.png', | ||
| 41 | x => 0, | ||
| 42 | y => 0, | ||
| 43 | ); | ||
| 44 | |||
| 45 | my $keyboard = Raylib::Keyboard->new( | ||
| 46 | key_map => { | ||
| 47 | # vim keys | ||
| 48 | KEY_SPACE() => sub { | ||
| 49 | if ($game_over) { | ||
| 50 | $score = 0; | ||
| 51 | @tubes = (); | ||
| 52 | $floppy->y( $height / 2 - $floppy_radius ); | ||
| 53 | $game_over = 0; | ||
| 54 | } else { | ||
| 55 | $self->handle_click(); | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | while (!$app->exiting) { | ||
| 62 | $app->draw( | ||
| 63 | sub { | ||
| 64 | $frame++; | ||
| 65 | $app->clear(); | ||
| 66 | $background->draw(0, 0, $width, $height); | ||
| 67 | |||
| 68 | if (IsMouseButtonReleased(0)) { | ||
| 69 | $self->handle_click(); | ||
| 70 | } | ||
| 71 | $keyboard->handle_events(); | ||
| 72 | |||
| 73 | if ($game_over) { | ||
| 74 | |||
| 75 | DrawText( "Current Score: $score High Score: $hi_score", 10, 10, 22, Raylib::Color::WHITE ); | ||
| 76 | DrawText( "Game Over", 10, 40, 22, Raylib::Color::WHITE ); | ||
| 77 | |||
| 78 | DrawText( "Press space to try again", 10, 70, 22, Raylib::Color::WHITE ); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | $self->handle_flappy(); | ||
| 82 | $self->handle_tubes(); | ||
| 83 | |||
| 84 | DrawText( "Current Score: $score High Score: $hi_score", 10, 10, 22, Raylib::Color::WHITE ); | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | method handle_click () { | ||
| 91 | $floppy->y($floppy->y - 30); | ||
| 92 | $floppy->sprite->rotate(-45); | ||
| 93 | } | ||
| 94 | |||
| 95 | method handle_flappy () { | ||
| 96 | $floppy->y($floppy->y + 3); | ||
| 97 | $floppy->draw(); | ||
| 98 | if ($floppy->y > $height || $floppy->y < 0) { | ||
| 99 | $game_over = 1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | method handle_tubes () { | ||
| 104 | for (reverse @tubes) { | ||
| 105 | $_->x($_->x - 3); | ||
| 106 | $_->draw(); | ||
| 107 | |||
| 108 | if ($_->check_collision($floppy)) { | ||
| 109 | $game_over = 1; | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (($_->x + $_->width < $floppy->x) && $_->active) { | ||
| 114 | $score += 100; | ||
| 115 | $_->active(0); | ||
| 116 | if ($score > $hi_score) { | ||
| 117 | $hi_score = $score; | ||
| 118 | } | ||
| 119 | shift @tubes if scalar @tubes; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | if ($frame % 100 == 0) { | ||
| 126 | push @tubes, Game::Floppy::Tube->new( | ||
| 127 | x => $width, | ||
| 128 | y => 0, | ||
| 129 | width => 50, | ||
| 130 | height => int(rand($height - 100)) | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | Game::Floppy - Floppy bird using raylib | ||
| 144 | |||
| 145 | =head1 VERSION | ||
| 146 | |||
| 147 | Version 0.08 | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | |||
| 152 | =head1 SYNOPSIS | ||
| 153 | |||
| 154 | lnation$ floppy.pl | ||
| 155 | ... | ||
| 156 | use Game::Floppy; | ||
| 157 | my $floppy = Game::Floppy->new(); | ||
| 158 | $floppy->run(); | ||
| 159 | |||
| 160 | =for html <img style="width:500px" src="https://raw.githubusercontent.com/ThisUsedToBeAnEmail/Game-Floppy/master/floppy.png" title="img-tag, local-dist" alt="Inlineimage" /> | ||
| 161 | |||
| 162 | =head1 AUTHOR | ||
| 163 | |||
| 164 | LNATION, C<< <email at lnation.org> >> | ||
| 165 | |||
| 166 | =head1 BUGS | ||
| 167 | |||
| 168 | Please report any bugs or feature requests to C<bug-game-floppy at rt.cpan.org>, or through | ||
| 169 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-Floppy>. I will be notified, and then you'll | ||
| 170 | automatically be notified of progress on your bug as I make changes. | ||
| 171 | |||
| 172 | =head1 SUPPORT | ||
| 173 | |||
| 174 | You can find documentation for this module with the perldoc command. | ||
| 175 | |||
| 176 | perldoc Game::Floppy | ||
| 177 | |||
| 178 | |||
| 179 | You can also look for information at: | ||
| 180 | |||
| 181 | =over 4 | ||
| 182 | |||
| 183 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 184 | |||
| 185 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Game-Floppy> | ||
| 186 | |||
| 187 | =item * CPAN Ratings | ||
| 188 | |||
| 189 | L<https://cpanratings.perl.org/d/Game-Floppy> | ||
| 190 | |||
| 191 | =item * Search CPAN | ||
| 192 | |||
| 193 | L<https://metacpan.org/release/Game-Floppy> | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | |||
| 198 | =head1 ACKNOWLEDGEMENTS | ||
| 199 | |||
| 200 | |||
| 201 | =head1 LICENSE AND COPYRIGHT | ||
| 202 | |||
| 203 | This software is Copyright (c) 2024 by LNATION. | ||
| 204 | |||
| 205 | This is free software, licensed under: | ||
| 206 | |||
| 207 | The Artistic License 2.0 (GPL Compatible) | ||
| 208 | |||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | 1; # End of Game::Floppy | ||