for loop
Blitz3D Forums/Blitz3D Beginners Area/for loop
| ||
r_x = 10 r_y = 10 For x = 1 To 20 Rect r_x+20,r_y+20,50,50,0 Next why doesnt this draw multiple square shouldnt it? |
| ||
Multiple squares ? You draw always the same square 20 times ! r_x + 20 does not add 20 to r_x ! It's like you write 10 + 20 this is always = 30 each loop... see ? You may write r_x = rx + 20 if you want to increase r_x. Perhaps this works better: r_x = 10 r_y = 10 For x = 1 To 20 r_x = r_x + 20 r_y = r_y + 20 Rect r_x,r_y,50,50,0 Next Or this: r_x = 10 r_y = 10 For x = 1 To 20 Rect r_x+(20*n),r_y+(20*n),50,50,0 Next Hope this has sense for you, Sergio. |
| ||
It draws the same square 20 times. The Rect command is exactly the same every time through the loop. |
| ||
Whats 20*n could anyone explnae this to me never saw that before? :o |
| ||
I think he means 20*x |
| ||
btw.. kaisuo, just a detail, but a name like r_x or r_y reads like hell, esp. in a bigger fomula :) just use small compact variables like x, y, width, height.. or if you have multiple x's .. then x1, x2 .. but not x_1 or x_2 ... :) |
| ||
r_x = 10 r_y = 10 For x = 1 To 20 Rect r_x+(20*n),r_y+(20*n),50,50,0 Next Ok. You appear to be a newbie to the extreme. So lets look at this code step by step. r_x = 10 r_y = 10 Those two lines I think you know. They set the variables r_x and r_y to an initial value of 10 each. For x = 1 To 20 This is the beginning of your loop. It initializes and defines how you want the loop to act. It initializes variable x to start at a value of 1 and through each iteration of the loop, increment x by one through value 20. Next That determines the end of your loop and sends the code back to the For statement to increment the x variable. For x = 1 to 20 Print x Next would print 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Since x is being incremented each time through the loop, you can use it's value for your rectangles to increment their x and y locations. Rect r_x+(20*x),r_y+(20*x),50,50,0 This line is your meat and potatoes of the loop. It draws your rectangles. r_x+(20*x) is your x location and r_y+(20*x) is your y location r_x+(20*x) and r_y+(20*x) on the first iteration would equal 30 (r_x = 10 : 20 * 1 = 20... 20 + 10 = 30) So on the first iteration it would draw a rectangle 50 pixels wide and 50 pixels high at location 30,30. r_x+(20*x) and r_y+(20*x) on the second iteration would equal 50 (r_x = 10 : 20 * 2 = 40... 10 + 40 = 50) So on the second iteration in would draw at 50,50 and so on through iteration 20 where it would draw the rectangle at 400,400 (10 + (20 * 20)). That should, I hope clear up the whole loop + rectangle mystery. :) Kanati [EDIT] And I agree totally with CS_GUY... don't use _ in variable names. It makes them rather unreadable. |
| ||
btw.. kaisuo, just a detail, but a name like r_x or r_y reads like hell, esp. in a bigger fomula :) Very good point CS_TBL. |