[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
TextGraphics-Pascal
The first question you might ask is: "Why would I try to make a game using text graphics?" The answer is: It is easy, and gets the message across for some games. An example of a game that uses Text Graphics quite well (If I may say so myself) is Snake
To understand using text as your form of graphics in pascal, you must understand the Co-Ordinate system which pascal uses to draw to the screen. The Dos screen is made up of 25 rows and 80 columns. In each Co-Ordinate, you can write a charachter. What we would like to do is go to any co-ordinate and write a charachter there. The procedure used in order to do this is "GoToXY([XCoOrd],[YCoOrd]);". Then we do a "write();" statement. to put the charachter there.
Just a quick definition before we continue:
Now that we have our type for our snake, we can work out a procedure to draw it. To draw it, we will go from head to tail (Co-Ordinates 1 to length) and draw it. First we draw the snakes head "H", then its body "B" then its Tail "T" and then clean up from the last time it moved (To be explained). Here is some code:
First I am going to explain how to move our snake and then why we need to clean up. To move our snake, we need to determine its current direction and then change the snakes current XY based on that. To move the head by itself is easy, eg. to move it right we just increase the X. But to move the body nedds a bit more work. The basic idea is that every part of the snake takes the place of the part in front of it. Here is where we hit a problem. If use a "for loop" going from its head to its tail, we will end up moving every part of thes snake into one location. Let me show you what I mean: Say the snake's head is at [5,5] and parts at [5,4],[4,4],[3,4],[3,5] and its tail at [3,6]. If we do a straight for loop moving the head to [5,6] then putting the second part to where the head is and then the third to where the second is and so on and so forth, this is what will happen locations wise: 2nd to where head is: [5,4] => [5,6] (The head has moved), then 3rd to 2nd [4,4] to [5,6] (the second has also jus changed). Therefore, what we will end up doing is setting every part of the snake to that position [5,6], which is not what we want to do... (If you didn't get that, use a for loop and see what happens, or, just trust me
)
What we need to do is move the tail to where the second last part is (Since we haven't moved it yet, we put the tail where the second last part currently is ie. tail now = [3,5]) and then the second last part to where the third last part is [3,5] to [3,6] and so on and so forth until we get to the head which we assign to where we now want the head to move. Before I show some code, I must remind you that the snake, if not given any input, continues in the same direction as it was in. Now, for some code:
You can also edit the input procedure shown in First Steps to work for your snake game adding keys for direction. For example:
We also need an Initialise procedure to start off oursnake:
And the last thing needeed is to add a short delay between refreshes in order to give the player time to respond.
There, we have now created a game of snake with text graphics! (Things you can add: Walls, Mazes, What happens if it crashes?)
To add things like walls, you need to add collision detection. What you do is that whenever the snake moves, you will make a "for loop" that iterates through the locations that make up the wall and if the snake's location is = to the wall's location at any point, the snake has crashed.
These can also be used to create a game of arkanoid. The types will include the ball (X, Y, deltaX, deltaY) and the bricks (Also X, Y) and the "cursor" (X, Y). Look at my article on programming games with graphics for a full explanation.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
TextGraphics-Pascal
The first question you might ask is: "Why would I try to make a game using text graphics?" The answer is: It is easy, and gets the message across for some games. An example of a game that uses Text Graphics quite well (If I may say so myself) is Snake
To understand using text as your form of graphics in pascal, you must understand the Co-Ordinate system which pascal uses to draw to the screen. The Dos screen is made up of 25 rows and 80 columns. In each Co-Ordinate, you can write a charachter. What we would like to do is go to any co-ordinate and write a charachter there. The procedure used in order to do this is "GoToXY([XCoOrd],[YCoOrd]);". Then we do a "write();" statement. to put the charachter there.
Just a quick definition before we continue:
- Sprite: A sprite is the thing that you control in a game. It is that image moving across the screen. It is the Snake in a game of snake.
- The objects current location (X, Y co-ordinate) for drawing purposes.
- Any other info that is required. (Different from game to game).
Type
CoOrd = Record
X, Y : Byte;
End;
Snake = Record
Parts : Array[1..100] of CoOrd; {Where is every part of the snake?}
Length : Byte; {What is the current length of our snake?}
End;
Var MySnake : Snake; {Just define it for the rest of this article}
Now that we have our type for our snake, we can work out a procedure to draw it. To draw it, we will go from head to tail (Co-Ordinates 1 to length) and draw it. First we draw the snakes head "H", then its body "B" then its Tail "T" and then clean up from the last time it moved (To be explained). Here is some code:
Procedure DrawSnake;
Var i : Byte; {It doesn't need to be an integer - the snake can only be up
to 100 parts in length}
Begin
GotoXY(MySnake.Parts[1].X, MySnake.Parts[1].Y); {parts[1] = The head}
Write('H');
For i := 2 to MySnake.Length - 2 Do {The last position being the spot
behind the Snake}
Begin
GotoXY(MySnake.Parts[i].X, MySnake.Parts[i].Y);
Write('B');
End;
{Go to the position of the tail - the second last position drawn}
GotoXY(MySnake.Parts[MySnake.Length-1].X, MySnake.Parts[MySnake.Length-1].Y);
Write('T');
GotoXY(MySnake.Parts[MySnake.Length].X, MySnake.Parts[MySnake.Length].Y);
Write(' '); {Draw " " behind the snake - clean up the old "Tail".}
End;
First I am going to explain how to move our snake and then why we need to clean up. To move our snake, we need to determine its current direction and then change the snakes current XY based on that. To move the head by itself is easy, eg. to move it right we just increase the X. But to move the body nedds a bit more work. The basic idea is that every part of the snake takes the place of the part in front of it. Here is where we hit a problem. If use a "for loop" going from its head to its tail, we will end up moving every part of thes snake into one location. Let me show you what I mean: Say the snake's head is at [5,5] and parts at [5,4],[4,4],[3,4],[3,5] and its tail at [3,6]. If we do a straight for loop moving the head to [5,6] then putting the second part to where the head is and then the third to where the second is and so on and so forth, this is what will happen locations wise: 2nd to where head is: [5,4] => [5,6] (The head has moved), then 3rd to 2nd [4,4] to [5,6] (the second has also jus changed). Therefore, what we will end up doing is setting every part of the snake to that position [5,6], which is not what we want to do... (If you didn't get that, use a for loop and see what happens, or, just trust me
What we need to do is move the tail to where the second last part is (Since we haven't moved it yet, we put the tail where the second last part currently is ie. tail now = [3,5]) and then the second last part to where the third last part is [3,5] to [3,6] and so on and so forth until we get to the head which we assign to where we now want the head to move. Before I show some code, I must remind you that the snake, if not given any input, continues in the same direction as it was in. Now, for some code:
Procedure MoveSnake;
Var i : Byte;
Begin
For i := MySnake.Length downto 2 do
{We also want to move the marker spot behind the snake to where the tail
is in order that we can clean it up on the next draw procedure}
Begin
MySnake.Parts[i].X = MySnake.Parts[i-1].X
MySnake.Parts[i].Y = MySnake.Parts[i-1].Y
End;
Case MySnake.Direction of {Add this to the Type "Snake"} of
1 : Inc(MySnake.Parts[1].X); {Right} {You should add error checking to
make sure the snake can move in it direction. Eg. if it is at the
right edge of the screen, it can't move more right. Add an If Statement.}
2 : Dec(MySnake.Parts[1].X); {Left}
3 : Inc(MySnake.Parts[1].Y); {Down}
4 : Dec(MySnake.Parts[1].Y); {Up}
End;
DrawSnake;
End;
And Waddayaknow, we know have a snake that moves. Now that it moves, we need to make sure that between moves it cleans itself up - hence our " "; (Try it without the " ", maybe change it to "a" and it will draw a trail behind the snake)You can also edit the input procedure shown in First Steps to work for your snake game adding keys for direction. For example:
'D' : Direction := 1; {Right}
'A' : Direction := 2; {Left}
'S' : Direction := 3; {Down}
'W' : Direction := 4; {Up}
We also need an Initialise procedure to start off oursnake:
Procedure Initialise;
Var i : Byte;
Begin
For i := 1 to 4 do
Begin
MySnake.Parts[i].X := i + 25; {Start the snake at 26 X}
MySnake.Parts[i].X := i + 10; {Start the snake at 11 Y}
End;
MySnake.Direction := 2; {Left}
MySnake.Length := 4; {Part 4 is the clean up part}
End;
And the last thing needeed is to add a short delay between refreshes in order to give the player time to respond.
There, we have now created a game of snake with text graphics! (Things you can add: Walls, Mazes, What happens if it crashes?)
To add things like walls, you need to add collision detection. What you do is that whenever the snake moves, you will make a "for loop" that iterates through the locations that make up the wall and if the snake's location is = to the wall's location at any point, the snake has crashed.
These can also be used to create a game of arkanoid. The types will include the ball (X, Y, deltaX, deltaY) and the bricks (Also X, Y) and the "cursor" (X, Y). Look at my article on programming games with graphics for a full explanation.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
