loops

BlitzPlus Forums/BlitzPlus Beginners Area/loops

SkidMarcUK(Posted 2012) [#1]
how do you for next all this. cant seem to get syntax right.


Graphics 800,600

SeedRnd MilliSecs() 

gfxGrab1=CreateImage(100,100) 
gfxGrab2=CreateImage(100,100) 
gfxGrab3=CreateImage(100,100) 
gfxGrab4=CreateImage(100,100) 
gfxGrab5=CreateImage(100,100) 

Color Rnd(255),Rnd(255),Rnd(255) 
Oval 50,50,50,50
GrabImage gfxGrab1,0,0 

Color Rnd(255),Rnd(255),Rnd(255) 
Oval 50,50,50,50
GrabImage gfxGrab2,0,0 

Color Rnd(255),Rnd(255),Rnd(255) 
Oval 50,50,50,50
GrabImage gfxGrab3,0,0 

Color Rnd(255),Rnd(255),Rnd(255) 
Oval 50,50,50,50
GrabImage gfxGrab4,0,0 

Color Rnd(255),Rnd(255),Rnd(255) 
Oval 50,50,50,50
GrabImage gfxGrab5,0,0 

MidHandle gfxGrab1
MidHandle gfxGrab2
MidHandle gfxGrab3
MidHandle gfxGrab4
MidHandle gfxGrab5

Repeat

Delay (50)

For t=1 To 10
DrawImage gfxgrab1,Rnd(750),Rnd(550)
DrawImage gfxgrab2,Rnd(750),Rnd(550)
DrawImage gfxgrab3,Rnd(750),Rnd(550)
DrawImage gfxgrab4,Rnd(750),Rnd(550)
DrawImage gfxgrab5,Rnd(750),Rnd(550)
Next 

Flip

Until KeyHit(1) 




Yasha(Posted 2012) [#2]
Can you explain exactly what you want the code to do? (I can take a good guess, but...)

What you have there looks like the syntax is correct, insofar as it is legal Blitz code, it just presumably isn't the particular legal Blitz code that you want. I have a feeling the answer to your problem may be to investigate "arrays".


Hotshot2005(Posted 2012) [#3]
Is that what you trying to do....like this

Const Press_Esc=1

Const Screen_Width=800
Const Screen_Height=600
Const Depth=16
Const Window_Mode=2

Const Ball_Size=30

Graphics Screen_Width,Screen_Height,Depth,Window_Mode

SeedRnd MilliSecs() 

Repeat
 
     Color Rnd(255),Rnd(255),Rnd(255) 
     Oval Rnd(Screen_Width),Rnd(Screen_Height),Ball_Size,Ball_Size
     Delay (50)
     Flip

Until KeyHit(Press_Esc) 


hope that help :)

Last edited 2012


SkidMarcUK(Posted 2012) [#4]
just wantad to do something like this

for n=1 to 5
Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab+n,0,0
next

instead of

Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab1,0,0

Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab2,0,0

Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab3,0,0

Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab4,0,0

Color Rnd(255),Rnd(255),Rnd(255)
Oval 50,50,50,50
GrabImage gfxGrab5,0,0

if that makes sense


Yasha(Posted 2012) [#5]
Yeup, what you want is an array.

First, a quick breakdown on the nature of the programming language:

Blitz Basic is compiled, not text-interpreted. This means that all control structures are turned into bare-metal machine instructions, all functions are just bits of code located somewhere else that the program knows how to jump to, "handles" of images and so on are just memory addresses identifying which piece of memory you're working with, and - importantly in the context of this question - all variable names are just offsets into the current stack frame.

You don't need to understand the specifics of what this means, but the upshot is that by the time it's been compiled and is now running, the program has no idea that there was a variable named gfxGrabAnything, or that there was one named i, or MyImg, or DaveTheVariable. It's only aware of StackPointer+4, StackPointer+8, StackPointer+12 and so on.

Therefore, trying to manipulate the name of a variable dynamically won't achieve anything: a variable isn't a value, it's a container for a value. Using the + operator "on" a variable is actually just using it on the value contained within the variable (and adding n to an image... is not an operation that we even want to make sense).

So, for the situation that you find yourself in - where you want to have several containers and dynamically control which one you access based on runtime information, in this case a loop - we have the "array": a multi-cell container for values; a variable that holds multiple things. Which thing you get, or set, from the array as a whole, is the part that can be specified dynamically.

An array is declared like this:

Local gfxGrab[5]

This gives gfxGrab six slots (they start at zero, but they go up to five, so if you want to forget zero and start at one that's fine). Note that you have to use Local or Global to declare the array, as it can't automatically detect from context when to create a new one, unlike regular variables (recommendation: always use Local or Global anyway, it's better style).

You can refer to individual cells within the array using the value - which can be any value expression that yields an integer - between the brackets:

gfxGrab[1] = something    ;Set
Print(gfxGrab[1])    ;Get

Local n : For n = 1 to 5    ;Indexed by changing variable
    Print gfxGrab[n]
Next

; Indexed by more complicated expression
gfxGrab[ComplicatedFormula(a, 8.0, "c") + 12] = something


However, when you create the array it must have a constant (fixed) size - you can't create new arrays with sizes set by runtime information. To do that you need to use the other kind of array provided by Blitz, created with the Dim command. I suggest learning how to use arrays like this first, and worrying about Dim later (as a rule, you don't need Dim: there's usually a better way to do it if you find yourself wanting that).