
For your Commodore 64 BASIC game to be fun and challenging, it will need to have some “logic”.
There are two kinds of logic we will need to address:
- The “rules” of your game – how the game works internally.
- Display or “render” logic – how the game updates visually.
For our example Commodore BASIC programs, we will first update the Dungeons and Dragons Character Generator to make our character stats more fair, then we will look at a game of Rock, Paper, Scissors that will have a computer “player”. Finally we will start controlling a text-based player character with the keyboard and move it around the screen.
Add Your Email, Get the Files
Never miss an update plus get these bonus downloads:


Commodore 64 BASIC Programming Series
Part 1: Introduction to Commodore BASIC
Part 1.5: Installing CBM Prg Studio on Mac/Linux
When computer games first started appearing, they tended to be two player, such as Space War and Pong, or were games with very simple logic, such as puzzle games.
Why is that?
It’s because even now, producing a computer player that is as challenging as a human is tough, and it was especially tough on early computers with low processing power and scarce memory.
Fortunately, you don’t need actual AI to make a fun game.
Simple Game Logic

Consider games like Space Invaders.
For the most part, the game keeps track of where the player is, positioning the invaders that are still standing, if to show the UFO, and missile hits from and against the aliens.
If you hit an alien then your score is incremented, if an alien hits you then a life is lost until you are out of lives.
Pathfinding Game Logic

Pac-Man is an example of more complex-feeling game logic, still crammed into a very simple machine by today’s standards.
You can read all about how it works in this in-depth article.
Essentially it was an early form of maze pathfinding.
The creator, Toru Iwatani, didn’t want the ghosts to be so aggressive and competent that the game was over-challenging. At the same time, it needed to have some challenge, otherwise people wouldn’t pump in coins to try and beat it.
Today pathfinding has a whole science around it, read more about that here.
Games from first-person shooters through to real time strategy games need to have the enemies and non-player characters avoid obstacles, go from point A to point B semi-directly, and not get stuck banging their pixelated heads against walls.
Note our bad guys don’t need to learn, just appear to be semi-rational in their behavior.

How to Program Logic in Commodore BASIC
Just talking about logic leads us to use the words IF
and THEN
.
IF Pac-Man eats a power pill THEN make ghosts panic
It is hardly surprising then then BASIC uses IF THEN as the commands for decision-making.
IF A > 10 THEN PRINT "A IS GREATER THAN 10"
IF A < 10 THEN PRINT "A IS LESS THAN 10"
IF A = 10 THEN PRINT "A IS EQUAL TO 10"
With this knowledge we can tweak our D&D Character Creator BASIC program from the previous lesson to make the stats more fair.
in our original we used a roll of a D20 (20-sided dice). This could mean you get a value of 20 – yay! But also get a roll of 1 … ouch.
Another approach is to roll 4x D6, take the top-3 numbers, and combine them. While you can’t ever get 20 in that scenario, it does mean the very worst score you can get is a 3. Not stellar but not tragic either.
As we are doing this as a learning process, it gives me the opportunity to introduce a Sorting Algorithm called the “Bubble” Sort. Wayyy overkill for this use-case but worthwhile to understand.
Bubble Sort in Commodore BASIC
In the Bubble Sort we go through our list of numbers and compare them.
We want the highest numbers to “bubble up”, so we compare using IF and when we find a higher number we switch them in our list (an array called D) then compare the next.

D&D Character Generator Version 2 BASIC Code

How to Print at a X,Y Location in Commodore BASIC – Displaying Text Anywhere You Want on Screen

Unfortunately, Commodore 64 BASIC doesn’t have a Print at X,Y command, or an easy Cursor/Locate command to set the cursor at a specific place on screen.
Fortunately, we have several options to overcome this limitation. The easiest but least-desirable is to use the control characters we touched on previously.
If you are using drag and drop to upload a .BAS BASIC text file to my in-browser C64 emulator, or are using CBM Prg Studio, then you can enclose keywords in {brackets}. On an actual or emulated Commodore 64 then if you start with ” then cursor key presses will be converted to special characters. Entering the final ” will turn that mode off.
At run time those characters will be interpreted as movement commands for the cursor. With the cursor in place, anything you print will appear after it.
Of course this is not as elegant as we like. We will cover alternative options as we get to them!
Detect Keypresses with GET
GET reads input from the keyboard, but unlike INPUT, GET does not require you to press the Return/Enter key to send the information back.
This obviously comes in handy for keyboard control in games, but also “Press any key to continue” and for menus.
Let’s combine this, along with the IF statement, and make a playable game of Rock, Paper, Scissors.
Rock, Paper, Scissors Commodore 64 BASIC Game
The user is asked to press 1, 2 or 3, after which the computer selects it’s move randomly, then the results are compared.
To make the screen more legible, we use our new-found cursor positioning powers.
After the match, the player is asked if they want to play again.
100 ? rnd(-ti)
110 w=0 : a$ = "" : cc = 0
120 print "{clear}{white}{reverse on}rock, paper, scissors{reverse off}"
125 print : print
130 print "press:"
140 print "1. rock"
150 print "2. paper"
160 print "3. scissors"
170 print : print
180 op$(1) = "rock"
190 op$(2) = "paper"
200 op$(3) = "scissors"
210 print "press the number of your choice"
220 get a$ : if a$ = "" or val(a$) > 3 then 220
230 print "i choose"
240 cc=int(rnd(1)*3)+1
250 print op$(cc)
260 print "{up}{up}{right}{right}{right}{right}{right}{right}{right}{right} you chose"
270 print "{right}{right}{right}{right}{right}{right}{right}{right} "op$(val(a$))
275 print : print
280 print "so the winner is ...";
290 if val(a$)=cc then print "a draw" : goto 350
300 if val(a$)=1 and cc=3 then w=1
310 if val(a$)=2 and cc=1 then w=1
320 if val(a$)=3 and cc=2 then w=1
330 if w=1 then print "you!"
340 if w=0 then print "me!"
345 print : print
350 print "{reverse on}play again? (y/n){reverse off}"
360 get p$ : if p$ <> "y" and p$ <> "n" then 360
370 if p$ <> "n" then 110
380 print "goodbye, thanks for playing"
Note the ? is translated to PRINT. It is just a shorthand. We can safely initialize our random number generator using PRINT because the next line clears the screen anyway!
Using Keyboard Control in Commodore BASIC Games
Over this series, one of the game examples we will work on is a Dungeon Crawler game, where you explore a dungeon battling bad-guys and collecting keys and treasure.
Now we can crudely position text on screen, and we can read the keyboard, we can start making our game.
An obvious flaw with our method is we can move anywhere we want without limitation, right over walls. Another problem is we leave a trail behind us.
We will fix these issues next time!
