• Sign Up
• Login
• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Pascal the Snake

TwoOfDiamonds Apr 23rd, 2013 404 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. program Snake;
2. uses Crt;
3.
4. type
5.         SnakeCell = record
6.                 xCoord: Integer;
7.                 yCoord: Integer;
8.         End;
9.
10. label wrongDirection;
11.
12. const
13.         up = 1;
14.         down = 2;
15.         left = 3;
16.         right = 4;
17.
18.         lines = 15; {number of lines}
19.         rows = 20; {number of rows}
20.
21.         {number of lines and rows can be changed without any restriction
22.         except the limits of the map[][]}
23.
24.         empty = 0;         //' '
25.         wall = 1;          //#
26.         food = 2;          //@
27.         clear = -1;
28.
29.         snakeHead = 10; //O
30.         snakeBody = 11; //x
31.
32. var
33.         i, j, h, t, k : integer;
34.         map: Array[1..25,1..80] of Integer;
35.         isRunning : boolean;
36.         snakeA: Array[1..200] of SnakeCell;
37.         direction: integer;
38.         foodX, foodY: integer;
39.         isFood: boolean;
40.         points: integer;
41.
42.         key : char;
43. begin
44.
45.
46. ClrScr;
47. isFood := false;
48. Randomize;
49. points := 0;
50.
51. for i:=2 to 200 do begin
52.         snakeA[i].xCoord := -1;
53.         snakeA[i].yCoord := -1;
54. end;
55.
56. {this is positioning the snake in the middle of the map}
57.
58. snakeA[1].xCoord := rows div 2;
59. snakeA[1].yCoord := lines div 2;
60.
61. {this is creating the game map}
62. for i:=1 to lines do begin
63.         for j:=1 to rows do begin
64.                 if (i=1) OR (j = 1) OR (i = lines) OR (j = rows) then map[i][j]:=1
65.                 else map[i][j] := 0;
66.          end;
67. end;
68.
69. {this prints the walls and leaves the inside space empty}
70.
71. for i:=1 to lines do begin
72.         for j:=1 to rows do begin
73.                 if map[i][j] = wall then write('#')
74.                 else if map[i][j] = empty then write(' ');
75.         end;
76.         writeln();
77.
78. end;
79.
80.
81. isRunning := true; {variable which will keep the game-loop running}
82.
83. {bringing Pascal to life }
84.
85. gotoxy(snakeA[1].xCoord, snakeA[1].yCoord);
86. write ('O');
87.
88. repeat                                 {game loop}
89.
90.         wrongDirection:      {this is a label for GOTO}
91.
92.         if isFood = false then begin       {if there is no food, create one}
93.                 foodX := random(rows-2) + 2;
94.                 foodY := random(lines-2) + 2;
95.                 isFood := true;
96.                 gotoxy(foodX, foodY);
97.                 write ('@');
98.         end;
99.
100.         {if a key is pressed, check if the snake can do that move
101.         (the direction doesn't change by 180 degrees) and if it is ok
102.         then change the direction}
103.
104.         if Keypressed then begin
105.                 key := readkey;
106.
107.                 case key of
108.                         #75 : begin if direction = right then goto wrongDirection; direction := left; end;
109.                         #72 : begin if direction = down then goto wrongDirection; direction := up; end;
110.                         #77 : begin if direction = left then goto wrongDirection; direction := right; end;
111.                         #80 : begin if direction = up then goto wrongDirection; direction := down; end;
112.                         #27 : break;
113.                 end;
114.         end;
115.
116.         {erase the snake so it can be painted to the next position}
117.
118.                 gotoxy(snakeA[1].xCoord, snakeA[1].yCoord); write(' ');
119.                 for t:=2 to 200 do begin
120.                         if snakeA[t].xCoord <> -1 then begin
121.                                 gotoxy(snakeA[t].xCoord, snakeA[t].yCoord);
122.                                 write(' ');
123.                         end
124.                         else break;
125.                 end;
126.
127.                 {compute the movement of each snake cell
128.                 the for must go from 200 down , otherwise all the cells would
129.                 be stacked up on the snakeA[2] position}
130.
131.                 {each cell will move where the one before it was}
132.
133.                 for j:=200 downto 2 do begin
134.                         if snakeA[j].xCoord <> - 1 then begin
135.                                 snakeA[j].xCoord := snakeA[j-1].xCoord;
136.                                 snakeA[j].yCoord := snakeA[j-1].yCoord;
137.                                 end;
138.                 end;
139.
140.                 {this is the movement of the Snake Head}
141.
142.                 case direction of
143.                         up : snakeA[1].yCoord := snakeA[1].yCoord - 1;
144.                         down : snakeA[1].yCoord := snakeA[1].yCoord + 1;
145.                         left : snakeA[1].xCoord := snakeA[1].xCoord - 1;
146.                         right : snakeA[1].xCoord := snakeA[1].xCoord + 1;
147.                 end;
148.
149.                 {display the snake at its new position}
150.
151.                 gotoxy(snakeA[1].xCoord, snakeA[1].yCoord); write('O');
152.                 for i:=2 to 200 do begin
153.                         if snakeA[i].xCoord <> -1 then begin gotoxy(snakeA[i].xCoord,snakeA[i].yCoord);write('x');
154.                         end
155.                         else break;
156.                 end;
157.
158.                 {check if the snakehead collided with any of its cells}
159.
160.                 for k:=2 to 200 do begin
161.                         if snakeA[k].xCoord = -1 then break;
162.                         if (snakeA[k].xCoord = snakeA[1].xCoord) and (snakeA[k].yCoord = snakeA[1].yCoord) then isRunning := false;
163.                 end;
164.
165.                 {if the snake collided with the food we consider to have eaten it
166.                 so: *there is no food on the screen anymore
167.                 *the user gains one point
168.                 *we find the first position in the array that isn't used and
169.                 add a cell to the current snake}
170.
171.                 if (snakeA[1].yCoord = foodY) and (snakeA[1].xCoord = foodX) then begin
172.                         isFood := false;
173.                         points := points+1;
174.                         for h:=2 to 200 do begin
175.                                 if snakeA[h].xCoord = -1 then begin snakeA[h].xCoord := snakeA[h-1].xCoord; snakeA[h].yCoord :=snakeA[h-1].yCoord;break;end;
176.
177.                         end;
178.                 end;
179.
180.                 {delay(150) makes the snake move reasonably fast but not too fast}
181.
182.                 delay(150);
183.                 gotoxy(1, lines+1); write('Points: ', points);  //points are displayed
184.
185.                 {if the snakehead collided with a wall , the game is over}
186.
187.                 if (snakeA[1].xCoord<=1) or (snakeA[1].yCoord<=1) or (snakeA[1].xCoord >= rows) or (snakeA[1].yCoord>=lines) then isRunning := false;
188.
189.
190.         until isRunning = false;
191.
192.         {the screen will be paused a little after the game was ended so the
193.         player can see the points}
194.
195.         delay(3000);
196.
197.
198. end.     {THE END}
RAW Paste Data
Top