Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
canvas i need help
« on: September 18, 2017, 12:38:21 pm »
i want the small square to bounce off from the canvas.rectangle when it's green and i want it to go through when it's red. please help me thank you.

please check the attachment

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #1 on: September 18, 2017, 01:00:18 pm »
You are checking for canvas.Brush.Color at the end of your Timer1Timer() (among the other coordinates).

But you need to check for a specific color at that point.
So you need to check with canvas.pixels[X,Y] what color exactly you have at X,Y and ONLY switch to red if that point is green.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #2 on: September 18, 2017, 01:19:54 pm »
You are checking for canvas.Brush.Color at the end of your Timer1Timer() (among the other coordinates).

But you need to check for a specific color at that point.
So you need to check with canvas.pixels[X,Y] what color exactly you have at X,Y and ONLY switch to red if that point is green.


can you help me coding with that please?

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #3 on: September 18, 2017, 01:23:14 pm »
You can start with changing the last part of Timer1Timer to this:

Code: Pascal  [Select][+][-]
  1.   for i := 1 to 7 do
  2.     if (b.top < yy + hh) and (b.left > ww * i + (g * (i - 1)) + (xx - ww)) and
  3.       (b.Left < ww * i + (g * (i - 1) + ww) + (xx - ww)) then // << remove the green check
  4.     begin
  5.       if canvas.Pixels[b.left, b.top] = clGreen then // add the green check on canvas.Pixels[x,y] here
  6.       begin
  7.         b.top := 50;
  8.         canvas.brush.color := clred;
  9.         canvas.pen.color := clred;
  10.         canvas.rectangle(ww * i + (g * (i - 1)) + (xx - ww), yy, ww * i + (g * (i - 1) + ww) + (xx - ww), yy + hh);
  11.       end;
  12.     end;

After that you have some small line you draw in green when the cursor hits the box which prevents the cursor to pass through it (probably in FormPaint). You would need to color the entire box red to fix that.
« Last Edit: September 18, 2017, 01:28:56 pm by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #4 on: September 18, 2017, 01:31:31 pm »
B.T.W. why are you not using TShapes for the boxes too? It would be much easier.

You can't control the FormPaint() which will redraw you boxes green again when the form needs to repaint.
With TShapes you don't have that problem.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #5 on: September 18, 2017, 02:35:05 pm »
B.T.W. why are you not using TShapes for the boxes too? It would be much easier.

You can't control the FormPaint() which will redraw you boxes green again when the form needs to repaint.
With TShapes you don't have that problem.


oh because i want to do i:= 1 to x so i can put any number in the, but i can not do that, right..?I thought i can't do that with the tshape.

also what do you mean by remove the green check and add the green check on canvas pixel?

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #6 on: September 18, 2017, 02:54:19 pm »
also what do you mean by remove the green check and add the green check on canvas pixel?
I gave you a snippet of code in which I explained that.
(See the comments in that snippet)

Quote
oh because i want to do i:= 1 to x so i can put any number in the, but i can not do that, right..?I thought i can't do that with the tshape.
You can do it if you create the TShapes dynamically.

You're correct if you put the TShape in the designer you have to know the number of boxes.
But if you create the TShapes in FormCreate you can do it in code.
Something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.      //...
  4.   private
  5.     { private declarations }
  6.     MyBoxes: array of TShape; // <----- add this to your forms private section
  7.   public
  8.  
  9. //...
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. var
  13.   i: integer;
  14. begin
  15.  
  16.   SetLength(MyBoxes, 7); // reserve 7 spots for the array
  17.   for i := 0 to 6 do // dynamic arrays are 0-based
  18.   begin
  19.     MyBoxes[i] := TShape.Create(Self);
  20.     MyBoxes[i].Parent := Self;
  21.     MyBoxes[i].Left := 25 { left } + (20 * i) + (5 * i);
  22.     MyBoxes[i].Top := 20;
  23.     MyBoxes[i].Height := 20;
  24.     MyBoxes[i].Width := 20;
  25.     MyBoxes[i].Shape := stSquare;
  26.     MyBoxes[i].Brush.Color := clGreen;
  27.   end;
  28.  
  29. end;

And if you want to check if your cursor (b) touches any of the boxes you can do this:
Code: Pascal  [Select][+][-]
  1. uses lclintf; // <-- you need to add this directly under implementation
  2.  
  3. //...
  4. procedure TForm1.Timer1Timer(Sender: TObject);
  5. begin
  6.   //...
  7.  
  8.   for i := 0 to 6 do
  9.   begin
  10.     if MyBoxes[i].Brush.Color = clGreen then // only act when box is green
  11.       if RectInRegion(CreateRectRgn(b.Left, b.Top, b.Width + b.Left, b.Height + b.Top), MyBoxes[i].BoundsRect) then
  12.       begin
  13.         b.top := 50; // set cursor to top=50.   // why?
  14.         MyBoxes[i].Brush.Color := clRed; // turn that box red
  15.       end;
  16.   end;
Inside the begin/end you can act on the touching of your cursor to one of the MyBoxes.
« Last Edit: September 18, 2017, 05:29:38 pm by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #7 on: September 19, 2017, 01:42:33 am »
Hi thank you it works very well. but when it goes through the red box it goes under the red box, so i can not see my white box while it's going through. is there any way i can code so that the white box will go above the red box?

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #8 on: September 19, 2017, 02:14:53 am »
You can do that in either two ways.

1) create your cursor/white box in runtime too. Creating it after the red boxes will make it appear in front..

2) use b.BringToFront after the creation of the red boxes in formcreate. (If it gives an error there move it to the timer1timer).

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #9 on: September 19, 2017, 06:33:12 am »
thank you and also i tried to creat two dfferent Tshapes in one form, but it doesn't work.

 private
     MyBoxes: array of TShape;
     Mysboxes: array of Tshape; 



   SetLength(MyBoxes, hplength); // reserve 7 spots for the array
  for i := 0 to (hplength-1) do // dynamic arrays are 0-based
  begin
    MyBoxes := TShape.Create(Self);
    MyBoxes.Parent := Self;
    MyBoxes.Left := ufo.left { left } + (w * i);
    MyBoxes.Top := ufo.top+50;
    MyBoxes.Height := 20;
    MyBoxes.Width := w;
    MyBoxes.Shape := strectangle;
    MyBoxes.Brush.Color := clGreen;
    MyBoxes.pen.Color := clGreen;
  end;

  boxlength:=10;
  ww:= 800 div boxlength;
  SetLength(MyBoxes, boxlength); // reserve 7 spots for the array
  for i := 0 to (boxlength-1) do // dynamic arrays are 0-based
  begin
    MysBoxes := TShape.Create(Self);
    MysBoxes.Parent := Self;
    MysBoxes.Left := form2.left { left } + (ww * i);
    MysBoxes.Top := ufo.top+100;
    MysBoxes.Height := 20;
    MysBoxes.Width := ww;
    MysBoxes.Shape := strectangle;
    MysBoxes.Brush.Color := clGreen;
    MysBoxes.pen.Color := clGreen;
  end;
                           

that's my coding for private and when i try to compile it, it says
"Project project 1 raised exception class 'External:SIGSEGV'.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: canvas i need help
« Reply #10 on: September 19, 2017, 06:40:44 am »
My guess:

Code: Pascal  [Select][+][-]
  1. for i := 0 to (hplength-1) do
  2.   begin
  3.     MyBoxes[i]        :=  ... // corect
  4.     MyBoxes           :=  ... // wrong
  5.     MyBoxes[i].Parent :=  ... // corect
  6.     MyBoxes.Parent    :=  ... // wrong
  7.  

« Last Edit: September 19, 2017, 06:42:38 am by Handoko »

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #11 on: September 19, 2017, 06:41:44 am »
Code: Pascal  [Select][+][-]
  1.   boxlength:=10;
  2.   ww:= 800 div boxlength;
  3.   SetLength(MyBoxes, boxlength); // reserve 7 spots for the array
  4.   for i := 0 to (boxlength-1) do // dynamic arrays are 0-based
  5.   begin
  6.     MysBoxes[i] := TShape.Create(Self);
  7.     MysBoxes[i].Parent := Self;
  8.  
   
Notice the second SetLength(MyBoxes. It should set the length for the mysBoxes. You forgot the s. (First highlighted line above)

You're using the second array so you need to set the length for that array too.

BTW... Please use the code tags (or use the #button above the editor when inserting code in your post.

@Handoko, code tags were missing so you didn't see the [ i ]. But they were there.
« Last Edit: September 19, 2017, 06:44:16 am by rvk »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: canvas i need help
« Reply #12 on: September 19, 2017, 06:44:46 am »
Oops, I forgot.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #13 on: September 19, 2017, 08:16:43 am »
Thank you so much guys
and i want to make it all green and all of them becomes red.

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: canvas i need help
« Reply #14 on: September 19, 2017, 09:29:57 am »
and i want to make it all green and all of them becomes red.
Some code?

If I use the code here, they become green.

 

TinyPortal © 2005-2018