Sunday, July 14, 2013

Tutorial #5 - Beginnings of a Game

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

  1. FOR VAR = 0 TO 5
  2. ...
  3. NEXT VAR
This will set the variable VAR to 0 and execute the code after 5 times, then continue.

  1. FOR VAR = 0 TO 10 STEP 2
  2. ...
  3. 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.


  1. ACLS:VISIBLE 1,1,0,0,0,0
  2. COLOR 15,11 'Set the color of the console to blue background with white characters
  3. CLS

  4. @MAINMENU
  5.  LOCATE 8,4 'Set our cursor position where we print to 8,4
  6.  PRINT "Petit Dungeon"
  7.  LOCATE 11,9 'Set our cursor position to 11,9
  8.  PRINT "> Start"
  9.  LOCATE 11,11
  10.  PRINT "  Quit"

  11.  'Set our Box X,Y, Width, and Height
  12.  BX=9:BY=7
  13.  BW=11:BH=6
  14.  GOSUB @DRAWBOX

  15.  BGMPLAY 0,4 'Play some background music, give it ID #0
  16.  CSRPOS = 1 'Set our default cursor position to 1(Top)

  17. @MMLOOP 'Our main menu loop
  18.  'If the up button was pressed, set the cursor position, play(beep) a sound effect, and update our pointer.
  19.  IF BUTTON(2) == 1 THEN CSRPOS = 1:BEEP 2:LOCATE 11,11:PRINT " ":LOCATE 11,9:PRINT ">"
  20.  'Move our cursor down if the down key is pressed
  21.  IF BUTTON(2) == 2 THEN CSRPOS = 2:BEEP 2:LOCATE 11,11:PRINT ">":LOCATE 11,9:PRINT " "
  22.  'If they press A and its cursor position 1(Top) then play the confirm sound and quit
  23.  IF BUTTON(2) == 16 AND CSRPOS == 1 THEN BEEP 3:GOSUB @ENDGAME
  24.  'If the buttom option is chosen, play the cancel sound and quit
  25.  IF BUTTON(2) == 16 AND CSRPOS == 2 THEN BEEP 4:GOSUB @ENDGAME
  26. GOTO @MMLOOP

  27. @ENDGAME
  28.  BGMSTOP 0 'Stop playing sound #0 (Our bg music)
  29. END

  30. 'Our function to draw a box
  31. 'Arguments: BX, BY, BW, BH
  32. @DRAWBOX
  33.  'Draw the top and bottom lines of our box
  34.  FOR LX = BX+1 TO BX+BW-1
  35.   LOCATE LX, BY
  36.   PRINT "─"
  37.   LOCATE LX, BY+BH
  38.   PRINT "─"
  39.  NEXT
  40.  'Draw the left and right sides
  41.  FOR LY = BY+1 TO BY+BH-1
  42.   LOCATE BX,LY
  43.   PRINT "│"
  44.   LOCATE BX+BW,LY
  45.   PRINT "│"
  46.  NEXT

  47.  'Draw our box corners
  48.  LOCATE BX,BY
  49.  PRINT "┌"
  50.  LOCATE BX+BW,BY
  51.  PRINT "┐"
  52.  LOCATE BX,BY+BH
  53.  PRINT "└"
  54.  LOCATE BX+BW,BY+BH
  55.  PRINT "┘"
  56. 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
  • FOR~ TO~ STEP
Topic 23: Basic Console Commands
  • COLOR
  • LOCATE
Topic 43: Basic Audio Commands
  • BEEP
  • BGMPLAY
  • BGMSTOP

1 comment:

  1. This is such a great blog/tutorial! thanks for doing these and please post more!!! thank you!

    ReplyDelete