Recent

Author Topic: canvas i need help  (Read 8652 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #15 on: September 19, 2017, 04:16:36 pm »
oh i meant i want to make them all green when all of them becomes red.

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #16 on: September 19, 2017, 04:20:01 pm »
Just check with a temp-variable (local variable AllRed) if all boxes are clRed. And if so, color them green again.

Code: Pascal  [Select][+][-]
  1.   AllRed := true;
  2.   for i := 0 to (boxlength - 1) do
  3.     if MyBoxes[i].Brush.Color <> clRed then AllRed := false;
  4.   if AllRed then
  5.     for i := 0 to (boxlength - 1) do
  6.       MyBoxes[i].Brush.Color := clGreen;

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #17 on: September 19, 2017, 05:01:50 pm »
Just check with a temp-variable (local variable AllRed) if all boxes are clRed. And if so, color them green again.

Code: Pascal  [Select][+][-]
  1.   AllRed := true;
  2.   for i := 0 to (boxlength - 1) do
  3.     if MyBoxes[i].Brush.Color <> clRed then AllRed := false;
  4.   if AllRed then
  5.     for i := 0 to (boxlength - 1) do
  6.       MyBoxes[i].Brush.Color := clGreen;


thank you very much. how do i make a random box green at a random time?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #18 on: September 19, 2017, 05:06:52 pm »
how do i make a random box green at a random time?
You might want to try that one yourself and mention where you get stuck.

(I've already given you a lot of code where you could have tried it yourself and this is not an ask-and-answer site for ready-made code.)

Hint: Randomize; at the beginning of your program and Random(10) will give you a random number between 0 and 10. You could use a timer with a random interval. (oops, maybe I said too much  ::))
« Last Edit: September 19, 2017, 05:11:56 pm by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #19 on: September 19, 2017, 05:19:08 pm »
Code: Pascal  [Select][+][-]
  1. for i := 0 to 6 do
  2.     if MyBoxes[i].Brush.Color = clmenu then
  3.     begin
  4.       timer2.enabled:=true;
  5.     end;        

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer2Timer(Sender: TObject);
  2. var
  3.    r:integer;
  4. begin
  5.   r:=random(6) ;
  6.   myboxes[r].brush.color:=clgreen;
  7. end;                                    

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin                                        
  3. timer2.interval:=random(2000)
  4. end;                            

i think it works but i don't want to make the box green when it is green already. can you help me coding with that?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #20 on: September 19, 2017, 05:27:56 pm »
First, I was wrong about Random(10) giving a number from 0 to 10. 10 is not included.
So you need Random(7); (or Random(max_boxes); where max_boxes is a global const in your program).

Then the line myboxes[r].brush.color:=clgreen;
If you don't want a green box to color green you can just do

Code: Pascal  [Select][+][-]
  1. if myboxes[r].brush.color <> clgreen then
  2.   myboxes[r].brush.color := clgreen;
and the code will take another box the next time the timer hits.

If you want to search for another colored box you need to check the color in a loop until you have a random number of a box that is not green. Try to make the loop with repeat until. After that loop you have a number r of a box that is not green.

(Note that this code with repeat might be tricky if all the boxes are already green. You could end up in a loop because you don't have any box to switch to green.)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #21 on: September 20, 2017, 02:01:52 am »
Thank you so much

Code: Pascal  [Select][+][-]
  1. <>
what is that sign for btw?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: canvas i need help
« Reply #22 on: September 20, 2017, 03:13:29 am »
@ rhong7:
The relation operator <> you meant ?

That means not equal to, e.g. x <> y means x not equal to y

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #23 on: September 20, 2017, 03:17:32 am »
also i would like to create the boxes underneath the boxes, so for example i want to create 2*5 boxes; 5 at the top and 5 underneath those 5 boxes. but all same sizes.

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #24 on: September 20, 2017, 09:34:08 am »
also i would like to create the boxes underneath the boxes, so for example i want to create 2*5 boxes; 5 at the top and 5 underneath those 5 boxes. but all same sizes.
You already had some code where you created a second MysBoxes (with an s). But in that case you would need to check 2 arrays to see if your white box hits one of them. It's easier to create all those boxes (including 2nd, 3rd and 4th row etc) in that one array.

You would still have your main loop to create those boxes:
Code: Pascal  [Select][+][-]
  1.   for i := 0 to (hplength - 1) do // dynamic arrays are 0-based
  2.   begin
  3.     MyBoxes[i] := TShape.Create(Self);
But set hplength to rows * boxes per row.

Before the loop you assign a special counter for Left (currentLeft) and Top (currentTop) the start values.
In the loop you assign currentLeft and currentTop to Left and Top of that Box.
Then you increase currentLeft with say 30 and check if that value is above 150 (or other value).
If it is, you reset currentLeft to the most left value again and increase top by 30.

I had some ready made code but try to do it like above yourself first.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #25 on: September 20, 2017, 09:49:23 am »
what do you mean by special counter for  Left (currentLeft) and Top (currentTop) the start values? variables?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #26 on: September 20, 2017, 09:52:22 am »
what do you mean by special counter for  Left (currentLeft) and Top (currentTop) the start values? variables?
Yep, just local variables currentLeft and currentTop that you can use to advance in the loop per Box.
Initially you set them outside to loop to the left and top position where you want the first box to be.

Inside the loop you "monitor" the currentLeft position. If it exceeds a certain value (the maximum left value of the last box in that row) you set it back to the left position and you can increase currentTop so the next box will appear under the first box in a row below.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #27 on: September 20, 2017, 10:02:35 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. currentleft:integer;
  4. currenttop:integer;
  5. begin
  6.      SetLength(MyBoxes, 19); // reserve 7 spots for the array
  7.   for i := 0 to 18 do // dynamic arrays are 0-based
  8.   begin
  9.     currentleft:=i;
  10.     currenttop:=0;
  11.     if currentleft > 5 then
  12.     begin
  13.       currentleft:=i-6;
  14.       currenttop:=currenttop+20;
  15.      end;
  16.     MyBoxes[i] := TShape.Create(Self);
  17.     MyBoxes[i].Parent := Self;
  18.     MyBoxes[i].Left := 25 { left } + (20 * currentleft) + (5 * currentleft);
  19.  
  20.     MyBoxes[i].Width := 20;
  21.     MyBoxes[i].Shape := strectangle;
  22.     MyBoxes[i].Brush.Color := clGreen;
  23.     MyBoxes[i].pen.Color := clGreen;
  24.  
  25.     MyBoxes[i].Top := 20+currenttop;
  26.     MyBoxes[i].Height := 5;
  27.  
  28.  
  29.   end;                  
i did this but it only works for 1st and 2nd raw

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: canvas i need help
« Reply #28 on: September 20, 2017, 10:30:34 am »
Like I said... you need to set the initial values of currentLeft and currentTop outside (above) the loop. Otherwise you reset them for each box.

Next I see you use currentLeft for Boxnumber and currentTop for rownumber. That is also a way to go. I initially thought about real coordinates but this is also possible. If you do that you need to set the initial values outside the box to 1 for both currentLeft and currentTop.

I would do the check for currentLeft under the creation of the box. Under the "MyBoxes =" lines you increase currentLeft by 1 (you don't need i to do that) and THEN do the check for currentLeft > 5.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #29 on: September 20, 2017, 10:53:28 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. currentleft:integer;
  4. currenttop:integer;
  5. begin
  6.  
  7.    currentleft:=0;
  8.     currenttop:=0;
  9.      SetLength(MyBoxes, 19); // reserve 7 spots for the array
  10.   for i := 0 to 18 do // dynamic arrays are 0-based
  11.   begin
  12.     if currentleft > 5 then
  13.     begin
  14.       currentleft:=0;
  15.       currenttop:=currenttop+20;
  16.      end;
  17.     MyBoxes[i] := TShape.Create(Self);
  18.     MyBoxes[i].Parent := Self;
  19.     MyBoxes[i].Left := 25 { left } + (20 * currentleft) + (5 * currentleft);
  20.  
  21.     MyBoxes[i].Width := 20;
  22.     MyBoxes[i].Shape := strectangle;
  23.     MyBoxes[i].Brush.Color := clGreen;
  24.     MyBoxes[i].pen.Color := clGreen;
  25.  
  26.     MyBoxes[i].Top := 20+currenttop;
  27.     MyBoxes[i].Height := 5;
  28.  
  29.    currentleft:=currentleft+1;
  30.  
  31.  
  32.   end;
  33.  
  34.  
  35. end;            

hi i did it but i don't understand how

 
Code: Pascal  [Select][+][-]
  1.  currentleft:=currentleft+1;
works it does not have i in it, how can it increase by one as the i increase 1?

 

TinyPortal © 2005-2018