SnakeWithoutATail
by Joe: Y8 Age ~13
Introduction
Joe is currently our youngest contributor, but he does acknowledge help from his father. Use the wasd keys to guide the snake to the fruit and gain ten points for each fruit that you eat. Toggle your speed between slow and fast with the space bar. We like the code and advise beginners to have a good look at it to see how to move a character around the screen. The program uses the Crt unit so it is easier to try it in Lazarus than in Delphi. You need to insert your own code if you want to display your score (scr).

Start of game
Version 1
program SnakeWithoutATail; //Version 1.0 { 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/ } uses sysutils, crt; const MaxX = 79; MaxY = 24; SlowSpeed = 120; FastSpeed = 60; x = 10; var c, oldC : char; playerX, playerY, oldplayerX, oldplayerY, speed, fruitY, fruitX, scr : integer; procedure game; begin fruitX := 8; fruitY := 9; playerX := 10; playerY := 10; speed := slowspeed; scr := 0; oldplayerX := playerX; oldplayerY := playerY; textBackground(green); textColor(blue); clrscr; cursoroff; Randomize; //This is the start screen. writeln('Welcome To The Game'); writeln('Instructions'); writeln('Eat fruit'); writeln('W - Up'); writeln('S - Down'); writeln('D - Right'); writeln('A - Left'); writeln('Space - toggle run or walk'); writeln('ESC - End'); writeln('Press any key to begin'); readkey; clrscr; //Draw initial screen position gotoxy(playerX, playerY); writeln('#'); repeat if keypressed = true then begin oldC := c; c := readkey; end; //Controls case c of 'a' : dec(playerX); 'd' : inc(playerX); 'w' : dec(playerY); 's' : inc(playerY); #27 : halt; ' ' : begin if speed = SlowSpeed then speed := FastSpeed else speed := SlowSpeed; c := oldC; end; else c := oldC; end; //drawing fruit gotoxy(fruitX, fruitY); write('&'); if playerX < 1 then playerX := MaxX; if playerY < 1 then playerY := MaxY; if playerX > MaxX then playerX := 1; if playerY > MaxY then playerY := 1; if (playerX <> oldplayerX) or (playerY <> oldPlayerY) then begin //Delete old snake. gotoxy(oldplayerX, oldplayerY); write(' '); //Write new snake. gotoxy(playerX, playerY); write('#'); //Remember old snake coordinates. oldplayerX := playerX; oldplayerY := playerY; end; //drawing fruit gotoxy(fruitX, fruitY); write('&'); sleep(speed); //code for collecting fruit if (playerX = fruitX) and (playerY = fruitY) then begin fruitX := Random(MaxX) + 1; fruitY := Random(MaxY) + 1; scr := scr + x; end; until false; end; begin Randomize; game end.
Remarks
Can you add a tail to the snake?