A tutorial on loops in Blitz.
Loops are a very important part of your program. First thought, let me explain how programs work.
There are two kinds of programs. First there are the ones you see on your computer most of the time, these programs just sit around and wait, and then when the user clicks something, a message is sent to do the action. However, in game programming, the game runs on a loop, and performs actions based on what it reads in each loop iteration, or run through. So the loop runs through as normal, then if the computer runs through the loop, but this time reads that the up key has been pressed, then it moves the player up that iteration. Iterations are extremely fast, which is why although you may only move the character up five pixels each frame (a frame is the screen per iteration) the character still moves at a smooth decent speed. In Blitz there are three kinds of loops: For…Next, While…Wend, and Repeat…Until.
First let’s go over the For…Next loop. This loop is used for when you know how many times you want the loop to run through, or iterate. For example, say you wanted to assign all he variables in an array the same value of 54.
For e = 0 To 5 characters(e) = 54 Print characters(e) ; just for show Next
Alright, let’s break it down. You say here that for the variable e (nothing special about e, I just picked it) which variable e = 1 To 5 means that for five iterations, (e = 0, e= 1, etc) you assign the character variable that matches (character(0),character(1), etc) the value of 54, then print it to prove it worked. Remember, this loop is for a set number of times.
Next up, While..Wend. This loop is the most popular loop because it is the best in almost all cases for your main game loop. The loop is declared as follows:
While play = 1 ;run game Wend
This loop is very simple, you state that while some condition exists, you do a certain action, very simple. And the Repeat…Until loop, just as easy.
Repeat ;do stuff Until play = 0
Basically in a Repeat…Until loop, you test the condition at the end. The only difference between the two loops is that the Repeat loop will always iterate at least once, while the While loop can be skipped over completely.
Don’t forget the loops, they are what makes your game run, and loops also play a crucial roll in using types, but we’ll get to that later. For now, let’s focus on functions.












Leave Your Response