| 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::Snake 0.11; | ||
| 5 | class Game::Snake { | ||
| 6 | our $VERSION = 0.11; | ||
| 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::Snake::Cell; | ||
| 12 | use Game::Snake::Text; | ||
| 13 | use Game::Snake::Head; | ||
| 14 | use Game::Snake::Tail; | ||
| 15 | use Game::Snake::Food; | ||
| 16 | |||
| 17 | field $frame = 0; | ||
| 18 | |||
| 19 | field $width : param = 800; | ||
| 20 | field $height : param = 600; | ||
| 21 | field $cell_size : param = 20; | ||
| 22 | field $top_padding : param = 20; | ||
| 23 | field $food_score : param = 10; | ||
| 24 | |||
| 25 | field @grid; | ||
| 26 | field %collide; | ||
| 27 | field $game_over = 0; | ||
| 28 | field $score = 0; | ||
| 29 | field $last_score = 10; | ||
| 30 | field $high_score = 0; | ||
| 31 | field @snake; | ||
| 32 | field $food; | ||
| 33 | field $fps = 2; | ||
| 34 | |||
| 35 | method reset_game () { | ||
| 36 | $game_over = 0; | ||
| 37 | $score = 0; | ||
| 38 | $last_score = 10; | ||
| 39 | $fps = 2; | ||
| 40 | @snake = (); | ||
| 41 | push @snake, Game::Snake::Head->new( | ||
| 42 | x => 300, | ||
| 43 | y => 400, | ||
| 44 | width => $cell_size, | ||
| 45 | height => $cell_size, | ||
| 46 | direction => 'left' | ||
| 47 | ); | ||
| 48 | push @snake, Game::Snake::Tail->new( | ||
| 49 | x => 320, | ||
| 50 | y => 400, | ||
| 51 | width => $cell_size, | ||
| 52 | height => $cell_size, | ||
| 53 | direction => 'left' | ||
| 54 | ); | ||
| 55 | push @snake, Game::Snake::Tail->new( | ||
| 56 | x => 340, | ||
| 57 | y => 400, | ||
| 58 | width => $cell_size, | ||
| 59 | height => $cell_size, | ||
| 60 | direction => 'left' | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | method run { | ||
| 65 | my $app = Raylib::App->window( $width, $height, "Snake" ); | ||
| 66 | $app->fps($fps); | ||
| 67 | |||
| 68 | $self->reset_game(); | ||
| 69 | |||
| 70 | my $score_text = Game::Snake::Text->new( | ||
| 71 | x => 10, | ||
| 72 | y => 10, | ||
| 73 | size => 20, | ||
| 74 | ); | ||
| 75 | |||
| 76 | my $gameover_text = Game::Snake::Text->new( | ||
| 77 | x => 260, | ||
| 78 | y => 280, | ||
| 79 | size => 40, | ||
| 80 | ); | ||
| 81 | |||
| 82 | my $keyboard = Raylib::Keyboard->new( | ||
| 83 | key_map => { | ||
| 84 | # vim keys | ||
| 85 | KEY_W() => sub { $self->move('up') }, | ||
| 86 | KEY_S() => sub { $self->move('down') }, | ||
| 87 | KEY_A() => sub { $self->move('left') }, | ||
| 88 | KEY_D() => sub { $self->move('right') }, | ||
| 89 | # arrow keys | ||
| 90 | KEY_UP() => sub { $self->move('up') }, | ||
| 91 | KEY_DOWN() => sub { $self->move('down') }, | ||
| 92 | KEY_LEFT() => sub { $self->move('left') }, | ||
| 93 | KEY_RIGHT() => sub { $self->move('right') }, | ||
| 94 | |||
| 95 | KEY_ENTER() => sub { | ||
| 96 | if ($game_over) { | ||
| 97 | $self->reset_game(); | ||
| 98 | } | ||
| 99 | }, | ||
| 100 | KEY_SPACE() => sub { | ||
| 101 | if ($game_over) { | ||
| 102 | $self->reset_game(); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | }, | ||
| 107 | ); | ||
| 108 | |||
| 109 | $self->create_grid(); | ||
| 110 | |||
| 111 | while (!$app->exiting) { | ||
| 112 | $app->draw( | ||
| 113 | sub { | ||
| 114 | $frame++; | ||
| 115 | $app->fps($fps); | ||
| 116 | $app->clear(); | ||
| 117 | $score_text->draw("Score: $score High Score: $high_score"); | ||
| 118 | |||
| 119 | $keyboard->handle_events(); | ||
| 120 | if ($game_over) { | ||
| 121 | $gameover_text->draw("Game Over"); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | $self->show_grid(); | ||
| 126 | $self->draw_snake(); | ||
| 127 | $self->draw_food(); | ||
| 128 | } | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | method create_grid { | ||
| 134 | my $alt = 1; | ||
| 135 | for (my $y = $cell_size + $top_padding; $y < $height; $y += $cell_size) { | ||
| 136 | for (my $x = 0; $x < $width; $x += $cell_size) { | ||
| 137 | push @grid, Game::Snake::Cell->new( | ||
| 138 | x => $x, | ||
| 139 | y => $y, | ||
| 140 | width => $cell_size, | ||
| 141 | height => $cell_size, | ||
| 142 | color => $alt ? Raylib::Color::GRAY : Raylib::Color::DARKGRAY | ||
| 143 | ); | ||
| 144 | $alt = !$alt; | ||
| 145 | } | ||
| 146 | $alt = !$alt; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | method show_grid { | ||
| 151 | for (@grid) { | ||
| 152 | $_->draw(); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | method move ($move) { | ||
| 157 | my $head = $snake[0]; | ||
| 158 | if ( | ||
| 159 | $move eq $head->direction | ||
| 160 | || ( $move eq 'right' && $head->direction eq 'left' ) | ||
| 161 | || ( $move eq 'left' && $head->direction eq 'right' ) | ||
| 162 | || ( $move eq 'up' && $head->direction eq 'down' ) | ||
| 163 | || ( $move eq 'down' && $head->direction eq 'up' ) | ||
| 164 | ) { | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | $head->direction($move); | ||
| 168 | } | ||
| 169 | |||
| 170 | method extend_snake { | ||
| 171 | my $last = $snake[-1]; | ||
| 172 | my $x = $last->x; | ||
| 173 | $x += $cell_size if ($last->direction eq 'left'); | ||
| 174 | $x -= $cell_size if ($last->direction eq 'right'); | ||
| 175 | my $y = $last->y; | ||
| 176 | $y += $cell_size if ($last->direction eq 'up'); | ||
| 177 | $y -= $cell_size if ($last->direction eq 'down'); | ||
| 178 | |||
| 179 | push @snake, Game::Snake::Tail->new( | ||
| 180 | x => $x, | ||
| 181 | y => $y, | ||
| 182 | width => $cell_size, | ||
| 183 | height => $cell_size, | ||
| 184 | direction => $last->direction | ||
| 185 | ); | ||
| 186 | |||
| 187 | |||
| 188 | if ($score % ($last_score * 2) == 0) { | ||
| 189 | $fps++; | ||
| 190 | $last_score = $score; | ||
| 191 | } | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | method draw_snake { | ||
| 196 | %collide = (); | ||
| 197 | my ($last); | ||
| 198 | for (my $i = scalar @snake - 1; $i >= 0; $i--) { | ||
| 199 | my $sn = $snake[$i]; | ||
| 200 | if ($sn->direction eq 'left') { | ||
| 201 | $sn->x($sn->x - $cell_size); | ||
| 202 | } elsif ($sn->direction eq 'right') { | ||
| 203 | $sn->x($sn->x + $cell_size); | ||
| 204 | } elsif ($sn->direction eq 'up') { | ||
| 205 | $sn->y($sn->y - $cell_size); | ||
| 206 | } elsif ($sn->direction eq 'down') { | ||
| 207 | $sn->y($sn->y + $cell_size); | ||
| 208 | } | ||
| 209 | |||
| 210 | if ($i > 0) { | ||
| 211 | $sn->bend( $snake[$i - 1]->direction ne $sn->direction ? $snake[$i - 1]->direction : 0 ); | ||
| 212 | $sn->last($i == $#snake ? 1 : 0 ); | ||
| 213 | } | ||
| 214 | $sn->draw(); | ||
| 215 | if ($last) { | ||
| 216 | $last->direction($sn->direction); | ||
| 217 | $collide{$last->x}->{$last->y}++; | ||
| 218 | } | ||
| 219 | $last = $sn; | ||
| 220 | } | ||
| 221 | |||
| 222 | my $head = $snake[0]; | ||
| 223 | if ( | ||
| 224 | $collide{$head->x}->{$head->y} | ||
| 225 | || $head->x < 0 | ||
| 226 | || $head->x > $width | ||
| 227 | || $head->y < $cell_size + $top_padding | ||
| 228 | || $head->y > $height | ||
| 229 | |||
| 230 | ) { | ||
| 231 | $game_over = 1; | ||
| 232 | } | ||
| 233 | |||
| 234 | $collide{$head->x}->{$head->y}++; | ||
| 235 | } | ||
| 236 | |||
| 237 | method draw_food { | ||
| 238 | if (!$food) { | ||
| 239 | my $rand; | ||
| 240 | while (!$rand) { | ||
| 241 | $rand = $grid[int(rand(scalar(@grid)))]; | ||
| 242 | $rand = undef if $collide{$rand->x}->{$rand->y}; | ||
| 243 | } | ||
| 244 | $food = Game::Snake::Food->new( | ||
| 245 | x => $rand->x, | ||
| 246 | y => $rand->y, | ||
| 247 | height => $cell_size, | ||
| 248 | width => $cell_size, | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | |||
| 252 | if ($collide{$food->x}->{$food->y}) { | ||
| 253 | $score += $food_score; | ||
| 254 | if ($score > $high_score) { | ||
| 255 | $high_score = $score; | ||
| 256 | } | ||
| 257 | $food = undef; | ||
| 258 | $self->extend_snake; | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | |||
| 262 | $food->draw(); | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | 1; | ||
| 269 | |||
| 270 | __END__ | ||