Creating A Game With PHP Part 3: Snake

10th November 2020

So far in this series of posts we have looked at detecting key presses from the user and creating a game of tic tac toe using PHP and the command line. The next step is to create a game that has graphics and real time user input. As we are using the command line we don't have much space to work with so the graphics we create aren't going to be very detailed. The simplest action game I could think of is snake. It has a few simple rules, can have very basic graphics and doesn't involve any physics or other mechanics that would effect the game as a whole. In fact the game snake dates back to the 1976 game Blockade, which was created using just text strings.

Unlike the tic tac toe, there are a few more variables to keep track of here. Instead of using pass by reference variables it makes sense to encapsulate these into an object. Using an object as the snake allows us to pass the entire thing by reference into a function and means we can encapsulate certain functionality within the object itself. This is by no means the pinnacle of good design, but more a proof of concept to see if this will work.

Instead of storing the game board as a large array, we only need to keep track of the snake, the apple and the movement of the snake. Here is the Snake class, with some default parameters that I will cover in detail later in this post.

  1. class Snake {
  2. public $width = 0;
  3. public $height = 0;
  4.  
  5. public $positionX = 0;
  6. public $positionY = 0;
  7.  
  8. public $appleX = 15;
  9. public $appleY = 15;
  10.  
  11. public $movementX = 0;
  12. public $movementY = 0;
  13.  
  14. public $trail = [];
  15. public $tail = 5;
  16.  
  17. public $speed = 100000;
  18.  
  19. public function __construct($width, $height) {
  20. $this->width = $width;
  21. $this->height = $height;
  22.  
  23. $this->positionX = rand(0, $width - 1);
  24. $this->positionY = rand(0, $height - 1);
  25.  
  26. $appleX = rand(0, $width - 1);
  27. $appleY = rand(0, $height - 1);
  28.  
  29. while (array_search([$appleX, $appleY], $this->trail) !== FALSE) {
  30. $appleX = rand(0, $width - 1);
  31. $appleY = rand(0, $height - 1);
  32. }
  33. $this->appleX = $appleX;
  34. $this->appleY = $appleY;
  35. }
  36. }

To create a new instance of the Snake object we just pass in the width and height of the game board.

  1. $width = 20;
  2. $height = 30;
  3. $snake = new Snake($width, $height);

To render the snake game we pass the snake object to a rendering function. The job of this function is to render the game grid that the snake will move around in along with the snake itself (represented by the letter X) and the apple (represented by the number 0). This function isn't exactly performant as it loops through the snake trail over and over again to see if one of the coordinates lies within the current cell being printed. However, because we are printing things in a linear pattern we can't really draw the background and then draw out the trail, we need to draw everything out at the same time. The background is essential here as it keeps everything aligned correctly. Without this in place everything would justify to the left.

  1. function renderGame($snake) {
  2. $output = '';
  3.  
  4. for ($i = 0; $i < $snake->width; $i++) {
  5. for ($j = 0; $j < $snake->height; $j++) {
  6. if ($snake->appleX == $i && $snake->appleY == $j) {
  7. $cell = '0';
  8. }
  9. else {
  10. $cell = '.';
  11. }
  12. foreach ($snake->trail as $trail) {
  13. if ($trail[0] == $i && $trail[1] == $j) {
  14. $cell = 'X';
  15. }
  16. }
  17. $output .= $cell;
  18. }
  19. $output .= PHP_EOL;
  20. }
  21.  
  22. $output .= PHP_EOL;
  23.  
  24. return $output;
  25. }

If we render a newly created snake object we see the following output. The snake only has one segment so it is printed out as a single X in the game board. The idea is that when the player moves we generate the segments until it reaches the minimal level of 5.

  1. ..............................
  2. ..............................
  3. ..............................
  4. ..............................
  5. ..............................
  6. ..............................
  7. ..............................
  8. ..............................
  9. ...X..........................
  10. ..............................
  11. ..............................
  12. ..............................
  13. ..............................
  14. ..............................
  15. ..............................
  16. ......................0.......
  17. ..............................
  18. ..............................
  19. ..............................
  20. ..............................

This doesn't do a lot, so we need to add some movement mechanics to the snake. Again, we use the translateKeypress() function from part one of this series of posts. All we are going here is to detect the arrow key being pressed and update the movement direction of the snake object.

  1. function direction($stdin, $snake) {
  2. // Listen to the button being pressed.
  3. $key = fgets($stdin);
  4. if ($key) {
  5. $key = translateKeypress($key);
  6. switch ($key) {
  7. case "UP":
  8. $snake->movementX = -1;
  9. $snake->movementY = 0;
  10. break;
  11. case "DOWN":
  12. $snake->movementX = 1;
  13. $snake->movementY = 0;
  14. break;
  15. case "RIGHT":
  16. $snake->movementX = 0;
  17. $snake->movementY = 1;
  18. break;
  19. case "LEFT":
  20. $snake->movementX = 0;
  21. $snake->movementY = -1;
  22. break;
  23. }
  24. }
  25. }

The movement direction is used to update the position of the snake to that it appears to move across the grid. For example, pressing the right arrow will set the movementY to 1, which means that the positionY value of the snake can be updated by 1 every 'tick' of the program.

The following move() function is used to move the snake across the game board using the above logic and then provides some other checks. We check to make sure that the snake hasn't gone past the boundaries of the game board and make some corrections if it has. By adding the position of the snake's head to the trail and then removing the last element from the trail array we are creating the illusion of a snake moving through the grid. We also need to check that the 'head' of the snake is over an apple, and if it is then we increase the length of the snake and move the apple to a new location. I also added a small while loop to ensure that the new position of the apple didn't sit inside the body of the snake.

  1. function move($snake) {
  2. // Move the snake.
  3. $snake->positionX += $snake->movementX;
  4. $snake->positionY += $snake->movementY;
  5.  
  6. // Wrap the snake around the boundaries of the board.
  7. if ($snake->positionX < 0) {
  8. $snake->positionX = $snake->width - 1;
  9. }
  10. if ($snake->positionX > $snake->width - 1) {
  11. $snake->positionX = 0;
  12. }
  13. if ($snake->positionY < 0) {
  14. $snake->positionY = $snake->height - 1;
  15. }
  16. if ($snake->positionY > $snake->height - 1) {
  17. $snake->positionY = 0;
  18. }
  19.  
  20. // Add to the snakes trail at the front.
  21. array_unshift($snake->trail, [$snake->positionX, $snake->positionY]);
  22.  
  23. // Remove a block from the end of the snake (but keep correct length).
  24. if (count($snake->trail) > $snake->tail) {
  25. array_pop($snake->trail);
  26. }
  27.  
  28. if ($snake->appleX == $snake->positionX && $snake->appleY == $snake->positionY) {
  29. // The snake has eaten an apple.
  30. $snake->tail++;
  31.  
  32. if ($snake->speed > 2000) {
  33. // Increase the speed of the game up to a certain limit.
  34. $snake->speed = $snake->speed - ($snake->tail * ($snake->width / $snake->height + 10));
  35. }
  36. // Figure out a different place for the apple.
  37. $appleX = rand(0, $snake->width - 1);
  38. $appleY = rand(0, $snake->height - 1);
  39. while (array_search([$appleX, $appleY], $snake->trail) !== FALSE) {
  40. $appleX = rand(0, $snake->width - 1);
  41. $appleY = rand(0, $snake->height - 1);
  42. }
  43. $snake->appleX = $appleX;
  44. $snake->appleY = $appleY;
  45. }
  46. }

The final thing to check is if the last movement caused the snake to die. This happens if the head of the snake touches its tail and can be worked out by looking at the entire trail of the snake compared to the head of the snake. We don't check for this condition at the start of the game as this would instantly create a game over situation.

If the head is found to intersect the tail then we end the game and print out a game over message.

  1. function gameOver($snake) {
  2. if ($snake->tail > 5) {
  3. // If the trail is greater than 5 then check for end condition.
  4. for ($i = 1; $i < count($snake->trail); $i++) {
  5. if ($snake->trail[$i][0] == $snake->positionX && $snake->trail[$i][1] == $snake->positionY) {
  6. die('dead :(');
  7. }
  8. }
  9. }
  10. }

With all of those functions written we can put it together into our infinite loop that controls the game. This is the same infinite loop that I introduced when creating the tic tac toe game. We start the loop by clearing the command line output, then update the direction of the snake before moving the snake and printing out the game board. Finally we then detect if a game over scenario was reached.

  1. while (1) {
  2. system('clear');
  3. echo 'Level: ' . $snake->tail . PHP_EOL;
  4. direction($stdin, $snake);
  5. move($snake);
  6. echo renderGame($snake);
  7. gameOver($snake);
  8. usleep($snake->speed);
  9. }

The main difference between this loop and the one I created for the game of tic tac toe is that I'm using usleep(). This forces the game to pause for 100000 microseconds (0.1 seconds) on every iteration through the loop and is intended to slow down the game simulation speed so that it runs about 10 frames a second. Without this in place the snake would run exceedingly fast and be impossible to actually play. You may have seen in the move() function that we reduce this value a little every time an apple is eaten. This is intended to replicate the increasing level of difficulty as the snake gets longer.

We now have a functioning game of snake, which looks like this when we get a game over screen.

  1. Level: 64
  2. ..............................
  3. ..............................
  4. .............0................
  5. ..............................
  6. ..............................
  7. ..............................
  8. XXXX.....................XXXXX
  9. ...X.....................X....
  10. ...X.....................X....
  11. ...X.....................X....
  12. ...X......X..............X....
  13. ...X......XXXXXXXXXXXXXXXX....
  14. ...X................X.........
  15. ...X................X.........
  16. ...X................X.........
  17. ...X................X.........
  18. ...X.......XXXXXXXXXX.........
  19. ...XXXXXXXXX..................
  20. ..............................
  21. ..............................
  22.  
  23. dead :(

This script works very well and is actually a fun version of snake. The included height and width settings allow the snake game board to be any size you want so you could make a very long snake on a very large board.

Some improvements that could be added might be to prevent going backwards on your trail (ie. pressing left when you are going right) causing a game over state. Secondly, some versions of snake allow a little bit of leeway before producing a game over, my version will instantly game over if you come into contact with the trail. This could be done by adding a few frames before producing the game over situation, but I'll leave this as an exercise for the reader.

If you want to see this code in full then I have created a GitHub gist that you can download and run.

Add new comment

The content of this field is kept private and will not be shown publicly.