• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Retro Game Coders

Retro Game Coders

Retro computer/console game + dev community

  • Home
  • About
  • Blog
  • Retro Resources
    • Retro Gaming Timeline
    • Browser C64 Emulator
    • Best Retro YouTube Channels
    • New Retro Books
    • Raspberry Pi Amiga Emulation
    • MiSTer FPGA Tutorial
    • BMC64 C64 Pi
  • Contact
Home » Programming

Loading Game Data – Commodore BASIC Programming Part 6

Commodore 64 BASIC Programming Series

Part 1: Introduction to Commodore BASIC

Part 1.5: Installing CBM Prg Studio on Mac/Linux

Part 2: Commodore BASIC Commands GOSUB and FOR Loops

Part 3: If/Then, Game Logic and Cursor Movement

Part 4: The Magic of POKE

Part 5: C64 DOS Commands

In part 6 of our Commodore BASIC series we will load level data so we do not need to hard-code it into our game program.

Check it out for yourself, see it running in your web browser!

Updated Game Code

As we discussed previously, if you want a very simple text file that you load in from start to finish, then Commodore BASIC makes them very easy to write and read.

Starting with our end in mind, here is the updated Dungeon Crawl game, modified to use our custom characters, loading into memory, and from 6000 our new map routine that loads a SEQ file from disk.

Here is the key part:

6060 open 1,8,2,"map, s"
6120 input#1, a$ :row$(r)=a$:print a$:r=r+1
6130 if st=0 then 6120
6140 close 1

We open our file using S for SEQ type, ie. Sequential. After that, each time we call input and the file number #1 we can load from the file a full line into the variable, in this case a$. If the st status variable is 0 then we know there is still data to load, otherwise we close the file and then continue.

Loading game data from disk
Our Dungeon game loading data from disk
0 rem Dungeon game that loads data
20 if a=0 then print chr$(147)"loading, please wait"
30 if a>0 then print "load file"a
40 a=a+1:on a goto 50,60,70
50 load "chars.bin",8,1
60 poke 53272,(peek(53272)and240)+12
70 gosub 4000            
80 rem show map    
90 gosub 6000
                
1000 rem start of game loop
1010 ox=col : oy=row
1020 gosub 3000 : rem get keyboard input
1030 if row<>oy or col<>ox then print "{left} "
1040 gosub 4000 : rem set the char pos
1050 print "{light blue}@";
1090 goto 1000 : rem go back to loop
3000 rem ...........................                                
3010 rem get keyboard input
3020 get p$ : rem peek(197) also works
3030 rem print asc(p$) to work out keys
3040 if p$="o" then col=col-1 : rem <
3050 if p$="p" then col=col+1 : rem >
3060 if p$="q" then row=row-1 : rem ^ 
3070 if p$="a" then row=row+1 : rem v
3080 if col < 0 then col=0
3090 if col > 39 then col=39
3100 if row < 0 then row=0
3110 if row > 23 then row=23
3120 if peek(1024+(row*40)+col)=32 then return
3130 rem don't move if not to a space
3140 row=oy : col=ox
3150 return
4000 rem                                
4010 rem ........................... 
4020 rem set cursor row and column
4030 x=row : y=col
4040 poke 780,a : rem set a register 
4050 poke 781,x : rem set x reg (row)
4060 poke 782,y : rem set y reg (col)
4070 poke 783,0 : rem set status reg
4080 sys 65520  : rem set the cursor
4090 return
6000 rem 
6010 rem ............................
6020 print "{clear}{home}{right}{right}{down}{down}{down}{cyan}press keys: q,a,o,p{white}"
6030 row=10:col=10:rem init vars, set initial position
6040 poke 53272,(peek(53272)and240)+12
6050 dim row$(12):r=0
6060 open 1,8,2,"map, s" 
6120 input#1, a$ :row$(r)=a$:print a$:r=r+1
6130 if st=0 then 6120
6140 close 1 
6210 poke 53280,0 : poke 53281,0
6280 return

If you like it, share it! (Please - because it really helps)

  • Tweet
Category: ProgrammingTag: basic, c64, commodore, programming
Previous Post:C64 DOS Commands – Commodore BASIC Programming Part 5
Next Post:Programming Electronics with the Commodore User Port Part 1: Blinking an LEDCommodore Electronics

Retro Game Coders

Retro computer/console game + dev community by Chris Garrett

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Maker Hacks ・ Geeky Game Master

© Copyright 2021 Chris Garrett

Privacy ﹒ Terms of Service

Return to top