Skip to main content

(V) Game loop, variables, dim as boolean, if then else, multikey, false, true, do loop until

Hey everybody!


Take a look at the code of the day:
The result of execute our new code will be the same as the obtained with the code of the last entry but you must press Esc key to exit:
An screen to play with 400 pixels of width and 300 pixels of height and four squares of 32x32 pixels one in each corner. The white square is painted or filled with same color. The screen is being draw again and again and again until you press Esc key.

Let me explain the new keywords used:
  • dim as boolean as const as but with dim we are telling that the label (in our case finish and kescape) will have a variable value. In the case of boolean this value can be false or true.
  • do loop until to make a loop that will repeat the code inside the instruction until the condition after until is resolve to true.
  • if then else to execute the code inside the instruction if the condition is resolve to true, else to execute an alternate code if the condition is  not resolve to true.
  • multikey that will return -1 if a concrete keyboard key is pressed. We have to pass to the instruction the scan code of the key we wnat to check, in the our code Esc key is value is 1 as we put in the declarations.
  • sleep although we have already see sleep in this case we are using it with two parameters. The first one is the amount of miliseconds that sleep will wait and the second one can be 0 if the wait can be interrupted by a key press or 1 without key interrupt.
And the structure, more accurate every time:

  • DESCRIPTION
  • DECLARATIONS
  • INITIALIZATIONS
  • GAME LOOP
    • INPUT
    • DO UPDATE
    • DRAW
    • FREE UP CPU TIME
      • sleep 1,1
  • ENDING
Let's focus on GAME LOOP, it will be always the same, we check for the input (keyboard, mouse or joystick), then we do the update of our new state of the program, then we draw our new state into the screen for the user and free up cpu time beacuse if we don't do this the loop will take all the CPU and don't let time to process other threads in our multitask operating system.

This is enough by now, in the next entry we will try to focus on DRAW part to learn how to control the frame rate and a right way to control the screen when we are drawing, preparing for the moment we are ready to control and move the white square.

Contact me for any question, see you!

Go to the next entry
Go to the previous entry
Go to the first entry

Comments

Popular posts from this blog

(VI) Frame control & locking the screen while drawing: screensync, cls, screenlock, screenunlock

Hiya everybody! Take a look at the code of the day: Let me explain the new keywords used: screensync  is the key to take the number of frames per second under control because this instruction waits until next refresh of the screen, usually 60 times per second known as 60 fps. After this we will draw our screen. cls  to CLear the Screen and draw from zero the new state of the game. screenlock  to lock the screen to hide it while we are drawing. screenunlock  to unlock the screen to show the new drawed screen. Is very important to respect that we have to draw inmediately after the screensync and remember that between screenlock and screenunlock you have to draw, nothing else to do this part of the code as fast as possible. How is the structure? more and more accurate every time: DESCRIPTION DECLARATIONS INITIALIZATIONS GAME LOOP INPUT DO UPDATE DRAW screensync screenlock cls ... screenunlock FREE UP CPU TIME sleep 1,1 E...

(II) Download & setup FreeBASIC's IDE FBIde

Hi everybody! If you use Microsoft Windows on your computer you can download FBIde from SourceForge in this link:  https://sourceforge.net/projects/fbide/files/ Go to fbide 0.4 folder and then fbide 0.4.6 r4  folder and download the zip file with the name  FBIde0.4.6r4.zip . Note: 0.4.6 r4 is the last version while doing this blog entry. Now decompress downloaded file in the path you desire. Me I decided to use my Desktop, beside my FreeBASIC compiler and help folders. Go into  FBIde0.4.6r4  folder and execute the application  fbide Click Yes in the message window. And search fbc application inside your FreeBASIC's compiler folder to open it. Go to View/Settings to Choose your Theme for the editor, I like obsidian theme. Add some new Keywords in Group 2 . Choose the path for FreeBASIC Help file (.chm) downloaded in the previous entry of this blog. Remember to click OK to submit the changes. This is e nough by now, i...