Coding with FUZE - BASIC Part 1

Variables are used in programming to store and retrieve data from the computer’s memory. It’s a specified location in memory that can be referenced by the programmer at any point in the code, as long as it’s created and valid.

LET THERE BE VARIABLES

We’ve already looked at assigning some variables in the previous tutorial so let’s extend that and see what else we can do with them.

STEP 1

Enter the Program Editor, by pressing the F2 key. Within the Program Editor enter the following, pressing Enter after each line:
 
Let x=10 
Print x  

Now click on the Save button, along the top of the screen and save the program as ‘Variables1’. Click the OK button to return to the Editor and the Run button to execute the code.

STEP 2

After clicking Run you drop back into Immediate Mode and the display will output the number 10. To break down this simple code, you’ve created the variable called X, and you’ve allocated the value 10 to it. The second line simply prints the current value of X – which is of course, 10.

STEP 3

Press F2 to enter Editor mode and click on New. Now let’s expand on the simple code. Enter the following:
Let x=10
Let y=20
Let z=30
Print x + y + z

Save as ‘Variables2’ and Run it. You now have the output of 60 on the screen, as you’ve assigned X, Y and Z with numerical values, and printed the total.

STEP 4

What if we wanted to change the value of a variable? Enter this listing:
Let x=10
Let x=x-1
Print x

To begin with X equalled 10 but the next line subtracts 1 making it 9, then prints the current value of X. Imagine this as lives in a game, starting with 10 lives, losing 1 and leaving 9 left.

STEP 5

We can extend this further with more commands. Try this:
                                                                        
Let x=10                                                                        
Cycle                                                                          
Print x                                                                        
Let x=x-1                                                                      
Repeat until x=0
Print “Blast Off!”
End

This creates a loop that will minus 1 from X until it reaches 0, then prints Blast Off!

STEP 6

Variables can do more than store numbers:

                                                                         
Input “Hello, what is your first name? “, f$
Print
Input “Thanks, and what is your surname? “, s$
Cls
Print “Hello “; f$; “ “; s$; “. How are you today?”
End

The variables f$ and s$ store input from the user, then printed it back to them on the same line.

STEP 7

Conditional statements allow you to make your program do different things depending on the user input. For example:
cls                                                                                            
Input “Enter your name: “, name$                                                               
If name$=”Dave” then                                                           
Print “I am sorry “; name$                                                     
Print “I am afraid I can’t do that”                                            
Else
Print “That is not a problem “; name$
Endif
End
Save as ‘HAL’ and Run.

STEP 8

The code from Step 7 introduced some new commands. First we clear the screen, then ask for user input and store it in the variable name $. Line 3 starts the conditional statement, if the user enters the name ‘Dave’ then the program will print HAL’s 2001 infamous lines. If another name is inputted, then it will print something else.

STEP 9

Programs store all manner of information, retrieving it from memory in different ways:
cls
Data “Monday”, “Tuesday”, “Wednesday”
Data “Thursday”, “Friday”, “Saturday”
Data “Sunday”
Dim DaysOfWeek$(7)
For DayNo = 1 TO 7 loop
Read DaysOfWeek$(DayNo)
Repeat
For DayNo = 1 TO 7 loop
Print “Day of the week number “; DayNo;
Print “ is “; DaysOfWeek$(DayNo)
Repeat
End

STEP 10

The code from Step 9 is beginning to look quite complex, using the Data command to store constant data, creating a variable called DaysOfWeek using the Dim command and assigning it an indexed dimension (7). The code then Reads the stored Data, assigns it a variable dimension from 1 to 7 and prints the result.

Coding with FUZE - BASIC Part 2

Moving on from the previous FUZE BASIC tutorial, let’s expand everything you’ve done so far and see if we can apply it to something other than counting numbers or asking for someone’s name. In the grand tradition of BASIC programming, let’s create a text adventure.

“PALE BULBOUS EYES STARE AT YOU…”

A text adventure game is an ideal genre to explore your BASIC skills in. There are variables, events, user input, counting and if you want, even a few graphics here and there to inject and use.

STEP 1

Enter the Program Editor and begin with a simple clear screen, as it’s always a good way to start. What we need to do is set some basic parameters first, so start with the number of lives a player has, for example 3.
Cls                                                                            
Let lives=3                                                              

STEP 2

Now you can introduce the game and let the player know how many lives they currently have. You can do this by adding the following to the code:

Printat (41,0); “You have “; lives; “ lives left.”                             
Printat (0,0); “Welcome to Cosmic Adventure!”

The printat command will specify a location on the screen to display the text using x,y.

STEP 3

Let’s add a way whereby the user is required to press a key to continue, this way you can leave instructions on the screen for an indefinite period:
Printat (15,15); “Press the Spacebar to continue…”
While inkey <> 32 cycle
Repeat

This prints the message whilst waiting for the specific key to be pressed on the keyboard: the Spacebar.

STEP 4

Now we can start the ‘story’ part of the adventure:

Cls
Print “You awake to find yourself in an airlock onboard a space station.”
Input “There are two buttons in front of you: Green and Red. Which do you press?”, button$
If button$=”Red” then Let lives=lives-1
Print “You just opened the airlock into space. You are dead!”
Print “You now have “;lives; “ lives left.”

STEP 5

Now add:

If lives=0 then goto 25                                                               
Print “Press the Spacebar to try again.”                                          
While inkey <> 32 cycle                                                           
Repeat                                                                            
Goto 8                                                                            
Else                                                                              
Print “The door to the interior of the space station
opens, lucky for you.”

The Goto command goes to a line number and continues with the code. Here you can use it to start an end of game routine.

STEP 6

Let’s finish this routine off with: Endif
Endif
Goto 29
Print “Sorry, you are dead. End of game. Press Spacebar
to start again.”
While inkey <> 32 cycle
Repeat
Goto 1
This closes the If statements, then goes to line 29 (if you pressed the Green button) to continue the game, skipping the end of game routine.

STEP 7

From line 25 we start the end of game routine as stated on line 15, goto 25. This only works if the variable lives equals 0; the player’s lives have run out. It prints a ‘sorry you are dead’ message and asks to press the Spacebar to start the game all over again from line 1, the goto 1 part.

STEP 8

We can now continue the game from line 29, adding another press the Spacebar routine, followed by a clear screen ready for the next part of the adventure.
Print “Press the Spacebar to continue…”
While inkey <> 32 cycle
Repeat   
Cls   

STEP 9

You can now Save the code, call it Adventure (or something), and Run it from the menu. Whilst it’s not the most elegant code you will ever see, it brings in many different elements and shows you what can be done with FUZE BASIC.

STEP 10

Before you continue with the adventure, and map the fate of our reluctant space hero, we’re going to improve our code with some graphics. FUZE BASIC has some great graphical commands at its disposal, along with some other useful and interesting extras.

Coding with FUZE - BASIC Part 3

The last tutorial had you creating the foundations for a text-based adventure game. Whilst it works perfectly fine, it would be nice to include some graphics and maybe a few other elements to have it stand out from the usual BASIC programs.

ADDING GRAPHICS

FUZE BASIC employs a variety of different commands to display graphics, either drawn on the screen or by displaying an image file.

STEP 1

You’re going to start by making the game full screen, then adding an appropriate image that sets the theme of the adventure. From line 2 press Enter, to create a new line 3, and type in the following:
Fullscreen=1                                                                    
Spriteindex=newsprite(1)
Earth$=”planetEarth.png”
Loadsprite (earth$, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)

STEP 2

The code from Step 1 will import and display an image of the Earth; the image itself is already available in the /Desktop/fuze-basic/extras/images folder. It’s now classed as a sprite and can be manipulated through the various graphical commands of FUZE BASIC. Any unique images you want to include should be copied to this folder to add to your game.

STEP 3

Now create a new line 13, by getting the cursor to the end of line 12 and pressing Enter. For the new line, type in: Hidesprite (spriteindex) This command will remove the image from the screen, allowing you to include a new image for the next step in the game.

STEP 4

You may need to source your own images for your game. In our example, we found an image of red and green buttons and copied to the /Desktop/fuze-basic/extras/images folder. Now we need to add it to our code from line 15:

buttons$=”buttons.png”
loadsprite (buttons$, spriteindex, 0)
plotsprite (spriteindex, 300, 400, 0)

Make sure the image is called before the Input command!

STEP 5

Continuing, we can use images of the interior of the ISS if the Green button is pressed. Download the image, put it in the images folder, name it ISS.png and call it from the code whilst hidesprite hides the previous image.

Hidesprite (spriteindex)
U;date
Print “The door to space station opens..”
ISS$=”ISS.png”
Loadsprite (ISS$, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)

STEP 6

By now your code is getting quite hefty. Don’t forget that with each new line you’re entering, the original Goto values will be different. It’s best to return to the code and update the lines where Goto is referenced.

STEP 7

Additionally we can add an image for the End of Game routine and insert the code from line 39: pre
Print “Sorry, you are dead.”
Gameover$=”gameover.png”
Loadsprite (gameover$, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)
While inkey <> 32 cycle
Repeat
Hidesprite (spriteindex)
Goto 1

STEP 8

Once more, the code has now expanded and as such you need to ensure that any reference to another line is updated to reflect the new numbering; especially lines 24 and 38, which call either End of Game routine or continue the game if the Green Button has been pressed.

STEP 9

Naturally you can continue with Cosmic Adventure yourself, adding choices, graphics and keeping tabs on the number of lives and whatever else you can think of. As we said, it’s not the most elegant code and it’s as far from a triple-A game as you can imagine; but at least it’s given you a head start with FUZE Basic.

STEP 10

Here’s a recap of the images we’ve used for the graphics in our adventure game. The FUZE BASIC manual comes with countless more commands to make better use of the system, so read through it and expand on what you’ve learned here.

More information