Tin học Game Rắn Săn Mồi (Sử dụng các phím mũi tên để di chuyển)

HauBright

Học sinh mới
20 Tháng tư 2023
72
36
11
14
Bến Tre
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Program SnakeGame;
Uses crt;
Const
width = 79;
height = 28;
Type
PDirection = (Up, Down, Left, Right);
PSnake = Record
HeadX, HeadY: Integer;
TailX, TailY: Array [1..100] Of Integer;
Length: Integer;
Direction: PDirection;
End;
PFood = Record
x,y: Integer;
End;
Var
Snake: PSnake;
GameOver: Boolean;
i, j, Speed, Score: Integer;
Food: PFood;

Function CheckFood(HeadX,HeadY,x,y: Integer): Boolean;
Begin
If (Snake.HeadX = Food.x) And (Snake.HeadY = Food.y) Then CheckFood := True
Else CheckFood := False;
End;
Procedure InitializeTheGame;
Begin
TextBackGround(Black);
GotoXY(31,14);
TextColor(LightBlue); Write('- THE SNAKE GAME -');
GotoXY(25,16);
Repeat
GotoXY(25,16);
TextColor(White);
WriteLn('PRESS ANY KEY TO START THE GAME!');
Delay(100);
GotoXY(25,16);
WriteLn(#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32,
#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32);
Delay(100);
Until KeyPressed;
End;
Procedure TheMap;
Var i,j: Byte;
Begin
TextColor(LightGreen);
For j := 1 To 2 Do
For i := 1 To 80 Do
Begin
GotoXY(i,(j-1)*29);
Write(Chr(219));
End;
For i := 1 To 80 Do
Begin
GotoXY(i,2);
Write(Chr(219));
GotoXY(i,3);
Write(Chr(219));
End;
For j := 1 To 2 Do
For i := 1 To 29 Do
Begin
GotoXY(1 + (j - 1) * 79,i);
Write(Chr(219));
End;
GotoXY(2,2);
TextBackGround(LightGreen);
TextColor(LightBlue); Write('SCORE: 0');
End;
Procedure DrawSnake;
Begin
TextBackGround(Black);
TextColor(Yellow);
Gotoxy(Snake.HeadX, Snake.HeadY);
Write(#219);
For i := 1 To Snake.Length Do
Begin
TextColor(White);
Gotoxy(Snake.TailX, Snake.TailY);
Write(#219);
End;
End;
Procedure MoveSnake;
Begin
For i := Snake.Length Downto 2 Do
Begin
Snake.TailX := Snake.TailX[i-1];
Snake.TailY := Snake.TailY[i-1];
End;
Snake.TailX[1] := Snake.HeadX;
Snake.TailY[1] := Snake.HeadY;
Case Snake.Direction Of
Up: Snake.HeadY := Snake.HeadY - 1;
Down: Snake.HeadY := Snake.HeadY + 1;
Left: Snake.HeadX := Snake.HeadX - 1;
Right: Snake.HeadX := Snake.HeadX + 1;
End;
If (Snake.HeadX < 2) Or (Snake.HeadX > width) Or (Snake.HeadY < 4) Or (Snake.HeadY > height) Then
GameOver := True;
For i := 1 To Snake.Length Do
If (Snake.TailX = Snake.HeadX) And (Snake.TailY = Snake.HeadY) Then
GameOver := True;
End;
Procedure GetInput;
Begin
If Keypressed Then
Case Readkey Of
#72: If Snake.Direction <> Down Then Snake.Direction := Up;
// Up arrow key
#80: If Snake.Direction <> Up Then Snake.Direction := Down;
// Down arrow key
#75: If Snake.Direction <> Right Then Snake.Direction := Left;
// Left arrow key
#77: If Snake.Direction <> Left Then Snake.Direction := Right;
// Right arrow key
End;
End;
Procedure AddTail;
Begin
Inc(Snake.Length);
Snake.TailX[Snake.Length] := Snake.TailX[Snake.Length-1];
Snake.TailY[Snake.Length] := Snake.TailY[Snake.Length-1];
End;
Procedure DrawFood(x,y: Byte);
Begin
GotoXY(Food.x,Food.y);
Write(Chr(2));
End;
Procedure MoveFood(x,y: Byte);
Begin
GotoXY(Food.x,Food.y);
Write(Chr(32));
End;
//==============================THE MAIN PROGRAM===========================
Begin
TextBackGround(Black);
Clrscr;
// Clear the screen
TheMap;
// Initialize the map for game
GotoXY(40,15);
InitializeTheGame;
// Initialize the game when begin
If KeyPressed Then
Begin
GotoXY(31,14);
Write(#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32#32);
End;
// Initialize the speed of snake
Speed := 75;
// Initialize the head of snake
Snake.HeadX := 18;
Snake.HeadY := 15;
// Initialize the tail of snake
Snake.TailX[1] := 17;
Snake.TailY[1] := 15;
Snake.TailX[2] := 16;
Snake.TailY[2] := 15;
// Initialize the length of snake
Snake.Length := 5;
// Initialize the original score
Score := 0;
// Initialize the original direction of snake
Snake.Direction := Right;
// Initiallize the position of the food
Begin
Randomize;
Food.x := Random(79);
Food.y := Random(28);
If (Food.x < 2) Then
Repeat
Food.x := Food.x + Random(5);
Until (Food.x > 2);
If (Food.x > 79) Then
Repeat
Food.x := Food.x - Random(5);
Until (Food.x < 79);
If (Food.y < 4) Then
Repeat
Food.y := Food.y + Random(5);
Until (Food.y > 4);
If (Food.y > 28) Then
Repeat
Food.y := Food.y - Random(5);
Until (Food.y < 28);
TextColor(LightRed);
TextBackGround(Black);
DrawFood(Food.x,Food.y);
End;
GameOver := False;
While Not GameOver Do
Begin
DrawSnake;
If (Snake.Direction = Up) Or (Snake.Direction = Down) Then Speed := 125; //Speed of snake when going along
If (Snake.Direction = Left) Or (Snake.Direction = Right) Then Speed := 100; //Speed of snake when going across
Delay(Speed);
GotoXY(Snake.TailX[Snake.Length],Snake.TailY[Snake.Length]);
Write(#32);
MoveSnake;
GetInput;
If CheckFood(Snake.HeadX,Snake.HeadY,Food.x,Food.y) = True Then
Begin
MoveFood(Food.x,Food.y);
Randomize;
Food.x := Random(79);
Food.y := Random(28);
If (Food.x < 2) Then
Repeat
Food.x := Food.x + Random(5);
Until (Food.x > 2);
If (Food.x > 79) Then
Repeat
Food.x := Food.x - Random(5);
Until (Food.x < 79);
If (Food.y < 4) Then
Repeat
Food.y := Food.y + Random(5);
Until (Food.y > 4);
If (Food.y > 28) Then
Repeat
Food.y := Food.y - Random(5);
Until (Food.y < 28);
Score := Score + 1;
GotoXY(9,2);
TextColor(LightBlue); TextBackGround(LightGreen);
Write(Score);
TextColor(LightRed); TextBackGround(Black);
AddTail;
DrawFood(Food.x,Food.y);
End;
End;
Gotoxy(35,14);
TextColor(LightRed);
Write('- GAME OVER -');
Readln;
End.
 

HauBright

Học sinh mới
20 Tháng tư 2023
72
36
11
14
Bến Tre
Compile trên Free Pascal thì nó thoát rồi báo là
exited with
exitcode = 201

Có tiền bối nào không giúp mik khắc phục lỗi với ạ =))
 
  • Like
Reactions: Sói_Đơn_Độc
Top Bottom