Snake

Snake that grows when it eats

Introduction

This is an entertaining Pascal implementation of a snake game, now available as a web program to play on the next page. The snake grows when you steer it accurately to eat the food. The sleep(50) instruction provides a reasonable level of difficulty, but you may decide to alter the number of milliseconds to suit your prowess.

This program uses the CRT unit and will run in Lazarus. In order to run it in Delphi, you will need to download and include CRT files as described in the last section of our Graphics tutorial.

The Program

program Snake;
{
    Copyright (c) 2010 Peter Hearnshaw
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
uses
  crt, sysUtils, math;
var
  charx : integer = 1;
  chary : integer = 1;
  cha : char;
  xInc : integer = 1;
  yInc : integer = 0;
  foodX, foodY, lx, SnakeLen : integer;
  myArrayX : array [1..90] of integer;
  myArrayY : array [1..90] of integer;
begin
  writeln('This program will make a snake game. However on this version you can not die.');
  writeln();
  sleep(1500);
  writeln('Use the arrow keys to move');
  writeln();
  sleep(1500);
  writeln('Press enter to start the game');
  readln();

  cursorOff;
  randomize; //This initialised the random function.
  foodX := random(79)+1;//foodX becomes a random number from 1 to 80
  foodY := random(24)+1;//foodY becomes a random number from 1 to 25
  SnakeLen := 3;//Start length of the snake
  repeat
    sleep(50);
    clrscr;
    charx := charx + xInc; //head of snake increases in X by xInc
    chary := chary + yInc; //head of snake increases in Y by yInc

    //If the snake is off the screen in any of the four sides the snake head will move to the opposite side.
    if(chary=0)then chary:=25;
    if(chary=26)then chary:=1;
    if(charX=0)then charX:=80;
    if(charX=81)then charX:=1;

    //array contains all positions the head of the snake has been on previous repeats
    //insert the new snake head value and shuffle all the other other body segment positions
    // along one and the end body segment will disappear

    for lx := 1 to SnakeLen-1 do
      begin
        myArrayX[lx] := myArrayX[lx+1];
        myArrayY[lx] := myArrayY[lx+1];
      end;
    myArrayX[SnakeLen] := charX;
    myArrayY[SnakeLen] := charY;
    //For all the values in the array add an O as a body segment.
    for lx := 1 to SnakeLen do
      begin
        gotoXY(myArrayX[lx],myArrayY[lx]);
        write('O');
      end;
    if(KeyPressed)then
      begin
        cha:=ReadKey;
        //Variable cha gives what key is pressed.
        case cha of
        //72, 80, 75, 77 happen to be the key values of the arrow keys
          #72 : begin
                  yInc := -1;
                  xInc := 0;
                  //yInc and xInc change the direction of the snake
                end;
          #80 : begin
                  yInc := 1;
                  xInc := 0;
                end;
          #75 : begin
                  xInc := -1;
                  yInc := 0;
                end;
          #77 : begin
                  xInc := 1;
                  yInc := 0;
                end;
        end; //case
      end; //if

    //foodX and foodY were set up earlier as random numbers
    //add a 9 at these positions
    gotoXY(foodX,foodY);
    write('9');
    //if the snake head is on the food position
    if(charX = foodX) and (charY = foodY) then
      begin
        //then move the food positions again
        foodX := random(79)+1;
        foodY := random(24)+1;
        //and make the snake longer
        if not(SnakeLen>89)then
          SnakeLen := SnakeLen + 1;
      end;
  until 1=2;
end.

Programming - a skill for life!

Fourteen programs (with five web versions) including 3D-Driving, GASP and Knowledge by Peter Hearnshaw