Jeżeli to ma być prosty tetris to wystarczy malowanie po canvasie formy lub przez winapi: nie ma sensu bawienie się w directdraw itp.
Wstaw na forme komponent TTimer, parametr interval zmienia czestotliwosc pojawiania sie zdarzenia OnTimer.
Do tego dorób metodę formy dla zdarzenia OnPaint i maluj po formie.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
shapes :array[0..6] of array[0..3,0..3] of byte =
(
(
(0,0,0,0),
(0,0,0,0),
(1,1,1,1),
(0,0,0,0)
),
(
(0,0,0,0),
(0,1,0,0),
(0,1,1,1),
(0,0,0,0)
),
(
(0,0,0,0),
(0,0,0,1),
(0,1,1,1),
(0,0,0,0)
),
(
(0,0,0,0),
(0,1,1,0),
(0,1,1,0),
(0,0,0,0)
),
(
(0,0,0,0),
(1,1,0,0),
(0,1,1,0),
(0,0,0,0)
),
(
(0,0,0,0),
(0,0,1,1),
(0,1,1,0),
(0,0,0,0)
),
(
(0,0,0,0),
(0,1,0,0),
(1,1,1,0),
(0,0,0,0)
)
);
bricksize=16;
playwidth=10;
playheight=20;
playwidthpx=bricksize*playwidth;
playheightpx=bricksize*playheight;
type
TTetromino= record
typ: integer;
x: integer;
y: integer;
color: TColor;
end;
var
vscreen: TBitmap;
playfield: array[0..playheight-1, 0..playwidth-1] of byte;
tetromino: TTetromino;
implementation
{$R *.dfm}
procedure PaintBrick(x,y:integer; color: TColor=clRed);
begin
if (x>=0)and(y>=0)and(x<playwidth)and(y<playheight) then
begin
vscreen.Canvas.Brush.Color:=color;
vscreen.Canvas.FillRect(Rect(x*bricksize,y*bricksize,(x+1)*bricksize,(y+1)*bricksize));
end;
end;
procedure PaintPlayField();
var
i,j:integer;
begin
vscreen.Canvas.Brush.Color:=clBlack;
vscreen.Canvas.FillRect(Rect(0,0,playwidthpx,playheightpx));
for i:=0 to playheight-1 do
for j:=0 to playwidth-1 do
if playfield[i,j]>0 then
PaintBrick(j,i);
for i:=0 to 3 do
for j:=0 to 3 do
if shapes[tetromino.typ][i,j]<>0 then
PaintBrick(tetromino.x+j,tetromino.y+i,clBlue);
end;
procedure NewTetromino();
begin
tetromino.typ:=random(7);
tetromino.y:=-4;
tetromino.x:=playwidth div 2 - 2;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
vscreen:=TBitmap.Create;
vscreen.Width:=playwidthpx;
vscreen.Height:=playheightpx;
Randomize();
NewTetromino();
Paint();
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
PaintPlayField;
Form1.Canvas.Draw(0,0,vscreen);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(tetromino.y);
if tetromino.y=playheight then NewTetromino();
Form1.Repaint;
end;
end.
Pamiętaj o przypisaniu zdarzenia do timerka Timer1Timer do timera.
Na początek powinno Ci wystarczyć.. eksperymentuj dalej sam.
Edit3: no i na OnPaint formy też musisz ustawić FormPaint ktore zrobilem...