Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Snake;
- uses Crt;
- type
- SnakeCell = record
- xCoord: Integer;
- yCoord: Integer;
- End;
- label wrongDirection;
- const
- up = 1;
- down = 2;
- left = 3;
- right = 4;
- lines = 15; {number of lines}
- rows = 20; {number of rows}
- {number of lines and rows can be changed without any restriction
- except the limits of the map[][]}
- empty = 0; //' '
- wall = 1; //#
- food = 2; //@
- clear = -1;
- snakeHead = 10; //O
- snakeBody = 11; //x
- var
- i, j, h, t, k : integer;
- map: Array[1..25,1..80] of Integer;
- isRunning : boolean;
- snakeA: Array[1..200] of SnakeCell;
- direction: integer;
- foodX, foodY: integer;
- isFood: boolean;
- points: integer;
- key : char;
- begin
- ClrScr;
- isFood := false;
- Randomize;
- points := 0;
- for i:=2 to 200 do begin
- snakeA[i].xCoord := -1;
- snakeA[i].yCoord := -1;
- end;
- {this is positioning the snake in the middle of the map}
- snakeA[1].xCoord := rows div 2;
- snakeA[1].yCoord := lines div 2;
- {this is creating the game map}
- for i:=1 to lines do begin
- for j:=1 to rows do begin
- if (i=1) OR (j = 1) OR (i = lines) OR (j = rows) then map[i][j]:=1
- else map[i][j] := 0;
- end;
- end;
- {this prints the walls and leaves the inside space empty}
- for i:=1 to lines do begin
- for j:=1 to rows do begin
- if map[i][j] = wall then write('#')
- else if map[i][j] = empty then write(' ');
- end;
- writeln();
- end;
- isRunning := true; {variable which will keep the game-loop running}
- {bringing Pascal to life }
- gotoxy(snakeA[1].xCoord, snakeA[1].yCoord);
- write ('O');
- repeat {game loop}
- wrongDirection: {this is a label for GOTO}
- if isFood = false then begin {if there is no food, create one}
- foodX := random(rows-2) + 2;
- foodY := random(lines-2) + 2;
- isFood := true;
- gotoxy(foodX, foodY);
- write ('@');
- end;
- {if a key is pressed, check if the snake can do that move
- (the direction doesn't change by 180 degrees) and if it is ok
- then change the direction}
- if Keypressed then begin
- key := readkey;
- case key of
- #75 : begin if direction = right then goto wrongDirection; direction := left; end;
- #72 : begin if direction = down then goto wrongDirection; direction := up; end;
- #77 : begin if direction = left then goto wrongDirection; direction := right; end;
- #80 : begin if direction = up then goto wrongDirection; direction := down; end;
- #27 : break;
- end;
- end;
- {erase the snake so it can be painted to the next position}
- gotoxy(snakeA[1].xCoord, snakeA[1].yCoord); write(' ');
- for t:=2 to 200 do begin
- if snakeA[t].xCoord <> -1 then begin
- gotoxy(snakeA[t].xCoord, snakeA[t].yCoord);
- write(' ');
- end
- else break;
- end;
- {compute the movement of each snake cell
- the for must go from 200 down , otherwise all the cells would
- be stacked up on the snakeA[2] position}
- {each cell will move where the one before it was}
- for j:=200 downto 2 do begin
- if snakeA[j].xCoord <> - 1 then begin
- snakeA[j].xCoord := snakeA[j-1].xCoord;
- snakeA[j].yCoord := snakeA[j-1].yCoord;
- end;
- end;
- {this is the movement of the Snake Head}
- case direction of
- up : snakeA[1].yCoord := snakeA[1].yCoord - 1;
- down : snakeA[1].yCoord := snakeA[1].yCoord + 1;
- left : snakeA[1].xCoord := snakeA[1].xCoord - 1;
- right : snakeA[1].xCoord := snakeA[1].xCoord + 1;
- end;
- {display the snake at its new position}
- gotoxy(snakeA[1].xCoord, snakeA[1].yCoord); write('O');
- for i:=2 to 200 do begin
- if snakeA[i].xCoord <> -1 then begin gotoxy(snakeA[i].xCoord,snakeA[i].yCoord);write('x');
- end
- else break;
- end;
- {check if the snakehead collided with any of its cells}
- for k:=2 to 200 do begin
- if snakeA[k].xCoord = -1 then break;
- if (snakeA[k].xCoord = snakeA[1].xCoord) and (snakeA[k].yCoord = snakeA[1].yCoord) then isRunning := false;
- end;
- {if the snake collided with the food we consider to have eaten it
- so: *there is no food on the screen anymore
- *the user gains one point
- *we find the first position in the array that isn't used and
- add a cell to the current snake}
- if (snakeA[1].yCoord = foodY) and (snakeA[1].xCoord = foodX) then begin
- isFood := false;
- points := points+1;
- for h:=2 to 200 do begin
- if snakeA[h].xCoord = -1 then begin snakeA[h].xCoord := snakeA[h-1].xCoord; snakeA[h].yCoord :=snakeA[h-1].yCoord;break;end;
- end;
- end;
- {delay(150) makes the snake move reasonably fast but not too fast}
- delay(150);
- gotoxy(1, lines+1); write('Points: ', points); //points are displayed
- {if the snakehead collided with a wall , the game is over}
- if (snakeA[1].xCoord<=1) or (snakeA[1].yCoord<=1) or (snakeA[1].xCoord >= rows) or (snakeA[1].yCoord>=lines) then isRunning := false;
- until isRunning = false;
- {the screen will be paused a little after the game was ended so the
- player can see the points}
- delay(3000);
- end. {THE END}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement