Welcome to the next session for the C64 Basic Marathon Game. In this session we will create a simple game where we can pick from random doors to obtain prizes, etc. My vision here is to help you learn the steps of programming in Basic and continue to grow your skills as this tutorial series evolves with the release of each new YouTube video.
For our first Commodore 64 Basic programming example we are going to clear the screen (line 30), setup background/foreground colors, and text color (lines 32-40), and print the start of our short game to the screen (lines 50-80). Once you RUN this you will see a simple text display.
The next part of this will be to get our text centered on the screen and in a good position. To accomplish this, we are going to change some lines. Line 50 introduces a new C64 Basic Marathon Game command called TAB. Essentially this will move the cursor to various locations on the screen, similar to the Tab key on a standard Windows/Apple keyboard.
At first I was trying to take the width of the screen (40 columns) and divide that by two to obtain the center, but since the text continues to display to the right, I decided much better and just fat fingered some values into our TAB statements instead. So we now end up with the new lines below.
10 REM EXAMPLE 2
20 ;
25 SC=0
30 PRINTCHR$(147)
32 POKE53280,2:REM BORDER TO RED
33 POKE53281,0:REM SCREEN TO BLACK
40 POKE646,7
50 PRINT TAB(12)”DOOR CRAZE”
60 PRINT:POKE646,1
70 PRINT TAB(12)”CHOOSE YOUR LUCK.”:PRINT
80 PRINTTAB(10)”DOOR 1.
90 PRINTTAB(12)”DOOR 2.”
100 PRINTTAB(12)”DOOR 3″
110 INPUT “WHAT IS YOUR NAME”;NM$
120 PRINT “THANK YOU “;NM$
130 PRINT
Moving forward, we will now add another INPUT command to our C64 Basic Marathon Game program at line 140. We also added a PRINT statement to line 130 to skip down a line. The new lines are listed here.
Also take note that we are not using a string variable this time. Instead this is known as a numerical variable, but it can also be used with an INPUT statement, except it will only accept a number entry. If you try to enter text or other characters that are not numbers, it will throw an error.
130 PRINT
140 INPUT “WHICH DOOR”;DO
We are going to now generate a random value for our doors. So on line 150 we are creating a variable name called DR, adding a one to it so that the variable does not start at zero, and then picking from a random integer number between 1-2.
In line 160, the program is now using a conditional statement (IF/THEN – ELSE) to check some logic. So the statement is looking at our random value that was created on line 150 and comparing it to the value we typed next to ‘WHICH DOOR’ at line 140. I also had to add a new variable called MO (short for ‘money’) appended to line 25 so it can print this value on line 170.
So it read “If the random value found in the variable DR is equal to the number we entered at line 140 then write ‘CONGRATULATIONS! YOU WON $1000’. I hope you are enjoying the C64 Basic Marathon Game so far!
10 REM EXAMPLE 2
20 ;
25 SC=0:MO=1000
30 PRINTCHR$(147)
32 POKE53280,2:REM BORDER TO RED
33 POKE53281,0:REM SCREEN TO BLACK
40 POKE646,7
50 PRINT TAB(12)”DOOR CRAZE”
60 PRINT:POKE646,1
70 PRINT TAB(10)”CHOOSE YOUR LUCK.”:PRINT
80 PRINTTAB(12)”DOOR 1.
90 PRINTTAB(12)”DOOR 2.”
100 PRINTTAB(12)”DOOR 3″
110 INPUT “WHAT IS YOUR NAME”;NM$
120 PRINT “THANK YOU “;NM$
130 PRINT
140 INPUT “WHICH DOOR”;DO
150 DR=1+INT(RND(0)*2)
160 REM GET RESULT OF DOOR
170 IFDR=DOTHENPRINT”CONGRATULATIONS! YOU WON $”;MO;”.00″
In the next few lines for 180 through 190 we are going to add a new condition to determine if the use chose a number that is less than the correct number. If this value is less than the random number generated on line 150 then a message will print “BAD LUCK. THE MONSTER ATE YOU AND YOU ARE NO MORE”. Then the program will end.
For line 190 we are going to check if the number entered was one higher than the correct number and write the message “CLOSE” to indicate that the correct number is within range.
Also line 170 had more added to it. At the end I used SC=SC-10 to subtract from the current score. Finally in line 190 a new additon was added for SC=SC+10 to increase the score if the user’s guess was close.
180 IFDRDO+1THENPRINT”CLOSE.”:SC=SC+10
Now we need to add some new lines to give the user a reward message when the guesses the correct number. So the system will write “AWESOME! YOU JUST WON “, which is followed by the current score variable (SC).
200 :
210 REM PRINT THE MONEY WON
220 PRINT:PRINT”AWESOME! YOU JUST WON “;SC
After this I appended data to line 70 to show the user’s score on the screen at the beginning of the C64 Basic Marathon Game. This will remain on the screen if the game does not end.
70 PRINTTAB(10)”CHOOSE YOUR LUCK”,”SCORE:”;SC:PRINT
Since I decided to add a game score, I altered line 170 to reflect an increase of 20 points: SC=SC+20, which is the result of getting the correct answer.
170 IFDR=DOTHENPRINT”CONGRATULATIONS! YOU WON $”;MO;”.00″:SC=SC+20
Next I added even more points to line 220 by appending SC=SC+50
220 PRINT:PRINT “AWESOME! YOU JUST WON “;SC:SC=SC+50
Soon after analyzing the C64 Basic Marathon Game further, I realized that it should have the ability to allow the player to win in the end. So I created line 230 to check for a score above 100 (SC>100) and write the message “YOU WON THE GAME!”. Then it finally ends
230 IF SC>100 THEN PRINT “YOU WON THE GAME!”:END
Below this I created a way for the program to keep going by returning to line 30 with line 300 below.
300 GOTO 30
Going forward I changed the program to check if the game has been running for the first time. So I modified line 110 to check if DR<1 then allow the INPUT prompt to appear. This will only ask for the user’s name the first time the program arrives at line 110. Other than that, it will not ask again. While I was writing this, I switched gears and used a GOSUB statement to keep the program tidy. The new line and changes are below.
110 IFDR<1THENGOSUB600
600 INPUT”WHAT IS YOUR NAME”;NM$
610 PRINT “THANK YOU “;NM$
620 RETURN
I felt it was also important to add a delay for our game. So line 630 was the result
630 REM DELAY
640 FORHH=1TO1000:NEXT:RETURN
In order for the C64 Basic Marathon Game to slow down, I also had to add new information to line 220 AND 170, which is below. Since the lines were moved, I changed line 120. The semicolon forces the compiler to ignore that line instead.
120 :
170 IFDR=DOTHENPRINT”CONGRATULATIONS! YOU WON $”;MO;:SC=SC+20:GOSUB640
220 PRINT:PRINT”AWESOME! YOU JUST WON “;SC:SC=SC+50:GOSUB640
When I ran the program, I realized the after the user guessed the correct number, the score was not updated correctly. So I adjusted line 220 to read the variable before the message was printed as seen below.
220 SC=SC+50:PRINT:PRINT”AWESOME! YOU JUST WON “;SC:GOSUB640
Much later, I realized I had to removed the MO variable from the C64 Basic Marathon Game since we are using SC for score. So line 25 was changed to
25 SC=0
Then line 170 was altered to calculate the score before writing a message.
170 IFDR=DOTHENSC=SC+20:PRINT:PRINT”CONGRATULATIONS! YOU WON $”;SC:GOSUB640
To enhance the visual display, new lines were added to change the text color. This also required adjusting line 640 to read the new subroutine at line 720
40 POKE646,7
135 GOSUB720
640 FORHH=1TO1000:NEXT:GOSUB720:RETURN
720 POKE646,1+INT*RND(0)*15)
730 RETURN
Get Connected!
Come and join our community. Expand your network and get to know new people!
No posts found.
No posts found.
Nothing more to show.