ASCII Game Primer

Creating ASCII games is easier than making 2d games and they are a lot of fun to program. This tutorial is a primer of the very basic concepts of making ASCII games. You will find this useful later when you begin to make 2d games.

ASCII Game Primer

Much like text games, ASCII games do not seem to very popular anymore — except among amateur game programmers. Learning to program ASCII games is an easy way to get a grasp on fundamental 2d game programming concepts. In this tutorial, you will learn the very basics of what you need to make an ASCII game in Basic4GL.

The game code is kept very simple and you will learn how to move a game object (in this case, an x around the screen using the arrow keys).

The Game Code

I'm going to give you the game code right away and then explain it part-by-part. You should go ahead and copy and paste this into the Basic4GL IDE and test it out. While you are testing, remember that even if you are running with the full screen option checked, you can end the game at any time by pressing the ESCAPE key.

Dim x
Dim y
x = 25
y = 25
TextMode(TEXT_BUFFERED)
SetTextScroll(FALSE)
ResizeText(50,50)

Do 

    If ScanKeyDown(VK_LEFT) Then x = x - 1 EndIf
    If ScanKeyDown(VK_RIGHT) Then x = x + 1 EndIf    
    If ScanKeyDown(VK_UP) Then y = y - 1 EndIf
    If ScanKeyDown(VK_DOWN) Then y = y + 1 EndIf

    Cls
    Locate x,y
    Print "x"
    DrawText()
    Sleep(32)
Loop

Initializing the Game

First, we need to set up everything to get our game going. Naturally, this means creating the variables our game will need and setting up anything else we will need.

Dimming Variables

For this simple ASCII game example, we only need two variables x and y.

  • x is the horizontal location of the on-screen x
  • y is the vertical location of the on-screen x

x and y are created with Dim, then both of them get set to 25, which puts its starting position vaguely in the center of the screen — 25 characters from the left side of the screen and 25 from the top.

Dim x
Dim y
x = 25
y = 25

Setting up the Environment

Next follows three functions that probably haven't been covered in previous tutorials. This is because they are mostly used for creating ASCII games.

TextMode(TEXT_BUFFERED)
SetTextScroll(FALSE)
ResizeText(50,50)

TextMode()

We want our ASCII game to be relatively fast, but using Print as many times as we need in such a game is slow. Therefore, a special text mode is used. The text mode is set by the function TextMode() and the text mode we are using is TEXT_BUFFERED.

TextMode(TEXT_BUFFERED)

TEXT_BUFFERED makes ASCII games faster by allowing you to display all the changes to the screen all at once. While using it, anytime you use Print or Printr the text is not immediately displayed. Instead, they are held in a buffer until you use DrawText(), which displays everything in one shot.

SetTextScroll()

Up until now, you've probably been making only text games. In text games, the text will scroll upwards when text is displayed on the last line. This is great for text games, as it makes room for new text so the game can continue. For ASCII games, this is absolutely terrible. If the text scrolled like this, every time the x reached the bottom of the screen, it would be displaced a few lines.

We don't want that, so we tell Basic4GL that we do not want it to scroll the text using SetTextScroll(). The line below turns text scrolling off.

SetTextScroll(FALSE)

However, if you wanted to turn it back on, for some scrolling ending text, for example, all you would have to do is turn it back on again.

SetTextScroll(TRUE)

ResizeText()

By default, the Basic4GL screen is 40 characters wide and 25 characters tall. Sometimes this just isn't enough for a good ASCII game. We can give our game more space by using ResizeText(). The code below sets the screen to 50 characters wide and 50 characters tall.

ResizeText(50,50)

You can give your game more space by setting the character height and width to 100 by 100.

ResizeText(50,50)

But remember, you want to keep the text readable so the player can read the messages you display. Also, the smaller the letters, the slower Basic4GL will run. Therefore, I suggest 50x50 as a maximum "character resolution."

The Game Loop

The game loop (the part between Do and Loop has two sections. The first part handles input from the player and the second displays the x on the screen.

Keyboard Control

If you've worked your way up through you text game programming tutorials then up until now you've dealt with Input and possibly (depending on how many tutorials you read) Inkey$. In ASCII games, we almost always use ScanKeyDown(), which is a completely new way of getting input.

Like Inkey$ it gets input in real time. Unlike Inkey, ScanKeyDown() doesn't rely on the keyboard buffer. Instead, it gets input directly from a specified key the moment it is pressed down.

You can specify which key to check by using a virtual keycode. They all start with VK_ and end with the name of the key you want to check. In this case, we want to check the arrow keys so we've used VK_UP, VK_DOWN, VK_RIGHT, and VK_LEFT. If the key is down, it will return true and false if not. This makes it perfect for putting in an if statement.

If ScanKeyDown(VK_LEFT) Then x = x - 1 EndIf
If ScanKeyDown(VK_RIGHT) Then x = x + 1 EndIf    
If ScanKeyDown(VK_UP) Then y = y - 1 EndIf
If ScanKeyDown(VK_DOWN) Then y = y + 1 EndIf

Remember, x and y keeps track of the horizontal and vertical location of the x. Each line here checks a key and subtracts or adds to x or y to move the location in an appropriate direction. This, of course, is only the numerical location of the x. We will use x and y to actually place the x in the next section of the game loop.

Display The Frame

In the next section of the game loop we display the frame. There's not much we have to do in order to do this :

Cls
Locate x,y
Print "x"
DrawText()
Sleep(32)

Here is what this code does :

  1. Cls clears the previous frame.
  2. Locate is used to move the output cursor (the thing that tells Basic4GL where to display text) to the x's current location.
  3. Print is used to actually display the x.
  4. DrawText() draws the current frame. Remember we're using the TEXT_BUFFERED text mode, so we have to call DrawText() to actually see anything.
  5. And finally, Sleep() pauses the game for 32 milliseconds so the game play isn't too fast.

Conclusion

And there you have it. A very simple demonstration of how an ASCII game (and therefore many 2d games) work. You will be using the skills you learned here as long as you continue to make ASCII and/or 2d games.

This page has been recommended for cleanup, integration, etc.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.