SnakeWithoutATail (Version 3)

by Joe: Y9 Age ~14

Version 3 of SnakeWithoutATail makes better use of objects and has excellent init routines with many parameters. This would be a suitable starting point for for experimenting with inheritance, since the moving objects (snake and enemies) have much in common. Toggling speed with the space bar is not possible in this version.

We have commented out the music code so that the program will run on the Raspberry Pi. The cursoroff routine does not work on the Pi, but the cursor is positioned to highlight the first digit of the score:

Snake on the Pi Snake on the Pi
program SnakeWithoutATail;
//Version 3
{
    Copyright (c) 2012 Joe

    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/
}
{$mode objfpc}{$H+}

uses
  SysUtils, crt
  //,MMsystem, windows
  ;
const
  MaxX = 80;
  MaxY = 24;
  maxCounter = 100;

type
  player = object
  public
    x : integer;
    y : integer;
    name : string;
    playertimer : integer;
    playerspeed : integer;
    procedure drawHim;
    procedure moveHim;
    procedure init(startx : integer; starty : integer; startname : string;
    startspeed : integer);
  end;

procedure player.drawHim;
begin
  gotoxy(x, y);
  write(name);
end;

procedure player.moveHim;
begin
  playertimer := playertimer + playerspeed;
  if playertimer > maxCounter then
    begin
      if x < 1 then
        x := MaxX;
      if y < 1 then
        y := MaxY;
      if x > MaxX then
        x := 1;
      if y > MaxY then
        y := 1;

      playertimer := 0;
    end;
end;

procedure player.init(startx : integer; starty : integer; startname : string;
                      startspeed : integer);
begin
  x := startx;
  y := starty;
  name := startname;
  playertimer := 0;
  playerspeed := startspeed;
end;


type
  fruit = object
  public
    x : integer;
    y : integer;
    name : string;
    procedure init(startx : integer; starty : integer; startname : string);
    procedure drawMe;
  end;

procedure fruit.drawMe;
begin
  gotoxy(x, y);
  write(name);
end;

procedure fruit.init(startx : integer; starty : integer; startname : string);
begin
  x := startx;
  y := starty;
  name := startname;
end;

type
  enemy = object
  public
    x : integer;
    y : integer;
    name : string;
    direction : string;
    enemytimer : integer;
    enemyspeed : integer;
    procedure moveMe;
    procedure init(startx : integer; starty : integer; startspeed : integer;
                   startname : string; startdirection : string);
    procedure drawMe;
  end;

procedure enemy.moveMe;
begin
  enemytimer := enemytimer + enemyspeed;
  //changed for testing
  if enemytimer > 0 then
    begin
      if direction = 'right' then
        x := x + 1;
      if direction = 'left' then
        x := x - 1;
      if direction = 'up' then
        y := y - 1;
      if direction = 'down' then
        y := y + 1;

      if x < 1 then
        x := MaxX;
      if y < 1 then
        y := MaxY;
      if x > MaxX then
        x := 1;
      if y > MaxY then
        y := 1;

      enemytimer := 0;
    end;
end;

procedure enemy.init(startx : integer; starty : integer; startspeed : integer;
                     startname : string; startdirection : string);
begin
  x := startx;
  y := starty;
  enemytimer := 0;
  enemyspeed := startspeed;
  name := startname;
  direction := startdirection;
end;

procedure enemy.drawMe;
begin
  gotoxy(x, y);
  write(name);
end;

var
  c, oldC : char;
  repeatend, scr : integer;
  player1 : player;
  enemy1 : enemy;
  enemy2 : enemy;
  enemy3 : enemy;
  enemy4 : enemy;
  fruit1 : fruit;


procedure endgame;
begin
  repeatend := 1;
 // PlaySound('sounds/kablosh.wav', 0, SND_ASYNC);
  clrscr;
  gotoxy(30, 12);
  write('GAME OVER');
  gotoxy(28, 14);
  write('Your score was');
  gotoxy(45, 14);
  write(scr);
  gotoxy(26, 16);
  write('Press r to retry or Esc to quit');
  repeat
    if (keypressed = True) then
       begin
         c := readkey;
       end;
    case c of
      #27 : halt;
      'r' : begin
              scr := 0;
              repeatend := repeatend + 1;
              player1.x := Random(MaxX) + 1;
              player1.y := Random(MaxY) + 1;
            end;
    end;
  until repeatend = 2;
end;

procedure game;
begin
  enemy1.init(79, 1, 44, 'G', 'right');
  enemy2.init(1, 10, 44, 'V', 'up');
  enemy3.init(79, 18, 44, 'H', 'down');
  enemy4.init(40, 16, 44, 'J', 'left');
  player1.init(10, 10, '#', 33);
  fruit1.init(15, 15, '&');
  scr := 0;
  textbackground(green);
  textcolor(white);
  clrscr;
  cursoroff;

  writeln('WELCOME TO SNAKE WITHOUT A TAIL BECAUSE I HAVEN''T FINISHED IT YET!');
  writeln('Controls ');
  writeln('Press w,a,s,d to run');
  writeln('Eat the fruit and avoid enemies to win');
  writeln('Press any button to continue');
  readkey;
  clrscr;

  repeat
    if keypressed = True then
      begin
        oldC := c;
        c := readkey;
      end;
    clrscr;

    // Controls
    case c of
      'a' : dec(player1.x);
      'd' : inc(player1.x);
      'w' : dec(player1.y);
      's' : inc(player1.y);
      #27 : halt;

      else
        c := oldC;
    end;

    enemy1.moveMe;
    enemy2.moveMe;
    enemy3.moveMe;
    enemy4.moveMe;
    enemy1.drawMe;
    enemy2.drawMe;
    enemy3.drawMe;
    enemy4.drawMe;

    player1.moveHim;
    player1.drawHim;

    fruit1.drawMe;

    gotoxy(10, 2);
    write(scr);

    gotoxy(4, 2);
    write('Score:');

    if (player1.x = enemy1.x) and (player1.y = enemy1.y) or
       (player1.x = enemy2.x) and (player1.y = enemy2.y) or
       (player1.x = enemy3.x) and (player1.y = enemy3.y) or
       (player1.x = enemy4.x) and (player1.y = enemy4.y) then
      begin
        endgame;
      end;

    if (player1.x = fruit1.x) and (player1.y = fruit1.y) then
      begin
        fruit1.x := Random(MaxX) + 1;
        fruit1.y := Random(MaxY) + 1;
        scr := scr + 10;
      end;
    sleep(120);
  until False;
end;

begin
  Randomize;
  game;
end.

Remarks

Can you add a tail to the snake?

Programming - a skill for life!

by Joe: Y8 Age ~13