In this tutorial I will be going over more complicated console functions, as well as for loops and playing sound. We are basically going to be making the main menu for a game. We will expand on this in later tutorials and when we are finished we will have a fully playable game. We will be introducing subroutines that basically take arguments by setting variables before we call them. This will allow us to reuse the same "functions" with different output depending on what we need. There will be a couple times in this tutorial where I will have things typed that wont be the same as what you will scan in the QR Code. PC has some characters that do not exist in standard unicode format such as a menu pointer finger. First though I will explain the For loop, it is very useful for programmers of all types. Basically it will be formatted as
- FOR VAR = 0 TO 5
- ...
- NEXT VAR
This will set the variable VAR to 0 and execute the code after 5 times, then continue.
- FOR VAR = 0 TO 10 STEP 2
- ...
- NEXT VAR
The above will do the same, only the variable will increase in increments of 2 as opposed to 1.
Alright, enough talk. Lets get to some code.
- ACLS:VISIBLE 1,1,0,0,0,0
- COLOR 15,11 'Set the color of the console to blue background with white characters
- CLS
- @MAINMENU
- LOCATE 8,4 'Set our cursor position where we print to 8,4
- PRINT "Petit Dungeon"
- LOCATE 11,9 'Set our cursor position to 11,9
- PRINT "> Start"
- LOCATE 11,11
- PRINT " Quit"
- 'Set our Box X,Y, Width, and Height
- BX=9:BY=7
- BW=11:BH=6
- GOSUB @DRAWBOX
- BGMPLAY 0,4 'Play some background music, give it ID #0
- CSRPOS = 1 'Set our default cursor position to 1(Top)
- @MMLOOP 'Our main menu loop
- 'If the up button was pressed, set the cursor position, play(beep) a sound effect, and update our pointer.
- IF BUTTON(2) == 1 THEN CSRPOS = 1:BEEP 2:LOCATE 11,11:PRINT " ":LOCATE 11,9:PRINT ">"
- 'Move our cursor down if the down key is pressed
- IF BUTTON(2) == 2 THEN CSRPOS = 2:BEEP 2:LOCATE 11,11:PRINT ">":LOCATE 11,9:PRINT " "
- 'If they press A and its cursor position 1(Top) then play the confirm sound and quit
- IF BUTTON(2) == 16 AND CSRPOS == 1 THEN BEEP 3:GOSUB @ENDGAME
- 'If the buttom option is chosen, play the cancel sound and quit
- IF BUTTON(2) == 16 AND CSRPOS == 2 THEN BEEP 4:GOSUB @ENDGAME
- GOTO @MMLOOP
- @ENDGAME
- BGMSTOP 0 'Stop playing sound #0 (Our bg music)
- END
- 'Our function to draw a box
- 'Arguments: BX, BY, BW, BH
- @DRAWBOX
- 'Draw the top and bottom lines of our box
- FOR LX = BX+1 TO BX+BW-1
- LOCATE LX, BY
- PRINT "─"
- LOCATE LX, BY+BH
- PRINT "─"
- NEXT
- 'Draw the left and right sides
- FOR LY = BY+1 TO BY+BH-1
- LOCATE BX,LY
- PRINT "│"
- LOCATE BX+BW,LY
- PRINT "│"
- NEXT
- 'Draw our box corners
- LOCATE BX,BY
- PRINT "┌"
- LOCATE BX+BW,BY
- PRINT "┐"
- LOCATE BX,BY+BH
- PRINT "└"
- LOCATE BX+BW,BY+BH
- PRINT "┘"
- RETURN
Okay, so that might seem daunting and super confusing. Just take your time reading it and feel free to play around with the code. Change things, break things, fix them. That's what its all about, right? =)
QR Code for this Tutorial
Homework
Okay, I am going to start doing this a little differently. Rather than explain them here I think it is time to teach you about referencing stuff in the manual if you don't already. It will help you more in the long run. The way it works is you press the help icon in the bottom left of the screen and find the topic you need to know something about and then you find the command you need after you pick the topic. From here on I will tell you the topic that the new commands are located and want you to look them up yourself. You may know the old saying: "Give a man a fish, and he will eat tonight. Teach a man to fish, and he will eat forever."
Topic 9: Sound Related Table (For sound/music reference)
Topic 21: Repeat/Comparison
Topic 23: Basic Console Commands
Topic 43: Basic Audio Commands