-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.pas
217 lines (191 loc) · 4.74 KB
/
snake.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
uses GraphABC, Events, Timers;
type TPoint = packed record
public
X: integer;
Y: integer;
constructor Create(x0, y0: integer);
begin
X := x0; Y := y0;
end;
end;
type TWindow = packed record
public
X: integer;
Y: integer;
blockSize: integer;
constructor Create(x0, y0: integer; bs: integer);
begin
X := round(x0 / bs) - 1;
Y := floor(y0 / bs) - 1;
blockSize := bs;
SetWindowSize(x0, y0);
end;
procedure drawPixel(x, y: integer; c: Color);
begin
SetBrushColor(c);
FillRectangle(x * blockSize, y * blockSize, (x *blockSize) + blockSize, (y * blockSize) + blockSize);
// DrawRectangle(x * blockSize, y * blockSize, (x *blockSize) + blockSize, (y * blockSize) + blockSize);
end;
procedure drawPixel(point: TPoint; c: Color);
begin
drawPixel(point.X, point.Y, c);
end;
end;
type TKeyboard = auto class
public
const KEY_UP = 'w';
const KEY_DOWN = 's';
const KEY_LEFT = 'a';
const KEY_RIGHT = 'd';
key: Char;
lastPressKey: Char;
constructor Create(last: Char);
begin
lastPressKey := last;
onKeyPress := _onKeyPress;
end;
private
procedure _onKeyPress(key: Char);
begin
if (key = KEY_UP) or (key = KEY_DOWN) or (key = KEY_RIGHT) or (key = KEY_LEFT) then begin
lastPressKey := key;
end;
end;
end;
type TSnake = packed record
private
len: integer;
die: boolean;
body: array[0..10000] of TPoint;
public
constructor Create(p: TPoint);
begin
len := 0;
body[len] := p;
end;
function isDie(): boolean;
begin
Result := die;
end;
function GetTail(): TPoint;
begin
Result := body[0];
end;
function GetHeader(): TPoint;
begin
Result := body[len];
end;
procedure Append(p: TPoint);
begin
inc(len);
body[len] := p;
end;
procedure Move(h: TPoint);
begin
for var i:=0 to len do begin
body[i] := body[i + 1 ];
end;
body[len] := h;
end;
procedure KeyPress(c: Char);
var p: TPoint;
begin
p := getHeader();
case c of
TKeyboard.KEY_UP: p := TPoint.Create(p.X, p.Y - 1);
TKeyboard.KEY_DOWN: p := TPoint.Create(p.X, p.Y + 1);
TKeyboard.KEY_LEFT: p := TPoint.Create(p.X - 1, p.Y);
TKeyboard.KEY_RIGHT: p := TPoint.Create(p.X + 1, p.Y);
end;
if Cross(p) then die := True else Move(p);
end;
function Cross(p: TPoint): Boolean;
begin
Result := False;
for var i := 0 to len do begin
if p.Equals(body[i]) then begin
Result := True;
end;
end;
end;
end;
type TScene = auto class
public
window: TWindow;
eat: TPoint;
snake: TSnake;
keyboard: TKeyboard;
gameTimer: Timer;
constructor Create(w: TWindow; s: TSnake; k: TKeyboard);
begin
window := w;
keyboard := k;
snake := s;
updateEat();
end;
procedure updateEat();
begin
repeat
eat := TPoint.Create(random(0, window.X),random(0, window.Y));
until not snake.Cross(eat);
end;
procedure checkMirrov();
begin
if snake.GetHeader().Y > window.Y then begin
snake.Move(TPoint.Create(snake.GetHeader().X, 0));
end;
if snake.GetHeader().Y < 0 then begin
snake.Move(TPoint.Create(snake.GetHeader().X, window.Y));
end;
if snake.GetHeader().X > window.X then begin
snake.Move(TPoint.Create(0, snake.GetHeader().Y));
end;
if snake.GetHeader().X < 0 then begin
snake.Move(TPoint.Create(window.X, snake.GetHeader().Y));
end;
end;
procedure update();
begin
if not snake.isDie() then begin
window.drawPixel(eat, clred);
window.drawPixel(snake.GetTail(), clwhite);
snake.KeyPress(keyboard.lastPressKey);
checkMirrov();
if snake.GetHeader().Equals(eat) then begin
snake.Append(eat);
updateEat();
end;
window.drawPixel(snake.GetHeader(), clgreen);
end else begin
die();
end;
end;
procedure die();
begin
writeln('Вы проиграли!');
writeln('Счёт:', snake.len);
gameTimer.Stop();
end;
procedure run(speed: integer);
begin
gameTimer := Timer.Create(speed, update);
gameTimer.Start();
end;
end;
procedure snakeGame();
var
window: TWindow;
keyboard: Tkeyboard;
scene: TScene;
snake: TSnake;
begin
randomize;
window := TWindow.Create(500, 500, 20);
keyboard := TKeyboard.Create(TKeyboard.KEY_DOWN);
snake := TSnake.Create(TPoint.Create(10, 10));
scene := TScene.Create(window, snake, keyboard);
scene.run(150);
end;
begin
snakeGame();
end.