AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Canvas Flackert bei Refresh
Thema durchsuchen
Ansicht
Themen-Optionen

Canvas Flackert bei Refresh

Ein Thema von stiftII · begonnen am 20. Sep 2009 · letzter Beitrag vom 10. Apr 2012
 
stiftII

Registriert seit: 2. Sep 2009
Ort: Cuxhaven
122 Beiträge
 
#1

Canvas Flackert bei Refresh

  Alt 20. Sep 2009, 17:06
Hi.

Ich probiere gerade mein erstes Computerspiel zuprogrammieren =). Und für den Anfang möchte ich erstmal mit dem Canvas Objekt arbeiten. Das Problem ist nur, dass es flackert wenn es schnell hintereinander neu gezeichnet wird.

Wie kann man sowas umgehen ?. Ich hab die Hauptfunktionen vom Spiel im "IdleEventHandler" geschrieben, kann man das so machen ?.

Den Canvas Lösche ich mit:
Form1.Canvas.Brush := Brush; //select brush
Form1.Canvas.FillRect(Form1.ClientRect); //redraw form

Und zeichne ich mit:
Form1.Canvas.Rectangle(x,y,100+x,100+y);
Form1.Canvas.Rectangle(x1,y1,x2,y2);

wieder neu

Hier Mein kompletter Quelltext:

Hoffe ihr könnt mir helfen ; /

Delphi-Quellcode:
unit delphigame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    function ColCheck:Boolean;
    procedure MovePlayer;
    procedure SpawnEnemy;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure IdleEventHandler(Sender: TObject; var Done: Boolean);

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  movingUp : boolean;
  movingDown : boolean;
  movingRight : boolean;
  movingLeft : boolean;
  x,y : Integer;
  oldx, oldy : Integer;
    x1,y1,x2,y2 : Integer;
    
   the_time : TDateTime;


  GameStarted : Boolean;

  Level: Integer;

  Enemies : Integer;

  EnemySpawned : Boolean;

implementation


procedure TForm1.IdleEventHandler(Sender: TObject; var Done: Boolean);
begin
  If GameStarted then
  begin


  Oldx := x;
  Oldy := y;

  MovePlayer;
  if (Mouse.CursorPos.x > Left) And (Mouse.CursorPos.x < (Form1.Width + Left)) then
    x:= Mouse.CursorPos.x - left-50;
  if (Mouse.CursorPos.y > top) And (Mouse.CursorPos.y < (Form1.height + top)) then
    y:=Mouse.CursorPos.Y - top-50;
  SpawnEnemy;


  Sleep(5);

  Form1.Canvas.Brush := Brush; //select brush
  Form1.Canvas.FillRect(Form1.ClientRect); //redraw form

  If ColCheck then
  begin
    Canvas.Brush.Color := clRed
  end else
    Canvas.Brush.Color := clBlack;




  Form1.Canvas.Rectangle(x,y,100+x,100+y);

  Form1.Canvas.Rectangle(x1,y1,x2,y2);

 // Label2.Caption := IntTOStr(x);
 // Label3.Caption := IntTOStr(y);
 {Do a small bit of work here}


  Done := false;
  If Colcheck then
  begin
  Button1.Visible := TRUE;
  Label4.Visible := TRUE;
  Label5.Caption := 'Score: '+IntToStr(Level);
  Label5.Visible := True;
  GameStarted := FALSE;
  end;

  end;
  end;
{$R *.dfm}

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    vk_left:
      begin
        movingLeft := True;
        movingRight := False;
      end;
    vk_right:
      begin
        movingLeft := False;
        movingRight := True;
      end;
    vk_up:
      begin
        movingUp := True;
        movingDown := False;
      end;
    vk_down:
      begin
        movingUp := False;
        movingDown := True;
      end;
 // vk_space: BulletFired;
  end;
end;

procedure TForm1.MovePlayer;
begin
  if movingUp AND (y>=0) then y:=y-2;
  if movingDown AND (y <= Form1.Height-100) then y:=y+2;
  if movingLeft AND (x>=0) then x:=x-2;
  if movingRight AND (x <= Form1.Width-100) then x:=x+2;
end;

function TForm1.ColCheck:Boolean;
begin
  //Check for collision.
  result := false;

  If ((x2 >= x) AND (x2<=x+100) AND (y1 >= y) AND (y1<=y+100)) OR
     ((x1 >= x) AND (x1<=x+100) AND (y1 >= y) AND (y1<=y+100)) OR
     ((x1 >= x) AND (x1<=x+100) AND (y2 >= y) AND (y2<=y+100)) OR
     ((x2 >= x) AND (x2<=x+100) AND (y2 >= y) AND (y2<=y+100)) then
  begin
    Label1.Caption := ' BOOOOOOOOOM ';
    result:=true;
  end;

end;

procedure TForm1.SpawnEnemy;
begin

  If not EnemySpawned then
  begin
    Randomize;
    Level := Level+1;
    x1 := 0+Random(Form1.Width);
    y1 := Form1.Height;
    x2 := x1+Random(50)+50;
    y2 := Form1.Height+Random(50)+50;
    Form1.Canvas.Rectangle(x1,y1,x2,y2);
    EnemySpawned := TRUE;
  end;

  //Move Enemy Upwards
  y1 := y1 - Level;
  y2 := y2 - Level;





  If y2 < 0 then
    EnemySpawned := FALSE;


end;


procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    vk_left : movingLeft := False;
    vk_right : movingRight := False;
    vk_up : movingUp := False;
    vk_down : movingDown := False;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

the_time := Time;
GameStarted := TRUE;
Application.OnIdle := IdleEventHandler;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GameStarted := TRUE;
Button1.Visible := FALSE;
Level := 1;
end;

end.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:47 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz