📄 unit1.pas
字号:
{The Delphi Games Creator Demo Program
-------------------------------------
This demo shows the use of the new TDGCJumper sprite. In this game
you must collect the jewels one at a time and return them to the
start platform. You have infinate lives but when you die all the
previously collected jewels are lost.
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DGCILib, DGC, dgcspts, dgcslib, DGCSnd;
type
TForm1 = class(TForm)
DGCScreen1: TDGCScreen;
DGCImageLib1: TDGCImageLib;
DGCSpriteMgr1: TDGCSpriteMgr;
DGCAudio1: TDGCAudio;
DGCSoundLib1: TDGCSoundLib;
procedure DGCScreen1Initialize(Sender: TObject);
procedure DGCScreen1Flip(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure DGCScreen1CleanUp(Sender: TObject);
procedure DGCSpriteMgr1SpriteStopped(Sprite: TDGCSprite;
LimitsSide: TLimitsSide);
procedure DGCSpriteMgr1SpriteDirChange(Sprite: TDGCSprite;
LimitsSide: TLimitsSide);
procedure DGCSpriteMgr1SpriteJump(Sprite: TDGCSprite);
private
{ Private declarations }
Background: TDGCSurface;
CurrentPlatform: Integer;
CurrentJewel: Integer;
StartNewGame: Boolean;
JewelCount: Integer;
CheckSprite: Integer;
PlayStartJingle: Boolean;
procedure NewGame;
procedure YourDeadMan;
procedure DropJewel;
procedure DrawCollectedJewels;
procedure LevelCompleted;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Trace;
{$R *.DFM}
procedure TForm1.DGCScreen1Initialize(Sender: TObject);
begin
//Create Background
Background := TDGCSurface.Create(DGCScreen1.DirectDraw2, 640, 480);
Background.Tile(0, 0, DGCScreen1.Images[6], False);
//Create Animations for man
DGCSpriteMgr1.Animations.Add('manwalk', True);
with DGCSpriteMgr1.Animations[0] do
begin
SetFrames(8, 100, [0, 1]);
SetFrames(24, 100, [2, 3]);
end;
DGCSpriteMgr1.Animations.Add('manidle', True);
with DGCSpriteMgr1.Animations[1] do
begin
SetFrames(8, 100, [0]);
SetFrames(24, 100, [2]);
end;
DGCSpriteMgr1.Animations.Add('manjump', True);
with DGCSpriteMgr1.Animations[2] do
begin
SetFrames(8, 100, [4]);
SetFrames(24, 100, [5]);
end;
DGCSpriteMgr1.Animations.Add('man die', True);
with DGCSpriteMgr1.Animations[3] do
begin
SetFrames(0, 80, [0, 2]);
end;
//Create Platform animation
DGCSpriteMgr1.Animations.Add('platform', True);
with DGCSpriteMgr1.Animations[4] do
begin
SetFrames(8, 100, [7]);
SetFrames(24, 100, [7]);
end;
//Create water animation
DGCSpriteMgr1.Animations.Add('water', True);
with DGCSpriteMgr1.Animations[5] do
begin
SetFrames(8, 150, [8, 9]);
end;
//Create Jewel animation
DGCSpriteMgr1.Animations.Add('jewel', True);
with DGCSpriteMgr1.Animations[6] do
begin
SetFrames(8, 100, [10]);
end;
//Create Tick animation (used to acknowledge dropping of jewel)
DGCSpriteMgr1.Animations.Add('check', True);
with DGCSpriteMgr1.Animations[7] do
begin
SetAllDirections(100, [11]);
end;
//Create the sprites and initialise
NewGame;
end;
procedure TForm1.NewGame;
var
x: Integer;
begin
//Initialise
DGCSpriteMgr1.Sprites.FreeSprites;
//Add Man sprite
DGCSpriteMgr1.Sprites.AddJumper(0, 5, 380, 8, 3, 1);
with DGCSpriteMgr1.Sprites[0] as TDGCJumper do
begin
WalkAnimation := 0;
JumpAnimation := 2;
//If you want the man to jump like a space man try
//adding these lines.
//JumpSpeed := 0.03;
//JumpRange := 3;
//Try altering the maximum falling speed by adding
///this line
//MaxFallSpeed := 1;
end;
//Add Patform sprites (ID, x, y, direction, speed, anim#);
DGCSpriteMgr1.Sprites.AddBouncer(1, 0, 60, 8, 1, 4);
DGCSpriteMgr1.Sprites.AddBouncer(1, 420, 120, 24, 1, 4);
DGCSpriteMgr1.Sprites.AddBouncer(1, 240, 180, 8, 2, 4);
DGCSpriteMgr1.Sprites.AddBouncer(1, 100, 240, 24, 1, 4);
DGCSpriteMgr1.Sprites.AddBouncer(1, 300, 300, 8, 1, 4);
DGCSpriteMgr1.Sprites.AddBouncer(1, 50, 360, 24, 2, 4);
//The last platform is does not move and has as
//special id of 6
DGCSpriteMgr1.Sprites.AddBouncer(1, 0, 420, 24, 0, 4);
//Add Random Jewels
DGCSpriteMgr1.Sprites.AddStatic(4, 50, 10, 6);
DGCSpriteMgr1.Sprites.AddStatic(4, 130, 10, 6);
DGCSpriteMgr1.Sprites.AddStatic(4, 300, 10, 6);
DGCSpriteMgr1.Sprites.AddStatic(4, 450, 10, 6);
DGCSpriteMgr1.Sprites.AddStatic(4, 200, 150, 6);
DGCSpriteMgr1.Sprites.AddStatic(4, 400, 210, 6);
//Add Check (tick) sprite
CheckSprite := DGCSpriteMgr1.Sprites.AddBouncer(7, 50, 480, 0, 2, 7);
with DGCSpriteMgr1.Sprites[CheckSprite] as TDGCBouncer do
begin
AllActions := laBounce;
Limits := Rect(0, 300, 640, 515);
Visible := False;
Enabled := False;
end;
//Add the static water sprites
x := 0;
while x < 640 do
begin
DGCSpriteMgr1.Sprites.AddStatic(2, x, 480 - 24, 5);
Inc(x, 32);
end;
StartNewGame := False;
CurrentJewel := -1;
JewelCount := 0;
PlayStartJingle := True;
end;
procedure TForm1.DGCScreen1Flip(Sender: TObject);
var
n: Integer;
tx, ty: Integer;
mansfeetpos: Integer;
begin
DGCScreen1.Back.Draw(0, 0, Background, False);
DrawCollectedJewels;
DGCSpriteMgr1.Draw;
DGCSpriteMgr1.Update;
if PlayStartJingle then
begin
DGCAudio1.Sound[2].Replay; //Play Pacman Jingle
PlayStartJingle := False;
end;
if StartNewGame then
begin
NewGame;
exit;
end;
//Landed on Platform>
with DGCSpriteMgr1.Sprites[0] as TDGCJumper do
begin
//Reached bottom then dead
if (Y > 440) and Enabled then
begin
YourDeadMan;
exit;
end;
//Hit a jewel?
if CurrentJewel = -1 then
begin
for n := 0 to Pred(DGCSpriteMgr1.Sprites.Count) do
begin
//ID of 4 = Jewel
if (DGCSpriteMgr1.Sprites[n].ID = 4) and
(DGCSPriteMgr1.Sprites[n].Visible) and
(DGCSpriteMgr1.Sprites.Collision(0, n)) then
begin
DGCAudio1.Sound[4].Replay; //Play Beep Sound
CurrentJewel := n;
DGCSPriteMgr1.Sprites[n].Visible := False;
break;
end;
end;
end;
if JumpState <> jsJumping then
begin
mansfeetpos := Y + 36;
if CurrentPlatform <> -1 then
begin
DGCSpriteMgr1.Sprites[CurrentPlatform].Detach;
CurrentPlatform := -1;
end;
//Loop through platform sprites seing if I have landed on one
for n := 1 to 7 do
begin
//Get the sprite position
tx := DGCSpriteMgr1.Sprites[n].X;
ty := DGCSpriteMgr1.Sprites[n].Y;
//First check the Y (36=man height. 32=Platform width
if (mansfeetpos > ty) and (mansfeetpos < ty + 32) then
begin
//now check man X pos
if (X + 32 >= tx) and (X < tx + 32) then
begin
Y := ty - 35;
JumpState := jsNone;
CurrentPlatform := n;
//Attach the man sprite (0) to the platform
//This will mean all updates to the platform
//position are also applied to the man
DGCSpriteMgr1.Sprites[n].Attach(DGCSpriteMgr1.Sprites[0]);
break;
end;
end;
end;
//if the man has not landed on a patform then start falling
if CurrentPlatform = -1 then
JumpState := jsFalling
else
//If landed on static platform with a jewel then drop it
if (CurrentPlatform = 7) and (CurrentJewel <> -1) then
begin
DropJewel;
end;
end;
//Reached bottom then dead
if (Y > 430) and Enabled then
YourDeadMan
end;
end;
//This procedure is called when your little guy falls into the
//water and dies. An animation of him speedily floating up to
//the sky is displayed. An event is fired when he reaches the
//top of the screen which causes the game to be restarted
//(DGCSpriteMgr1SpriteStopped).
procedure TForm1.YourDeadMan;
var
n: Integer;
begin
DGCAudio1.Sound[3].Replay; //Play Scream
with DGCSpriteMgr1.Sprites[0] do
begin
//Remove any sprite attachments
for n := 1 to 7 do
DGCSpriteMgr1.Sprites[n].Detach;
//Disable/Hide player sprite and create die version
Visible := False;
Enabled := False;
n := DGCSpriteMgr1.Sprites.AddBouncer(3, X, Y, 0, 5, 3);
DGCSpriteMgr1.Sprites[n].AllActions := laStopOutSide;
end;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
//Escape Ends Game
if Key = #27 then
Close
else
if not DGCScreen1.FlippingEnabled then
begin
NewGame;
DGCScreen1.FlippingEnabled := True;
end;
end;
procedure TForm1.DGCScreen1CleanUp(Sender: TObject);
begin
Background.Free;
end;
procedure TForm1.DGCSpriteMgr1SpriteStopped(Sprite: TDGCSprite;
LimitsSide: TLimitsSide);
begin
//Note: Can't actually call NewGame here because it deletes
//all of the sprites and causes and error as this function
//passes in one of the sprites as a paramter. The only
//solution is to set a flag and test it in the page flipping
//code
if Sprite.ID = 3 then
StartNewGame := True;
end;
//This procedure is called when a collecte jewel if returned to
//the start platform. The code invokes the "check" sprite
//which indicates the jewel has been dropped.
procedure TForm1.DropJewel;
begin
DGCAudio1.Sound[0].Replay; //Play DropJewl
CurrentJewel := -1;
Inc(JewelCount);
with DGCSpriteMgr1.Sprites[CheckSprite] do
begin
Direction := 0;
Enabled := True;
Visible := True;
end;
end;
procedure TForm1.DGCSpriteMgr1SpriteDirChange(Sprite: TDGCSprite;
LimitsSide: TLimitsSide);
begin
//Sprite 7 is the "check" sprite. If the current direction
//is 0 then it must have hit the bottom and it's on it's way
//up again. The sprite should only go up and down once so
//disable and hide if this condition is true.
if (Sprite.ID = 7) and (Sprite.Direction = 0) then
begin
Sprite.Visible := False;
Sprite.Enabled := False;
if JewelCount = 6 then
LevelCompleted;
end;
end;
procedure TForm1.DGCSpriteMgr1SpriteJump(Sprite: TDGCSprite);
begin
DGCAudio1.Sound[1].Replay; //Play jump Sound
if CurrentPlatForm <> -1 then
DGCSpriteMgr1.Sprites[CurrentPlatform].Detach;
end;
//This procedure will draw any collected jewels in the top
//right hand corner of the screens. They are drawn half
//size uses the StretchDraw method.
procedure TForm1.DrawCollectedJewels;
var
x, n: Integer;
begin
x := 640 - 13;
for n := 1 to JewelCount do
begin
DGCScreen1.Back.StretchDraw(Rect(x, 2, x + 13, 12),
DGCScreen1.Images[10], True);
Dec(x, 15);
end;
end;
//This procedure is called when all 6 jewels have been collected.
//The page flipping is stopped until a key is pressed.
//One more flip will still occur so we must draw out messages
//on to the back surface.
procedure TForm1.LevelCompleted;
const
msg1 = 'Well done you completed the demo';
msg2 = 'Press any key to play again';
begin
DGCSCreen1.FlippingEnabled := False;
DGCAudio1.Sound[2].Replay; //Play Pacman Jingle
with DGCScreen1.Back.Canvas do
begin
Font.Size := 18;
Brush.Style := bsClear;
//msg1
Font.Color := clRed;
TextOut(131, 201, msg1);
Font.Color := clYellow;
TextOut(130, 200, msg1);
//msg2
Font.Color := clRed;
TextOut(171, 226, msg2);
Font.Color := clYellow;
TextOut(170, 225, msg2);
Release;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -