Questions lazarus / 9-Editor ---------------------------------------------------------------------- gyts · 2010-10-18 10:12 How to make procedure that would place Shape when Form opens. How to make fixed window (that couldn't be resized)? ---------------------------------------------------------------------- typo · 2010-10-18 10:21 Fixed window: set BorderStyle to bsSingle or bsDialog. ---------------------------------------------------------------------- gyts · 2010-10-19 11:22 How to draw triangle? Do i need to paint it by myself and load it as a picture? If so, how to do that? ---------------------------------------------------------------------- typo · 2010-10-19 11:26 Well, if you draw it by yourself you need 3 points and need to Line to them on the canvas. Code: [Select]Canvas.Line(x1, y1, x2, y2); ---------------------------------------------------------------------- gyts · 2010-10-21 12:42 How to make shapes array? ---------------------------------------------------------------------- typo · 2010-10-21 13:39 Code: [Select]var   a :array[0..N] of TShape; ---------------------------------------------------------------------- gyts · 2010-10-21 14:59 Quote from: typo on October 19, 2010, 01:26:16 pmWell, if you draw it by yourself you need 3 points and need to Line to them on the canvas. Code: [Select]Canvas.Line(x1, y1, x2, y2); How to make, this triangle, into shape, that can be moved on a button press? How to change it's line color, fill color? ---------------------------------------------------------------------- ivan17 · 2010-10-21 20:02 QuoteHow to make, this triangle, into shape, that can be moved on a button press? How to change it's line color, fill color? canvas has a "pen" property. change its properties (color/style) to set line styles; canvas has a "brush" property. change its properties (color/style) to set fill styles; canvas has "font" property. change its properties to set how text is drawn; handle Click event of a button or MouseDown/MouseUp/MouseMove events of the TPaintBox/TPanel or wherever you're doing the drawing. once you feel its time to move the shape, set the color to background (clBtnFace/clWhite/whatever), draw a triangle at old coordinates, acquire new coordinates, set hte color to foreground and draw the triangle at new coordinates. also invoke the drawing routine from OnPaint event ---------------------------------------------------------------------- gyts · 2010-10-22 10:43 How to fill painted triangle? ---------------------------------------------------------------------- Leledumbo · 2010-10-22 11:14 QuoteHow to fill painted triangle?Google for "triangle fill algorithm" (with quotes) or "polygon fill" (without quotes). The latter is more generic and AFAIK used by, for example, OpenGL. Hmm... I think triangle drawing should be added to TCanvas. ---------------------------------------------------------------------- ivan17 · 2010-10-22 11:35 Quote from: gyts on October 22, 2010, 12:43:55 pmHow to fill painted triangle? simplest option (not for serious programs): floodfill serious option: draw a family of adjacent horizontal lines (either do a bit of math or search the net for an algorithm) Quote from: Leledumbo on October 22, 2010, 01:14:43 pmHmm... I think triangle drawing should be added to TCanvas. you need someone else to implement moveto(point1); lineto(point2); lineto(point3); lineto(point1); for you? off topic, that got me going: someone (one one) someone (one one)  someone (one one) someone else (x2)... ---------------------------------------------------------------------- gyts · 2010-10-22 13:44 Quote from: typo on October 21, 2010, 03:39:37 pmCode: [Select]var   a :array[0..N] of TShape; How to put already created shapes into this array? I have ~100 shapes. ---------------------------------------------------------------------- typo · 2010-10-22 13:49 Code: [Select]a[N] := Shape1; or Code: [Select]for i := 0 to 99 do begin   a[i] := TShape(FindComponent('Shape' + IntToStr(i + 1))); end; ---------------------------------------------------------------------- gyts · 2010-10-22 13:55 How to make this work faster? Because in this way it will require 100 lines... I want program to be compact. ---------------------------------------------------------------------- typo · 2010-10-22 13:57 I have edited my previous message, have a look. ---------------------------------------------------------------------- gyts · 2010-10-22 14:28 Code: [Select]mas = array [1..1000] of TShape; var   A: mas;   number: integer; for i := 1 to 144 do    begin     A[i] := TShape(FindComponent('Shape' + IntToStr(i + 1)));    end; for i := 1 to 144 do    begin     Randomize;     number := random(5);     if number = (0 or 1)      then A[i].Brush.Color := clred      else if number = (2 or 3)            then A[i].Brush.Color := clgreen;    end; I put both cycles in TGame.FormPaint Why it doesn't colors my shapes? ---------------------------------------------------------------------- typo · 2010-10-22 14:46 Warning with the use of arrays, they are 0-based Code: [Select]for i := 1 to 144 do    begin     A[i] := TShape(FindComponent('Shape' + IntToStr(i + 1)));    end; It should be: Code: [Select]for i := 0 to 143 do    begin     A[i] := TShape(FindComponent('Shape' + IntToStr(i + 1)));    end; for 144 Shapes. QuoteWhy it doesn't colors my shapes? Try: Code: [Select]Application.ProcessMessages; ---------------------------------------------------------------------- typo · 2010-10-22 15:13 Your code has problems. Try: Code: [Select]if (number = 0) or (number = 1)      then A[i].Brush.Color := clred      else if (number = 2) or (number = 3)            then A[i].Brush.Color := clgreen; ---------------------------------------------------------------------- gyts · 2010-10-22 15:15 I've already fixed. Thanks Why i can't change loop variable? "unit1.pas(236,34) Error: Illegal assignment to for-loop variable "i"" ---------------------------------------------------------------------- typo · 2010-10-22 15:17 You simply can't change a FOR loop variable. That's all. ---------------------------------------------------------------------- ivan17 · 2010-10-22 15:18 Quote from: gyts on October 22, 2010, 05:15:07 pmWhy i can't change loop variable? turn the for-loop into a while loop if you need that; (if you just need to skip some elements, use if condition then Continue;) ---------------------------------------------------------------------- Leledumbo · 2010-10-23 07:11 QuoteWhy i can't change loop variable?For statement are meant for loop which terminating condition is predictable, so the compiler can do loop unrolling optimization if requested. Changing the loop variable will make it unpredictable, thus making the optimization impossible. Use while / repeat for loops whose terminating condition is unpredictable (i.e. uses sentinel). ---------------------------------------------------------------------- gyts · 2010-10-23 09:57 Quote from: typo on October 18, 2010, 12:21:31 pmFixed window: set BorderStyle to bsSingle or bsDialog. It's fixed, but it still can be tricked: http://img52.imageshack.us/img52/6837/21603160.png How to do that it couldn't be Maximized? ---------------------------------------------------------------------- typo · 2010-10-23 10:16 Set BorderIcons in order to remove it. ---------------------------------------------------------------------- gyts · 2010-10-23 12:03 It was already set up: http://img530.imageshack.us/img530/8774/45436372.png <..> When i maximize window, move it our of screen, shape colors messes, triangle gets back to basic position. How to save everything? ---------------------------------------------------------------------- ivan17 · 2010-10-23 21:27 Quote from: gyts on October 23, 2010, 02:03:35 pmWhen i maximize window, move it our of screen, shape colors messes, triangle gets back to basic position. How to save everything? you do ALL drawing in OnPaint event based on outside variables (coordinates, colors...); you set the variables in other events and tell the control to repaint itself. system will call OnPaint when you drag the window around... ---------------------------------------------------------------------- gyts · 2010-10-23 22:26 Quote from: ivan17 on October 23, 2010, 11:27:06 pmQuote from: gyts on October 23, 2010, 02:03:35 pmWhen i maximize window, move it our of screen, shape colors messes, triangle gets back to basic position. How to save everything? you do ALL drawing in OnPaint event based on outside variables (coordinates, colors...); you set the variables in other events and tell the control to repaint itself. system will call OnPaint when you drag the window around... Can you describe it much crearly? I don't understand it ---------------------------------------------------------------------- ivan17 · 2010-10-24 00:41 Quote from: gyts on October 24, 2010, 12:26:07 amCan you describe it much crearly? I don't understand it button click event (doesn't matter, can be a keyboard handler for arrow keys) Code: [Select]  point1 := random point in paintbox1;   point2 := random point in paintbox1;   point3 := random point in paintbox1;   PaintBox1.Refresh; paintbox-OnPaint event (can be any control with canvas) Code: [Select]  draw triangle using those variables now try moving it below screen bottom or resizing... ---------------------------------------------------------------------- Martin_fr · 2010-10-24 00:59 The OnPaint is described here... http://wiki.lazarus.freepascal.org/De veloping_with_Graphics#Draw_on_the_OnPaint_event But you should read the rest of the page too, to get a it of background, and learn some of the other options. ---------------------------------------------------------------------- gyts · 2010-10-24 05:48 I've put shapes' colors and starting canvas in FormActivate. In OnPaint i'm repainting canvas. So when i maximize and move window around it's working as i want. But i saw another problem: http://img138.imageshack.us/img138/4661/65251983.png. When i right click on it, or just click to minimize it, shapes' colors messes and canvas goes to starting position in that way sometimes making 2 canvas. ---------------------------------------------------------------------- ivan17 · 2010-10-25 03:14 you know, there's a limit to how much we can guess.  how about sharing some code with us, if you don't get it working yourself (though i'm sure you can get it to work by trying things out yourself)? ---------------------------------------------------------------------- gyts · 2010-10-25 14:59 Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,   ExtCtrls; type                                                                                         mas = array [1..1000] of TShape;   { TGame }   TGame = class(TForm)     Kubukai: TShape;     Shape87: TShape;     Shape88: TShape;         Shape143: TShape;     Shape144: TShape;     Desine2: TShape;     Desine3: TShape;     Shape16: TShape;     Desine4: TShape;     Desine5: TShape;     Desine6: TShape;     Desine7: TShape;     Desine8: TShape;     Desine9: TShape;     Desine10: TShape;     Desine11: TShape;     Desine12: TShape;     Shape17: TShape;     Shape18: TShape;     Shape19: TShape;     Shape2: TShape;     Shape20: TShape;         Shape3: TShape;     Desine1: TShape;     Kaire4: TShape;     Kaire2: TShape;     Kaire3: TShape;     Kaire11: TShape;     Kaire10: TShape;     Kaire9: TShape;     Kaire8: TShape;     Kaire7: TShape;     Kaire6: TShape;     Kaire5: TShape;     Kaire12: TShape;     Kaire1: TShape;     Shape81: TShape;     Shape82: TShape;     Shape83: TShape;     Shape84: TShape;     Shape85: TShape;     Shape86: TShape;     Wall: TShape;     Kaire: TShape;     Desine: TShape;     Apacia: TShape;     Virsus: TShape;     procedure FormActivate(Sender: TObject);     procedure FormKeyPress(Sender: TObject; var Key: char);     procedure FormPaint(Sender: TObject);   private   public   end; var   Game: TGame;   p, i, x, y: integer;   kiek: integer;   boo: boolean;   A, K, D: mas;   number, tr, Des: integer;   spalva: TColor;   ToLeft, ToDown: boolean; implementation { TGame } procedure TGame.FormActivate(Sender: TObject); begin    tr := 5;    Des := 0;    for i := 1 to 12 do     begin      K[i] := TShape(FindComponent('Kaire' + IntToStr(i)));     end;    for i := 1 to 144 do     begin      A[i] := TShape(FindComponent('Shape' + IntToStr(i)));     end;   Randomize;   kiek := 0;   boo := true;   for i := 1 to 132 do    begin     if boo = true      then       begin        number := random(5);        if (number = 0) or (number = 1)         then begin kiek := kiek + 1; A[i].Brush.Color := clred end         else if (number = 2) or (number = 3)               then begin kiek := kiek + 1; A[i].Brush.Color := clLime end               else boo := false;       end      else if i mod 12 = 0 then boo := true;    end;   for i := 1 to 12 do    begin     number := random (2);     if number = 0      then K[i].Brush.Color := clred      else if number = 1            then K[i].Brush.Color := clLime    end;   Canvas.Pen.Color := clyellow;   for i := 0 to 24 do    if i mod 2 = 0     then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 25 + 5 - i div 2)     else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 25 + 5 - (i - 1) div 2); end; procedure TGame.FormPaint(Sender: TObject); begin   Canvas.Pen.Color := clyellow;   for i := 0 to 24 do    if i mod 2 = 0     then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 25 + 5 - i div 2)     else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 25 + 5 - (i - 1) div 2); end; procedure TGame.FormKeyPress(Sender: TObject; var Key: char); begin   case Key of    'w': begin          Canvas.Pen.Color := clblack;          for i := 0 to 24 do           if i mod 2 = 0            then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 5 + 25 - i div 2)            else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 5 + 25 - (i - 1) div 2);           if tr = 0            then tr := 11            else tr := tr - 1;          Canvas.Pen.Color := clyellow;          for i := 0 to 24 do           if i mod 2 = 0            then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 5 + 25 - i div 2)            else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 5 + 25 - (i - 1) div 2);         end;    's': begin          Canvas.Pen.Color := clblack;          for i := 0 to 24 do           if i mod 2 = 0            then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 5 + 25 - i div 2)            else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 5 + 25 - (i - 1) div 2);          if tr = 11           then tr := 0           else tr := tr + 1;          Canvas.Pen.Color := clyellow;          for i := 0 to 24 do           if i mod 2 = 0            then Canvas.Line (353 - i, tr * 25 + 5 + i div 2, 353 - i, tr * 25 + 5 + 25 - i div 2)            else Canvas.Line (353 - i, tr * 25 + 5 + (i - 1) div 2, 353 - i, tr * 25 + 5 + 25 - (i - 1) div 2);         end;    'a': begin          p := tr;          y := 305;          spalva := TShape(FindComponent('Kaire' + IntToStr(p + 1))).Brush.Color;          boo := true;          If (ToLeft <> false) and (ToDown <> false)           then         end;    'd': begin          TShape(FindComponent('Desine' + IntToStr(Des + 1))).Brush.Color := TShape(FindComponent('Kaire' + IntToStr(tr + 1))).Brush.Color;          Des := Des + 1;          if Des = 12           then begin                 ShowMessage ('Game Over :(');                 Halt;                end;          for i := tr + 1 downto 2 do           TShape(FindComponent('Kaire' + IntToStr(i))).Brush.Color := TShape(FindComponent('Kaire' + IntToStr(i - 1))).Brush.Color;          number := random (2) + 1;          if number = 1           then TShape(FindComponent('Kaire' + IntToStr(1))).Brush.Color := clRed           else TShape(FindComponent('Kaire' + IntToStr(1))).Brush.Color := clLime;         end;   end; end; initialization   {$I unit1.lrs} end. ---------------------------------------------------------------------- ivan17 · 2010-10-25 20:29 move initialization into oncreate event. onactivate may trigger many times and it is not a place to initialize arrays. don't move the last (painting) part; if you really need it somewhere, call formpaint(nil), don't double the code.  replace halt with application.terminate. keypress event is meant for typing, not games. if you need to allow player to hold the key, go with keydown/keyup events (you can get several keydown events for each one keyup event) ---------------------------------------------------------------------- gyts · 2010-10-26 06:30 How to write text? How to change its size, color, etc. ? How to change wasd with arrow keys? ---------------------------------------------------------------------- eny · 2010-10-26 06:55 Pick any canvas and do something like: Code: [Select]...    Canvas.Font.Name := 'Verdana';    Canvas.Font.Size := 8;    Canvas.Font.Style := [fsBold];    Canvas.Font.Color := clNavy;    // etc...    Canvas.TextOut(42,42,'Howdy World!'); ---------------------------------------------------------------------- gyts · 2010-10-26 09:43 Quote from: eny on October 26, 2010, 08:55:23 amPick any canvas and do something like How to write on Shape? ---------------------------------------------------------------------- eny · 2010-10-26 09:46 Use the shape's canvas like AnyShape.Canvas.TextOut(...). ---------------------------------------------------------------------- gyts · 2010-10-26 09:51 Quote from: eny on October 26, 2010, 11:46:57 amUse the shape's canvas like AnyShape.Canvas.TextOut(...). It didn't worked... :/ Modify: AnyShape.Canvas.TextOut (x, y, 'Text'); Does this mean that text will be printed AnyShape.Left + x, AnyShape.Top + y? ---------------------------------------------------------------------- typo · 2010-10-26 10:02 Shape1.Canvas.TextOut(0,0,'text'); ---------------------------------------------------------------------- gyts · 2010-10-26 11:49 Quote from: typo on October 26, 2010, 12:02:49 pmShape1.Canvas.TextOut(0,0,'text'); It still doesn't work :/ Modify: Text appears only when i move window out of screen ---------------------------------------------------------------------- Leledumbo · 2010-10-26 12:04 QuoteText appears only when i move window out of screencall Invalidate (force painting). ---------------------------------------------------------------------- gyts · 2010-10-26 12:08 In which procedure i need put Invalidate? Before or after text? Modify: Fixed. Now working properly. ---------------------------------------------------------------------- gyts · 2010-10-27 13:52 How to write it in programing language: FindComponent whose .Left and .Top = ShapeX.Left and ShapeX.Top? Is it posible at all? ---------------------------------------------------------------------- typo · 2010-10-27 13:56 Code: [Select]if TShape(FindComponent('Desine' + IntToStr(Des + 1))).Brush.Color = clBlack then   Something; ---------------------------------------------------------------------- gyts · 2010-10-27 14:02 Read again. I'm comparing not colors, but Tops and Lefts Modify: I'm moving ShapeX around and if ShapeX's Left and Top = another shape's Left and Top, i need to change that shape's color. ---------------------------------------------------------------------- typo · 2010-10-27 14:13 I am trying to give you the right address to ideas for you to make your program, not to program by you. ---------------------------------------------------------------------- gyts · 2010-10-27 17:05 Is there any procedure in Lazarus, like Delay in Free Pascal? ---------------------------------------------------------------------- eny · 2010-10-27 17:15 Yes, but why would you need it? ---------------------------------------------------------------------- typo · 2010-10-27 17:25 BTW, what your background knowledge, Delphi, Turbo Pascal, another language? ---------------------------------------------------------------------- gyts · 2010-10-27 17:33 Quote from: typo on October 27, 2010, 07:25:25 pmBTW, what your background knowledge, Delphi, Turbo Pascal, another language? 1y of Free Pascal. I haven't been creating Units. Quote from: eny on October 27, 2010, 07:15:28 pmYes, but why would you need it? Shape moves to another place too quick. I need to delay it, so that shape movement could be seen. ---------------------------------------------------------------------- eny · 2010-10-27 17:51 Quote from: gyts on October 27, 2010, 07:33:50 pmShape moves to another place too quick. I need to delay it, so that shape movement could be seen. You could use a TTimer for that; move objects based on timer events. ---------------------------------------------------------------------- gyts · 2010-10-27 17:55 Timer is too complicated for me, that's why i want to use Delay. I was thinking about using timer if there's no Delay proc in Lazarus. ---------------------------------------------------------------------- eny · 2010-10-27 18:02 sleep(