stop flicker on game in Delphi 32

Posted on 2009-05-17
Last Modified: 2013-12-27
How do i stop the flicker in the 2D game engine on the following web site the frames should render smoothly so that there is no flicker in the frame changes.  There is a transparent panel (uses the Tapanel component) for a base of the object and a animate image component to animate the images
The animate image component works on its own to do image processing but when
combined with a scrolling background there is flicker in the image when it is non scrolling (some flicker is to be expected in the scrolling image) to run the demo just move the mouse to the right edge of the screen and movement will start. (there is a exe in the download so you can also see the problem if you don't have delphi 32 ... i think the solution lies in invalidating the draw at the right point perhaps as it may be drawing the background too often........ its for a open source game any hint? thanks
Question by:venvirupa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 21

Expert Comment

ID: 24414756
The practice of using multiple timers is... problematic at best.  Use one timer and fire any events you need from there.  Normally games are done differently from normal windows applications, but in this case I guess you wanted to do it the normal Delphi way.

Place a timer and a PaintBox on a form and make the paint box align client.  Use the following code to see smooth side scrolling with no flicker.  The side scrolling is slow but that is based on the combination of the timer interval and the number of pixels you move per timer event.  This example does a weak job of setting up a background similar to a very old arcade game.  The setup is not perfect, just for an easy to set up example.  

Let me know if you need more.
unit Unit1;
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    { Private declarations }
    fBackground : TBitmap;
    PanX : integer;
    function RandomHeight : integer;
    { Public declarations }
  Form1: TForm1;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
  TieHeight : integer;
  X, Y : integer;
  LastPoint : TPoint;
  StarColor : TColor;
  fBackground := TBitmap.Create;
  with fBackground do
      Width := 2000;
      Height := 500;
      Canvas.Brush.Color := clBlack;
      //Canvas.Pen.Color := clBlack;
      Canvas.FloodFill(0, 0, clBlack, fsBorder);
      //do the mountains
      fBackground.Canvas.Pen.Color := clWhite;
      TieHeight := RandomHeight;
      X := 0;
      Y := TieHeight;
        LastPoint := Point(X, Y);
        if X = 0 then
          fBackground.Canvas.MoveTo(X, Y)
          fBackground.Canvas.LineTo(X, Y);
        inc(X, (Random(5) + 1) * 25);
        Y := RandomHeight;
      until X >= 1975;
      fBackground.Canvas.LineTo(2000, TieHeight);
      //Do the stars
      TieHeight := RandomHeight;
      X := 0;
        case Random(5) of
          0 : StarColor := clRed;
          1 : StarColor := clBlue;
          2 : StarColor := clGreen;
          3 : StarColor := clYellow;
          4 : StarColor := clWhite;
        fBackground.Canvas.Pixels[x, Y] := StarColor;
        inc(X, (Random(5) + 1));
        Y := Random(300);
      until X >= 2000;
  DoubleBuffered := true;
procedure TForm1.FormDestroy(Sender: TObject);
procedure TForm1.PaintBox1Paint(Sender: TObject);
  SR, DR : TRect;
  SR := Rect(PanX, 0, PaintBox1.Width + PanX, PaintBox1.Height);
  DR := Rect(0, 0, PaintBox1.Width, PaintBox1.Height);
  PaintBox1.Canvas.CopyRect(DR, fBackground.Canvas, SR);
function TForm1.RandomHeight: integer;
  Result := Random(100) + 300;
procedure TForm1.Timer1Timer(Sender: TObject);
  inc(PanX, 1);

Open in new window

LVL 21

Expert Comment

ID: 24414801
P.S. You will need to manually tie the timer event and the paintbox OnPaint events up.
LVL 19

Accepted Solution

Barthax earned 500 total points
ID: 24419464
You could set the panel to be DoubleBuffered in a create method.  This should prevent the flicker.

Panel1.DoubleBuffered := true;
LVL 19

Expert Comment

ID: 24431479
I knew there was another way, via handling the windows messages themselves.  Good article at on flicker-free covers this.

Author Comment

ID: 24437319
points to Barthax thanks i had seen that page on flicker hacks but lost it in my messy filesystem the panel1.doublebuffered did the trick although now the images appear progressing sideways each frame but thanks anyway I have a new line of enquiry to follow and thanks to developmentGuru for the paintbox code that gives me a line too although further from my current code base  maybe you can split the points (if that can be done)? up to you Barthax

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question