Recent

Author Topic: Why is the rectangle the same color after the ELSE condition?  (Read 1450 times)

Tomi

  • Full Member
  • ***
  • Posts: 102
In the FormPaint, this code always gives blue rectangles, even if the unit is not belong to the player, when the rectangle should be yellow.
What is wrong with this?

Code: Pascal  [Select][+][-]
  1. if numberofunits>0 then
  2.           begin
  3.             for edb:=0 to numberofunits-1 do
  4.             begin
  5.               if gameunits[edb].exists=true then
  6.               begin
  7.                if gameunits[edb].unitowner='player' then
  8.                   canvas.brush.Color:=clBlue
  9.                else
  10.                    canvas.brush.Color:=clYellow;
  11.                radarobj.left:=gameunits[edb].xplace;
  12.                radarobj.top:=gameunits[edb].yplace;
  13.                radarobj.right:=radarobj.left+2;
  14.                radarobj.bottom:=radarobj.top+2;
  15.                canvas.rectangle(radarobj);
  16.               end;
  17.             end;
  18. end;

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #1 on: May 30, 2023, 03:19:21 pm »
Try this:

Code: Pascal  [Select][+][-]
  1.   if numberofunits>0 then
  2.   begin
  3.     for edb:=0 to numberofunits-1 do
  4.     begin
  5.       if gameunits[edb].exists then
  6.       begin
  7.        if LowerCase(gameunits[edb].unitowner) = 'player' then
  8.          canvas.brush.Color:=clBlue
  9.        else
  10.          canvas.brush.Color:=clYellow;
  11.        radarobj.left:=gameunits[edb].xplace;
  12.        radarobj.top:=gameunits[edb].yplace;
  13.        radarobj.right:=radarobj.left+2;
  14.        radarobj.bottom:=radarobj.top+2;
  15.        canvas.rectangle(radarobj);
  16.       end;
  17.     end;
  18.   end;

We need to the whole source code to be sure what's wrong with your code. My guess is case issue, see line #7. It usually good to convert the string to upper/lower case before comparison if you're not sure the case of the text it compares to. But better, don't use string, use enum type instead:
https://wiki.freepascal.org/Enum_Type

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #2 on: May 30, 2023, 04:36:49 pm »
We need to the whole source code to be sure what's wrong with your code.
In my program, there are units controlled by the player and controlled by the computer, too. The unitowner field holds these two attributions in string format ('player' or 'enemy').
And I would like draw a blue rectangle on the radar in case of player's units, and yellow rectangles in case of enemy units.
But now the program draws blue rectangles in both cases. Only this piece of code handles this drawing. It seems good, but it is not...

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #3 on: May 30, 2023, 04:55:23 pm »
Can you provide the whole source code? Or the code after removing all other unnecessary items. Simply showing some lines of code isn't helpful because bug can be hidden in anywhere in the code. We need the the code that we can compile, run and inspect so we can tell you where goes wrong.

Copy all the source files into a new folder but excluding the binary (exe file), *.bak, lib and backup folders. Compress the folder and sent the zip file to the forum.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #4 on: May 30, 2023, 05:56:45 pm »
Add some debugln statements to inspect the content of relevant variables (e.g. gameunits[edb].unitowner).
It's unlikely (very unlikely) the compiler is wrong, mos likely you made a mistake somewehere.

Bart

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #5 on: May 31, 2023, 11:16:17 am »
Interesting and I don't know why, but the drawing with the two colors now works with this code:
Code: Pascal  [Select][+][-]
  1. if gameunits[edb].unitowner='player' then
  2.                   canvas.pen.Color:=clBlue
  3.                else
  4.                    canvas.pen.Color:=clYellow;
So, I used pen instead of brush:o

zeljko

  • Hero Member
  • *****
  • Posts: 1591
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #6 on: May 31, 2023, 02:35:37 pm »
Pen IS correct for rectangle, Brush is used to fill rectangle.
So if you want yellow rectangle with red border you should set:
Pen.Color := clRed;
Brush.Color := clYellow.

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Why is the rectangle the same color after the ELSE condition?
« Reply #7 on: June 01, 2023, 10:56:24 am »
A-ha; thanks, Zeljko!
Maybe the rectangle with 2x2 pixels dimensions only shows its border and not a filled area, therefore I couldn't see the correct colors(?).

 

TinyPortal © 2005-2018