[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Arkanoid-Pascal
This is the code for an arkanoid game made in pascal. It is to suplement the article on graphics programming in Pascal
By Relman
Things to do:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Arkanoid-Pascal
This is the code for an arkanoid game made in pascal. It is to suplement the article on graphics programming in Pascal
Uses crt, Gfx3;
Type
Ball = Record
X, Y, DX, DY : Integer; {Here we need integer, unlike textgraphics because
the x and y values can be bigger then 255.
Dx, DY = The change in X and Y per frame)}
Col : Byte;
End;
Brick = Record {We will make our cursor a brick.}
X, Y : Integer;
Col : Byte;
Visible : Boolean; {If it was hit, the it wont be visible}
End;
Var
Bricks : Array[1..10,1..5] of Brick;
TheBall : Ball;
Cursor : Brick;
Quit : Boolean;
Procedure DrawBrick(Var ABrick : Brick);
Var k : Byte;
Begin
For k := 0 to 9 do {10 pixels down}
HLine(ABrick.X,ABrick.X+32,ABrick.Y+K,ABrick.Col,VGA);
End;
Procedure DrawBricks; {This only needs to be called once - at the beginning}
Var i, j, k : Integer;
Begin
For i := 1 to 10 do
For j := 1 to 5 do
DrawBrick(Bricks[i,j]);
End;
Procedure DrawCursor;
Var i : Integer;
Begin
For i := 1 to 3 do
HLine(Cursor.X, Cursor.X+20,Cursor.Y + i,Cursor.Col,VGA);
End;
Procedure DrawBall(Col : Byte);
Begin
PutPixel(TheBall.X,TheBall.Y, Col, VGA);
PutPixel(TheBall.X+1,TheBall.Y, Col, VGA);
PutPixel(TheBall.X,TheBall.Y+1, Col, VGA);
PutPixel(TheBall.X+1,TheBall.Y+1, Col, VGA); {a 2 by 2 ball}
End;
Function CheckHitBrick(Var ABrick : Brick) : boolean;
Var i, j : byte;
Temp : Boolean;
Begin
{We need to check each pixel of the ball to see if it is in the same area as
the brick. Since this brick is 2 by 2, we will use a double for loop.}
Temp := False;
For i := 0 to 1 do
for j := 0 to 1 do
If not Temp then Temp := (TheBall.X+i > ABrick.X) And (TheBall.X+i < ABrick.X+32) And
(TheBall.Y+j > ABrick.Y) And (TheBall.Y+j < ABrick.Y+10) And
(ABrick.Visible = True);
CheckHitBrick := Temp;
End;
Function CheckHitCursor(Var TheCursor : Brick) : boolean;
Var i, j : byte;
Begin
For i := 0 to 1 do
For j := 0 to 1 do
CheckHitCursor := (TheBall.X+i > TheCursor.X) And (TheBall.X+i < TheCursor.X+20) And
(TheBall.Y+j > TheCursor.Y) And (TheBall.Y+j < TheCursor.Y+5);
End;
Procedure CheckHit;
Var i, j : integer;
Begin
For i := 1 to 10 do
For j := 1 to 5 do
If CheckHitBrick(Bricks[i,j]) then
Begin
Bricks[i,j].Visible := False;
Bricks[i,j].Col := 0; {0 is generally black}
DrawBrick(Bricks[i,j]);
TheBall.DY := TheBall.DY * -1 {Flip the Verticle Direction}
End;
If CheckHitCursor(Cursor) then
Begin
TheBall.DY := TheBall.DY * -1; {Flip the Verticle Direction}
DrawCursor;
End;
If TheBall.Y > 195 Then {By the bottom of the screen}
Quit := True;
End;
Procedure MoveBall;
Begin
DrawBall(0); {Clean up}
TheBall.X := TheBall.X + TheBall.DX;
TheBall.Y := TheBall.Y + TheBall.DY;
{Check boundaries of the screen and rebound/quit as necessary}
If TheBall.X > 320 then
Begin
TheBall.X := 319;
TheBall.DX := TheBall.DX * -1;
End;
If TheBall.X < 0 then
Begin
TheBall.X := 0;
TheBall.DX := TheBall.DX * -1;
End;
If TheBall.Y > 240 then
Begin
TheBall.Y := 239;
TheBall.DY := TheBall.DY * -1;
Quit := True;
End;
If TheBall.Y < 0 then
Begin
TheBall.Y := 0;
TheBall.DY := TheBall.DY * -1;
End;
DrawBall(TheBall.Col);
DrawCursor;
End;
Procedure MoveCursor(DX : Integer);
Begin
If ((Cursor.X + DX >= 0) And (DX < 0)) Or
((Cursor.X + DX <= 300) And (DX > 0)) Then
Begin
{Clean up the old cursor}
Cursor.Col := 0;
DrawCursor;
{Move it and redraw}
Cursor.Col := 2; {If Colour 2 in the pallete represented your cursors color}
Cursor.X := Cursor.X + DX;
DrawCursor;
End;
End;
Procedure GetInput;
Var Ch : Char;
i : Byte;
{In the loop or in the procedure getinput which is called by the main loop}
Begin
If KeyPressed Then {We only want the program to stop if there was a
key pressed}
Begin
Ch := UpCase(ReadKey);
Case Ch of
'A' : MoveCursor(-10);
'D' : MoveCursor(10);
'Q' : Quit := True;
End;
End;
End;
Procedure Initialise;
Var i, j : Integer;
Begin
SetMCGA; {To Graphics mode}
{If you want, insert your new pallete colours here 0 = black, 1 = ball,
2 = Cursor, 3 to 17 = bricks. To set a colour:
SetPal(Col,Red,Green,Blue, VGA) Where VGA is a constant (Already defined)
Col is the number of the colour
Red,Green,Blue are the RGB components of the colour}
Pal(0,0,0,0); {Colour 0 = Black}
Pal(1,255,0,0); {Colour 1 = Red}
Pal(2,0,255,0); {Colour 2 = Green}
For i := 1 to 10 Do
For j := 1 to 5 Do
Begin
Bricks[i,j].X := (i - 1) * 32;
Bricks[i,j].Y := (j - 1) * 10;
Bricks[i,j].Col := j+i+2; {Colours 3 to 17 - for variety}
Bricks[i,j].Visible := True;
End;
TheBall.X :=160;
TheBall.Y :=100;
TheBall.Col := 1;
TheBall.DX := 1;
TheBall.DY := -1; {Downwards}
Cursor.X := 100;
Cursor.Y := 190;
Cursor.Col := 2;
DrawBricks;
DrawBall(TheBall.Col);
DrawCursor;
End;
Var i : Integer;
Begin
Initialise;
Repeat {Main Loop}
MoveBall;
For i := 1 to 20 Do {Allow the user up to 20 moves between a ball move}
Begin
GetInput; {This procedure contains your ch = readkey; and the case
statement}
End;
Delay(10); {That is 0.01 seconds}
CheckHit;
Until Quit = true;
SetText {Back to textmode}
End.
By Relman
Things to do:
- Keep Score
- End the game when there are no bricks left to get
- (Based on the mouse use article) Control cursor with the mouse.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
