The Beginner’s All-Purpose Symbolic Instruction Code, BASIC, is where I started my coding journey. Sat, aged four or five in front of a Commodore 16, I typed in lines of words and numbers which made up an application or sound effect. Sometimes they worked, often they did not. The words that I typed, along with “syntax error,” meant nothing to a five-year-old child who wanted sugar-coated cereal and Transformers cartoons, but I persevered.
Fast forward to the 21st century, and I still use BASIC from time to time. I write BASIC on a Commodore 64 and a ZX Spectrum that I recently renovated. I’ve even written BASIC on a Raspberry Pi Pico to control a series of NeoPixels.
I can’t use MOS6502 BASIC without spending a lot of time getting it to work on my PC, so what can I use instead? It turns out that there is, and it is called QB64 Phoenix Edition, and it looks and feels just like QBasic, but with many more features.
- Realtime input error checking
- Online and offline help system
- Syntax highlighting
- Automatic source formatting
- Debugging
- Compiles native binaries for Windows, macOS and Linux
QB64 Phoenix Edition (QB64 PE) is a fresh offshoot of the original QB64 project, which seemed to die out in the early 2020s. The project’s goal is to keep the spirit of the old application alive and to provide more people with access to running BASIC on their computers. QB64 PE is cross-platform and it has compatibility with QBasic and QuickBasic 4.5 code. But QB64 PE also gives us an extended BASIC and OpenGL, meaning that we can create applications/games with graphics and sound.
So let's take a look at QB64 PE and create our own BASIC project.
Getting Started with QB64 PE
It really couldn’t be any simpler to get started with QB64 PE.
1. Go to the QB4 PE project page and download the package for your operating system.
2. Extract the file to a directory.
3. Navigate to that directory and double left click on the qb64pe.exe application.
The QB64 PE user interface is very much like QBasic. We have the menu at the top, the coding area in the middle, and a status/output/debugging area at the bottom. Our project will see us working with the File and Run menu.
On first boot, QB64 PE will tell us that the application and any executables made by it can be falsely identified by your anti-virus package. You can optionally whitelist the directory, but I have not had any issues in my testing.
QB64 PE is an awesome application, and I urge you to dig into the gloriously in-depth documentation (which is where I found how to show images) and learn more about QB64 PE.
A short history of BASIC
The BASIC language has been with us since 1964, and it was developed as a high-level (easy-to-read) language by John G Kemeny, chairman of the Dartmouth College Math Department. The name BASIC came from Thomas Kurtz.
The structure of BASIC is meant to be over-simplified on purpose. Each line of BASIC is read by the compiler and turned into bytecode, which the machine can then run. But BASIC can be compiled or interpreted, depending on the version you are using. For example, on the Commodore 64, we see Commodore 64 BASIC V2, a version of Microsoft BASIC, which has a limited number of keywords.
The Project Goal: To Make A Simple Game
Learning BASIC, or any coding language, really, is best done by creating a project. I could sit here and teach you about core programming concepts, or we could have some fun and make a simple game. The game in question is inspired by Usborne’s 1980s BASIC coding books, specifically Computer Space Games, which I adored as a child.
The game is “Escape from planet LXF329,” and the goal of the game is to take off and escape a group of aliens who are trying to capture the spaceship. Essentially, this is a number-guessing game, but with a sci-fi theme.
- Learn the BASIC syntax
- Learn how to display images
- Use conditional statements
- Create variables for integers and strings
- Get user input
You may have spotted “Learn how to display images,” and yes, I will show you how to display a static image on the screen using QB64 PE. In the interest of full disclosure, the images used in the game were generated using Adobe Firefly, an AI service. Why? I could’ve used stock images or spent some time in GIMP and Inkscape, but I didn’t have the time, nor the talent, so Adobe Firefly did the work for me.
We’ll start by creating the starting screen, an image that advertises the game and sets the scene.
1. Create a new blank document and click on File >> Save and save the project as space.bas. Remember to save often.
2. Create a new screen object, setting the screen to 1024 x 1024 pixels and 32-bit color, then set the window title to ESCAPE FROM PLANET LXF329”.
Screen _NewImage(1024, 1024, 32) _Title "ESCAPE FROM PLANET LXF329"3. Create a variable, myImage& and store your image.
myImage& = _LoadImage("title.png")4. Create an error handler to check that the image has been loaded correctly. This is basically a conditional check that checks for the loaded file in the variable. If there is nothing there, 0, then it will print an error message.
If myImage& = 0 Then Print "Error loading image!" End End If5. Put the image onto the screen at position 0,0 (top left) then pause the code, waiting for the user to press a key.
_PutImage (0, 0), myImage& Sleep6. Save the code, and click on Run >> run Only (no EXE) to start the code. Click OK in the dialog to start. You should see the image appear on the screen. Press any key to close the window.
7. Create a label, START: Labels enable us to write code that can jump around to different sections by using a Go To command. You may be familiar with using numbers and jumping to a numbered line. Here ,we are not using numbers, so labels will do the same job.
START:8. To begin the game code, clear the screen (CLS) and then use a series of print statements to print a narrative to get the reader up to speed on the game mechanics and story.
Cls Print "ESCAPE FROM PLANET LXF329" Print "Your vessel, the USS TOMSHARDWARE has landed on LXF329" Print "on a routine survey mission" Print Print "But the evil aliens, led by HOAL are after your blood!" Print Print "Calculate the thrust necessary to take-off and escape" Print "the planet's gravity!" Print Print "You only have ten seconds until the aliens burst into" Print "the starship and lay waste to your crew!"9. Ensure that the game uses pseudo-random numbers. If we don’t do this then the game will pick the same numbers each time.
Randomize Timer10. Create two variables, g (gravity) and w (weight), which are random integers, multiplied by 20 and 40, respectively. BASIC uses LET to create variables.
Let g = Int(Rnd * 20) Let w = Int(Rnd * 40)11. Create a third variable to store the correct thrust to leave the planet.
Let r = g * w12. For debug purposes, print the answer to the screen. Later, comment this out using the REM (remark) keyword.
Print rChange to this for the final game
REM Print r13. Print the planet’s gravity and then ask the user to make their thrust calculation.
Print "Planet Gravity = "; g Print "Type in thrust to escape: "14. Using a for loop, we give the player ten guesses before the aliens enter the ship.
For c = 1 To 1015. Stored the player’s guess in a variable, f. That value is compared to the answer, r. If the thrust is too high or low, the for loop repeats until the player runs out of guesses. If the guess is correct, then the game goes to the ENDGAME label.
For c = 1 To 10 Input f If f > r Then Print "Thrust too high"; If f < r Then Print "Thrust too low"; If f = r Then GoTo ENDGAME Next c16. Print “the bad ending” to the screen. This section runs if the player fails to make the correct calculations, and then the for loop ends, throwing the user into this nightmare scenario.
Print Print "The aliens have entered the starship and you and your" Print "crew are now their prisoners!" Print17. Add a graphic to show your ship losing against the alien onslaught. You will need to create a PNG file, stored in the same directory as the game file. This is the same code as used for the start screen. Sleeping for 5 is important; otherwise, there is a bug where the screen automatically closes and ends the game.
Screen _NewImage(1024, 1024, 32) _Title "YOU DIED!" myImage& = _LoadImage("die.png") If myImage& = 0 Then Print "Error loading image!" End End If _PutImage (0, 0), myImage& Sleep 518. Using a GoTo, send the player to the REPLAY section of code.
GoTo REPLAY19. Print a message to the user to say that they have won. Then show an image on the screen. The ENDGAME label is where the successful player is sent. They are rewarded with a message and an escape image showing them blasting off into space!
Print "You have successfully taken off!" Print "The aliens burn in the wake of your engines" Screen _NewImage(1024, 1024, 32) _Title "YOU WON!" myImage& = _LoadImage("escape.png") If myImage& = 0 Then Print "Error loading image!" End End If _PutImage (0, 0), myImage& Sleep 520. Create a means to ask the player if they would like to try again. The player’s input is stored as a variable, a. But note that the variable contains a string ($) which can be y or n. If it is y, the game goes back to the START label. Otherwise, the game stops. We use the REPLAY label to identify what the code is for and to direct the player through the game.
REPLAY: Print "Would you like to try again? Press y or n" Input a$ If a$ = "y" Then GoTo START Else Stop End If21. Save the code, and click on Run >> run Only (no EXE) to start the code. Click OK in the dialog to start.
22. Run the game a few times, win and lose. Remember that the answer is printed to the screen, so make sure to comment that out when letting your friends have a go.
Complete Code Listing
Screen _NewImage(1024, 1024, 32) _Title "ESCAPE FROM PLANET LXF329" myImage& = _LoadImage("title.png") If myImage& = 0 Then Print "Error loading image!" End End If _PutImage (0, 0), myImage& Sleep START: Cls Print "ESCAPE FROM PLANET LXF329" Print "Your vessel, the USS TOMSHARDWARE has landed on LXF329" Print "on a routine survey mission" Print Print "But the evil aliens, led by HOAL are after your blood!" Print Print "Calculate the thrust necessary to take-off and escape" Print "the planet's gravity!" Print Print "You only have ten seconds until the aliens burst into" Print "the starship and lay waste to your crew!" Randomize Timer Let g = Int(Rnd * 20) Let w = Int(Rnd * 40) Let r = g * w Print r Print "Planet Gravity = "; g Print "Type in thrust to escape: " For c = 1 To 10 Input f If f > r Then Print "Thrust too high"; If f < r Then Print "Thrust too low"; If f = r Then GoTo ENDGAME Next c Print Print "The aliens have entered the starship and you and your" Print "crew are now their prisoners!" Print Screen _NewImage(1024, 1024, 32) _Title "YOU WON!" myImage& = _LoadImage("die.png") If myImage& = 0 Then Print "Error loading image!" End End If _PutImage (0, 0), myImage& Sleep 5 GoTo REPLAY ENDGAME: Print "You have successfully taken off!" Print "The aliens burn in the wake of your engines" Screen _NewImage(1024, 1024, 32) _Title "YOU WON!" myImage& = _LoadImage("escape.png") If myImage& = 0 Then Print "Error loading image!" End End If _PutImage (0, 0), myImage& Sleep 5 REPLAY: Print "Would you like to try again? Press y or n" Input a$ If a$ = "y" Then GoTo START Else Stop End IfSharing your work!
If you want to share the game with your friends, then you can! Just bear in mind that the resulting executable is bound to the OS it was created with. Additionally, as QB64 PE notes, your executable may be flagged as a false positive by your antivirus.
1. Click on Run >> Make EXE Only. You can also click on Run >> Start to compile and run the code. This way you get both the executable, and the code will run in the editor.
2. Click OK to compile the executable into the qb64pe folder, the same folder where QB64 PE is being run.
3. Wait for the code to compile. Check the status bar at the bottom of the screen to monitor the progress. On modern systems this will take seconds.
4. Navigate to the folder with the compiled game code. I scanned my compiled code using Microsoft Defender and Malwarebytes, and nothing nasty was found.
5. Double click on the executable to run the game.