|
<< Click To Display Table Of Contents >> You Are Here: MADRIX Script (Introduction) > Basics > Statements > 'For' And 'While' Loops |
Loops are used in programming languages to repeat tasks. For example, every pixel should be set to a green color. Loops run as often as a given condition is true. MADRIX Script offers two possible forms of loops, the for-loop and the while-loop. Both are similar to the loops in the programming language C.
The while-loop is built in this way:
while(condition)
statement
The statement may be a single expression statement. If more than one statement needs to be executed, a block is needed, as shown in the following example:
int x = 0;
while(x < 10)
{
SetPixel(WHITE, x, 0);
x++;
}
The for-loop is built like this:
for(initialization; condition; expression)
statement
This is the same as:
initialization;
while(condition)
{
statement;
expression;
}
The initialization-part may contain the declaration of a new variable or an expression. It is executed the first time the loop runs. There may be different initializations separated by comma. Newly declared variables only exist within the for-loop. After initialization, the condition is checked. If it is unequal to 0, the given statement is executed. (In this respect, a value of 0 represents false. But a while-loop will only be executed if the condition is true. And true is represented by a value of 1, which in turn is unequal to 0.)
The condition-part contains an expression. As long as the given expression is not 0 or false, the given statement is executed. If the condition-part is empty as discussed beneath, it will be interpreted as true.
The expression in the expression-part is executed each time after running the statement and before checking the condition. There may be different expressions separated by comma.
It is possible to leave different parts of the for-loop empty. But semicolons are necessary, nevertheless. This may be used to implement the initialization outside the loop. Here are three examples for for-loops. The first one is an endless loop:
for( ; ;)
{
do anything;
}
for(int x = 0; x < GetMatrixWidth(); x++)
for(int y = 0; y < GetMatrixHeight(); y++)
SetPixel(WHITE, x, y);
for(int x = 0; x < GetMatrixWidth(); x++)
{
if(x % 2)
DrawPixelLine(WHITE, x, 0, x, GetMatrixHeight());
else
DrawPixelLine(BLACK, x, 0, x, GetMatrixHeight());
}
There are two possibilities to control a loop. First, it is possible to interrupt the execution of a loop (break). Furthermore, there is a way to skip the rest of the statements of the loop and to go to its beginning (continue).
With the keyword break a loop can be quit immediately. For example:
int x = 0;
while(x < 10)
{
if(x++ >= GetMatrixWidth())
break; //leave loop now!
SetPixel(WHITE, x, 0);
}
The execution of the script is continued after the loop. No other statement within the loop is executed after break.
With the keyword continue it is possible to skip the rest of the statements within a loop and to start anew. For example:
int x = 0;
while(x < 10)
{
if(x++ % 2 == 0)
continue;
SetPixel(WHITE, x, 0);
}
The following example for the MAS Script Effect draws parts of a cosinus curve onto the matrix while changing the background color.
void InitEffect()
{
SetAsync(true);
SetBpm(300);
}
void RenderEffect()
{
color col = {200, 200, 100, 200};
color colBK={random(0,150), random(0,255), random(0,100), random(0,255)};
//set background
Clear(colBK);
//draw cosine curve
int px = 0;
int py = 0;
float y;
float t = 0;
while(px < GetMatrixWidth())
{
y = cos(t) * GetMatrixHeight();
py = (int)y;
t = t + (PI * 3 / GetMatrixHeight());
px++;
SetPixel(col, px, py);
}//while[x < GetMatrixWidth()]
}
Here is another full example for the MAS Script Effect which uses fields to store random colors and to fill the matrix with them.
persistent color g_MatrixColors[][];
void RenderEffect()
{
//select random color
color col = {random(0,255), random(0,255), random(0,255), random(0,255)};
//select random pixel coordinates
int px = random(0,GetMatrixWidth()-1);
int py = random(0,GetMatrixHeight()-1);
//save the selected color
g_MatrixColors[px][py] = col;
//draw points of the array
for(px = 0; px < GetMatrixWidth(); px++)
{
for(py = 0; py < GetMatrixHeight(); py++)
{
SetPixel(g_MatrixColors[px][py], px, py);
}//for[each line]
}//for[each column]
}
void InitEffect()
{
for(int x = 0; x < GetMatrixWidth(); x++)
for(int y = 0; y < GetMatrixHeight(); y++)
{
g_MatrixColors[x][y].r = 0;
g_MatrixColors[x][y].g = 0;
g_MatrixColors[x][y].b = 0;
g_MatrixColors[x][y].w = 0;
}
SetAsync(true);
SetBpm(300);
}
| MADRIX 3.23. |
| [Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
|
Previous Next
|
|
Enable Automatic Translation | Activer La Traduction Automatique | 启用自动翻译 |