Recent

Author Topic: simple snake game + color conversion  (Read 10595 times)

zoiddani

  • New Member
  • *
  • Posts: 29
simple snake game + color conversion
« on: December 20, 2010, 01:00:06 pm »
Hi,
I've been trying to make a snake game in lazarus, but i encountered the following problem:
I use a TImage's canvas to draw on. The main procedure is called by a button, and in that procedure there's a repeat cycle. However, nothing is drawn, and the button stays 'clicked'. I put also a sleep statement in the code, but that didn't help.
I think it's because the program waits until everything is done. How can I avoid this problem?

Also, how can I convert an integer to a color. I know I can use colors like this: Pen.Color:= #FFFFFF, but I can't do Pen.Color:= #aninteger. After converting the integer to a hexadecimal number, I get a word, so how could I do it?

Thank you for any help!
 


zoiddani

  • New Member
  • *
  • Posts: 29
Re: simple snake game + color conversion
« Reply #1 on: December 20, 2010, 01:36:25 pm »
Update: first one partly solved with putting an Image.Refresh in the repeat cycle. However, the form won't recognise that I pressed a key...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: simple snake game + color conversion
« Reply #2 on: December 20, 2010, 01:41:53 pm »
Try the attached project

Imants

  • Full Member
  • ***
  • Posts: 198
Re: simple snake game + color conversion
« Reply #3 on: December 20, 2010, 01:50:20 pm »
Better try TPaintBox it works better for such task.
I recommend to use TTimer component or Application.OnIdle

Timer example (interval=20 for 50 fps):

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  PaintBox1.Canvas.Pen.Color := clWhite;
  PaintBox1.Canvas.Brush.Color := clWhite;
  PaintBox1,Canvas.Rectangle(0, 0, PaintBox1.Width, PaintBox1.Height);//clears screan
  DrawScene(PaintBox1.Canvas);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer1.Enabled := False ;
end;

DrawScene will contain all drawing. I think that it is most easy way to implement animation.

You can assign color as integer like this:
PaintBox1.Canvas.Brush.Color := 1000;  //without #

zoiddani

  • New Member
  • *
  • Posts: 29
Re: simple snake game + color conversion
« Reply #4 on: December 20, 2010, 02:18:35 pm »
Try the attached project

I've tried it, and got the following error: Error while linking. c:\lazarus\fpc\2.2.4\bin\i386-win32\windres.exe: can't open file `TForm1': No such file or directory

@Imants: I will try it, thanks for your help!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: simple snake game + color conversion
« Reply #5 on: December 20, 2010, 02:21:43 pm »
Quote
I've tried it, and got the following error: Error while linking. c:\lazarus\fpc\2.2.4\bin\i386-win32\windres.exe: can't open file `TForm1': No such file or directory
Sorry, I use Lazarus svn. Please delete {$R *.lfm} and add:
Code: [Select]
initialization
  {$I unit1.lrs}
to the end of unit1 (before end.)

zoiddani

  • New Member
  • *
  • Posts: 29
Re: simple snake game + color conversion
« Reply #6 on: December 20, 2010, 02:30:11 pm »
  DrawScene(PaintBox1.Canvas);

I understand your code, except this (Drwascene) part.
Is this a procedure? If yes, how should I declare it?
Thanks!

Imants

  • Full Member
  • ***
  • Posts: 198
Re: simple snake game + color conversion
« Reply #7 on: December 20, 2010, 03:27:29 pm »
It can be as separate procedure or as part of class.

TForm1 = class(TForm)
public
  Procedure DrawScene(ACanvas: TCanvas); 
end;

Procedure TForm1.DrawScene(ACanvas: TCanvas);
Begin

end;

//Modified Timer function to stop flickering
procedure TForm1.Timer1Timer(Sender: TObject);
var
  BMP: TBitmap;
begin
  BMP := TBitmap.Create;
  BMP.SetSize(PaintBox1.Width, PaintBox1.Height);
  DrawScene(BMP .Canvas);
  PaintBox1.Canvas.Draw(0, 0, BPM);
  BMP.Free
end;




 

TinyPortal © 2005-2018