Você está na página 1de 25

DELPHI

: 1.
Lesson .. , .

data .
2.
www.programmersforum.ru
1.

Delphi ( ).

WINDOWS
GDI
(Graphics Devices Interface - ). GDI
, (
Delphi )
,
( DC (Display Context)).

, (, ).
Delphi ,
:
- TCanvas - ;
- TFont - ;
- TPen - ;
- TBrush - .
- canvas,
font, pen, brush, , , .
Delphi :
TGraphic, TPicture, TImage, TBitMap, TJpegImage, TShape, TIcon, TMetaFile .
TCanvas.

, anvas.
TPoint
TRect, :
TPoint = record
//
X : LongInt ;
Y : LongInt ;
TRect = record
//
Left : Integer ;
Top : Integer ;
Right : Integer ;
Bottom : Integer ;

TRect = record
//
TopLeft : TPoint ;
BottomRight : TPoint ;
:
TRect := Bounds ( X,Y, Width, Height : Integer );

X, Y - ;
Width, Height - ;
(
) TCanvas.
,
, / ,
.
()
:
- Canvas. < ( ) > Arc ( ), Pie ( ),
Ellipse ( , ), Rectangle () .
- Canvas.Draw - ;
- Canvas.StretchDraw - ;
- Canvas.CopyRect - ;
- Canvas. TextOut - ;
,
.
2 . .
Delphi File => New => Application( Delphi
). C - File => SaveAll.
- Delphi - Unit 1.
Project 1 Lesson 1.
Delphi Form1 Unit 1
.

Object Inspector Form1 Caption


1 :

Top - 100
Left - 230
Width - 700
Height - 575
ClientWidth - 700
ClientHeight - 540 // ( ).
.
Object Inspector Events ()
OnPaint.
On Paint.

( Run F9 ,
) .
, ,
( - .bmp ).
, , data
.
( ship1.bmp )
( star1.bmp ). ship1 (
), star1
700 540 (
Object Inspector ).
TBitMap :
BufFon - star1.bmp ;
BufSpr - ship1.bmp ;
BufPic - BufSpr ;
Buffer -
.
BufFon BufSpr
. Buffer BufFon ,
BufPic - ,
:
BufPic.Width := round ( BufSpr.Width / n ) ;
BufPic.Height := round ( BufSpr.Height / m ) ;
n - - sprite ;
m - .
TForm1.FormCreate(Sender: TObject);

BufPic
BufSpr BufPic CopyRect
procedure DrawShip1 ( i: byte); //
begin
BufPic.Canvas.CopyRect(bounds(0, 0, BufPic.Width, BufPic.Height),
BufSpr.Canvas,bounds( i * 66, 0, BufPic.Width, BufPic.Height));
end;
:
Canvas BufPic X= 0 Y = 0,
BufPic
X= i * 66 Y = 0,
BufPic.
66 . i
( 0 - 1-, 1 2-).
(
) TForm1.FormPaint(Sender: TObject);
.
xS1 yS1 - .
DrawShip1 ( 0 ) c 0
BufPic. Buffer
.


.
.

Lesson1.
3. ( ).


dX dY.

. ,
,
, .
. .1 .
,
.

,
. , ,
, ,

.
(
TBitMap), .

.
-

(
) . .2 .
,
.2



(
).

,

( ) , ,
dX dY ( )

.
.
,
. , .3,
.

, .4


,
.3.

,
.



dx dy.

dx > 0 dy > 0 , bx = dx by = dy;


dx > 0 dy <= 0, bx = dx by = 0;
dx <= 0 dy > 0 , bx = 0 by = dy;
dx <= 0 dy <= 0, bx = 0 by = 0;
bx by ,
dx dy.
:
********************************************************************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormActivate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
{ , .
, }
Pic, Fon, Buf : TBitMap;
//
RectFon, RectBuf : TRect;
//
x,y: integer;
//
dx, dy: integer;
// .
W,H,WB,HB: integer;
implementation
{$R *.dfm}
procedure TForm1.FormActivate(Sender: TObject);
begin
// TBitMap TRect
//
Fon:= TBitMap.Create;
Fon.LoadFromFile('fon.bmp');
{ ,

}
Pic:= TBitMap.Create;
Pic.LoadFromFile('picture.bmp');
//
Pic.Transparent:= true;
Pic.TransparentColor:= Pic.Canvas.Pixels[1,1];
{
. }
W:= Pic.Width;
H:= Pic.Height;
Buf:= TBitMap.Create;
Buf.Width:= W + 2*abs(dx);
Buf.Height:= H + 2*abs(dy);
WB:= Buf.Width;
HB:= Buf.Height;
//
Buf.Palette:= Fon.Palette;
Buf.Canvas.CopyMode:= cmSrcCopy;
{ .
}
RectBuf:= Bounds(0,0,WB,HB);
//
x:=300; y:=150;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin //
Form1.Canvas.Draw(0,0,Fon);
Form1.Canvas.Draw(x,y,Pic);
end;
{
}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
37: begin dx:= -3; dy:= 0; end;
38: begin dx:= 0; dy:= -3; end;
39: begin dx:= 3; dy:= 0; end;
40: begin dx:= 0; dy:= 3; end;
end;
{ ,
}
RectFon:= Bounds(x+dx,y+dy,WB,HB);
//
Buf.Canvas.CopyRect(RectBuf,Fon.Canvas,RectFon);
//
Buf.Canvas.Draw(abs(dx),abs(dy),Pic);
//
x:=x+dx; y:=y+dy;
Form1.Canvas.Draw(x,y,Buf);
end;
end.
*************************************************************************
:

**************************************************************************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormActivate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
{ , .
, }
Pic, Fon, Buf : TBitMap;
//
RectFon ,RectBuf : TRect;
//
x,y: integer;
//
dx, dy: integer;
{
}
bx, by: integer;
// .
W,H,WB,HB: integer;
implementation
{$R *.dfm}
procedure TForm1.FormActivate(Sender: TObject);
begin
// TBitMap TRect
//
Fon:= TBitMap.Create;
Fon.LoadFromFile('fon.bmp');
{ ,
}
Pic:= TBitMap.Create;
Pic.LoadFromFile('picture.bmp');
//
Pic.Transparent:= true;
Pic.TransparentColor:= Pic.Canvas.Pixels[1,1];
{
. }
W:= Pic.Width;

H:= Pic.Height;
Buf:= TBitMap.Create;
Buf.Width:= W + abs(dx);
Buf.Height:= H + abs(dy);
WB:= Buf.Width;
HB:= Buf.Height;
//
Buf.Palette:= Fon.Palette;
Buf.Canvas.CopyMode:= cmSrcCopy;
{ .
}
RectBuf:= Bounds(0,0,WB,HB);
//
x:=300; y:=150;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin //
Form1.Canvas.Draw(0,0,Fon);
Form1.Canvas.Draw(x,y,Pic);
end;
{
}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
37: begin dx:= -3; dy:= 0; end;
38: begin dx:= 0; dy:= -3; end;
39: begin dx:= 3; dy:= 0; end;
40: begin dx:= 0; dy:= 3; end;
end;
if (dx>0) and (dy>0) then begin bx:=abs(dx); by:=abs(dy); end;
if (dx>0) and (dy<=0) then begin bx:=abs(dx); by:=0; end;
if (dx<=0) and (dy>0) then begin bx:=0; by:=abs(dy); end;
if (dx<=0) and (dy<=0) then begin bx:=0; by:=0; end;
{ ,
}
RectFon:= Bounds(x+dx,y+dy,WB,HB);
//
Buf.Canvas.CopyRect(RectBuf,Fon.Canvas,RectFon);
//
Buf.Canvas.Draw(bx,by,Pic);
//
x:=x+dx; y:=y+dy;
Form1.Canvas.Draw(x,y,Buf);
end;
end.
*****************************************************************
,
. .
,
picture.bmp . , .
.
. CopyRect

TBitMap , .

.

TBitMap, CopyRect , ,
, Canvas.Draw
.
, , .
.
.

4. .
2 Lesson2.
Form1 2,
Form1 2.
, ( )
, .
. star1.bmp star2.bmp.
BufFonDop.
,
.

2
TForm1.FormCreate(Sender: TObject) ( . ).
:

DrawShip1( i ) .

.
(50 ) :
- yf dyf (+ 2);
- yS1 dyS1 ( - 2 ).
,
;
- (Buffer) (
);
-
dyS1;
- .
DrawShip1( ns ) ns ,
0 1 , .


TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);

Lesson2.
.
5. .
Lesson 3 .
( data .
- 100 80 ).
( Randomize) .
'ship1'
Lesson 2. BufShipR BufPicR .
-
.
'ship 1' 'ship 2' - 'ship 5' , .

TForm1.FormCreate(Sender: TObject)
.
DrawShipR(i,j: byte) ( 'ship2' 'ship5')
: i - j -
. ..
j , BufShipR
DrawShipR(i,j: byte).

. 'ship2' 'ship5' Buffer xR1, yR1 xR2, yR2 .


dyR1
, dxR2 dyR2 - . ,
, random( )
xR1, xR2 ( nr1, nr2).
yR1, yR2 yS1 , 'ship1'
.

random ( 4 ) 0 .. 3,
2 .. 5.
BufShipR.LoadFromFile('data/ship' + IntToStr(j+2) + '.bmp');
- ... IntToStr(j + 2) ...

Lesson 2.
Lesson 3.
6. .
Lesson 3, ,
( ,
, ..). , ,

. ,
?
. , .
,
.
LoadObjectToBufferMod ,

( , ) ,
.
Lesson 4.
:
- TBitMap
TRect ;
- InitFon () LoadFon
( ) ;
- InitSprite ;
- TBitMap.
InitBuff , FreeBuff
, .. , ;
- LoadBuff .
:

{ (BufFon BufFonD ( - Create)


( InitFon
OnCreate OnActivate). InitBuff,FreeBuff,InitSprite,LoadBuf . Buffer,BufSprite,BufPic
.
Buffer,BufSprite,BufPic Free.}
*************************************************************************************
n - , c
1024 1024. LoadFon n -
FonName xf yf.
( SpriteName).
N_goriz N_vertic = 1;
, bs = 0;
*** - .jpg. TBitMap
TJpegImage uses LoadObjectToBufferMod uses Unit1 Jpeg.
!!! uses Unit1 !!!
:
1. - FormActivate( FormCreate) .
procedure InitFon(nw,nh: byte; FonName: string) .
- n procedure LoadFon(xf,yf: integer; FonName: string),
.
- Buffer, procedure InitBuff.
.
2.
function InitSprite(SpriteName: string; N_goriz,N_vertic,N_stroka, N_kadr: byte).

. (N_kadr)
. ,
N_kadr .

, procedure LoadBuff(xf,yf,xs,ys,bs: integer),


.
Draw.
7. .
LoadObjectToBufferMod .
Lesson 4.
uses var
ship1 . .

TForm1.FormCreate(Sender: TObject)
.

InitFon . 1 -
, 2 - , data/star1.bmp -
BufFon . loadFon
BufFon.
InitBuff - Buffer .
:

InitSprite (
ship1),
. LoadBuff
TRect .
FreeBuff
Buffer Buffer
.
.

. Lesson 3.
, .
Lesson 4_1.
Lesson 4 ,
( ship4 ship7.
).
var .

InitSprite Nspr -
ns - ,
random
.
ship1 FormKeyDown.
Lesson 4.

8. Lesson 4.
Lesson 4 , , . ship1
.
(bullet) (explo) data.
Lesson 4.
Lesson 5.
Lesson 4 Delphi. - File =>
Save Project as - Lesson5
. Lesson5.
:
- Lesson4
;
- Lesson4
Delphi project;
- Lesson4
RES;
- Lesson4.dof DOF;
- Lesson4
Application
Lesson5 Delphi.

vistrel flag_S Boolean


.
ship4 ship7 ,

, (
).

FormKeyDown - .

-
vistrel : = true;

Lesson 5.

.
.

(: count1, count2) Font (Size,Style,Color)
TextOut(X,Y, IntToStr(count..) .
www.programmers.ru

(aka DeKot)

degvv@mail.ru

E-mail .
2009 .

Você também pode gostar