mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-21 05:19:10 +02:00
Examples/sprites: Add smooth movement of sprite by keyboard, as well as interia effect for keyboard movement (code by Handoko, https://forum.lazarus.freepascal.org/index.php/topic,41178.msg285678.html#msg285678).
git-svn-id: trunk@57899 -
This commit is contained in:
parent
9997fa404c
commit
7b4fa8c326
@ -66,8 +66,10 @@ object PlayGroundForm: TPlayGroundForm
|
|||||||
ItemIndex = 0
|
ItemIndex = 0
|
||||||
Items.Strings = (
|
Items.Strings = (
|
||||||
'code'
|
'code'
|
||||||
'arrow keys'
|
|
||||||
'mouse'
|
'mouse'
|
||||||
|
'arrow keys'
|
||||||
|
'arrow keys (smooth)'
|
||||||
|
'arrow keys with inertia'
|
||||||
)
|
)
|
||||||
OnChange = ComboBox1Change
|
OnChange = ComboBox1Change
|
||||||
Style = csDropDownList
|
Style = csDropDownList
|
||||||
@ -77,7 +79,7 @@ object PlayGroundForm: TPlayGroundForm
|
|||||||
end
|
end
|
||||||
object Timer1: TTimer
|
object Timer1: TTimer
|
||||||
Enabled = False
|
Enabled = False
|
||||||
Interval = 100
|
Interval = 40
|
||||||
OnTimer = Timer1Timer
|
OnTimer = Timer1Timer
|
||||||
left = 164
|
left = 164
|
||||||
top = 49
|
top = 49
|
||||||
|
@ -6,7 +6,7 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
|
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
|
||||||
LMessages, LCLType, ExtCtrls, StdCtrls;
|
LMessages, LCLType, LCLIntf, ExtCtrls, StdCtrls;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
@ -48,6 +48,8 @@ type
|
|||||||
procedure PictureControlKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
|
procedure PictureControlKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
|
||||||
procedure Timer1Timer(Sender: TObject);
|
procedure Timer1Timer(Sender: TObject);
|
||||||
private
|
private
|
||||||
|
FXIntertia: Double;
|
||||||
|
FYIntertia: Double;
|
||||||
FSpritePos: TPoint;
|
FSpritePos: TPoint;
|
||||||
FSpritePosChange: TPoint;
|
FSpritePosChange: TPoint;
|
||||||
FSpritePosInit: Boolean;
|
FSpritePosInit: Boolean;
|
||||||
@ -89,6 +91,9 @@ begin
|
|||||||
BufferImg.Width:=BackgroundImg.Width;
|
BufferImg.Width:=BackgroundImg.Width;
|
||||||
BufferImg.Height:=BackgroundImg.Height;
|
BufferImg.Height:=BackgroundImg.Height;
|
||||||
|
|
||||||
|
FXIntertia := 0;
|
||||||
|
FYIntertia := 0;
|
||||||
|
|
||||||
Timer1.Enabled := Combobox1.ItemIndex = 0;
|
Timer1.Enabled := Combobox1.ItemIndex = 0;
|
||||||
|
|
||||||
UpdateImage;
|
UpdateImage;
|
||||||
@ -115,7 +120,7 @@ end;
|
|||||||
|
|
||||||
procedure TPlayGroundForm.ComboBox1Change(Sender: TObject);
|
procedure TPlayGroundForm.ComboBox1Change(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
Timer1.Enabled := Combobox1.ItemIndex in [0, 2];
|
Timer1.Enabled := Combobox1.ItemIndex in [0, 1, 3, 4];
|
||||||
PictureControl.SetFocus;
|
PictureControl.SetFocus;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -124,6 +129,7 @@ procedure TPlayGroundForm.PictureControlKeyDown(Sender: TObject;
|
|||||||
const
|
const
|
||||||
DELTA = 20;
|
DELTA = 20;
|
||||||
begin
|
begin
|
||||||
|
if ComboBox1.ItemIndex = 3 then Exit; // for smooth arrow key input
|
||||||
case Key of
|
case Key of
|
||||||
VK_LEFT : FSpritePosChange := Point(-DELTA, 0);
|
VK_LEFT : FSpritePosChange := Point(-DELTA, 0);
|
||||||
VK_RIGHT : FSpritePosChange := Point( DELTA, 0);
|
VK_RIGHT : FSpritePosChange := Point( DELTA, 0);
|
||||||
@ -197,12 +203,6 @@ begin
|
|||||||
FSpritePos.Y := CenterY + round(sin(t*0.7)*CenterY*2/3) - SpriteImg.Height div 2;
|
FSpritePos.Y := CenterY + round(sin(t*0.7)*CenterY*2/3) - SpriteImg.Height div 2;
|
||||||
end;
|
end;
|
||||||
1: begin
|
1: begin
|
||||||
// Movement of sprite by keyboard: UP/DOWN/LEFT/RIGHT arrows advance
|
|
||||||
// the sprite position by a given amount
|
|
||||||
FSpritePos.X := FSpritePos.X + FSpritePosChange.X;
|
|
||||||
FSpritePos.Y := FSpritePos.Y + FSpritePosChange.Y;
|
|
||||||
end;
|
|
||||||
2: begin
|
|
||||||
// Movement of sprite by mouse: the sprite follows the mouse
|
// Movement of sprite by mouse: the sprite follows the mouse
|
||||||
// Convert screen coordinates to images coordinates
|
// Convert screen coordinates to images coordinates
|
||||||
MousePos := PictureControl.ScreenToClient(Mouse.CursorPos);
|
MousePos := PictureControl.ScreenToClient(Mouse.CursorPos);
|
||||||
@ -213,6 +213,36 @@ begin
|
|||||||
FSpritePos.X := FSpritePos.X + dx;
|
FSpritePos.X := FSpritePos.X + dx;
|
||||||
FSpritePos.Y := FSpritePos.Y + dy;
|
FSpritePos.Y := FSpritePos.Y + dy;
|
||||||
end;
|
end;
|
||||||
|
2: begin
|
||||||
|
// Movement of sprite by keyboard: UP/DOWN/LEFT/RIGHT arrows advance
|
||||||
|
// the sprite position by a given amount
|
||||||
|
FSpritePos.X := FSpritePos.X + FSpritePosChange.X;
|
||||||
|
FSpritePos.Y := FSpritePos.Y + FSpritePosChange.Y;
|
||||||
|
end;
|
||||||
|
3: begin
|
||||||
|
// Movement of sprite by keyboard: UP/DOWN/LEFT/RIGHT arrows advance smooth version
|
||||||
|
if (GetKeyState(VK_LEFT) < 0) then FSpritePos.X := FSpritePos.X - 10;
|
||||||
|
if (GetKeyState(VK_RIGHT) < 0) then FSpritePos.X := FSpritePos.X + 10;
|
||||||
|
if (GetKeyState(VK_UP) < 0) then FSpritePos.Y := FSpritePos.Y - 10;
|
||||||
|
if (GetKeyState(VK_DOWN) < 0) then FSpritePos.Y := FSpritePos.Y + 10;
|
||||||
|
end;
|
||||||
|
4: begin
|
||||||
|
// Movement of sprite by keyboard: UP/DOWN/LEFT/RIGHT arrows advance with inertia
|
||||||
|
if (GetKeyState(VK_LEFT) < 0) then FXIntertia := FXIntertia - 0.5;
|
||||||
|
if (GetKeyState(VK_RIGHT) < 0) then FXIntertia := FXIntertia + 0.5;
|
||||||
|
if (GetKeyState(VK_UP) < 0) then FYIntertia := FYIntertia - 0.5;
|
||||||
|
if (GetKeyState(VK_DOWN) < 0) then FYIntertia := FYIntertia + 0.5;
|
||||||
|
if (FXIntertia > 6) then FXIntertia := 6;
|
||||||
|
if (FXIntertia < -6) then FXIntertia := -6;
|
||||||
|
if (FYIntertia > 6) then FYIntertia := 6;
|
||||||
|
if (FYIntertia < -6) then FYIntertia := -6;
|
||||||
|
if (FXIntertia > 0) then FXIntertia := FXIntertia - 0.2;
|
||||||
|
if (FXIntertia < 0) then FXIntertia := FXIntertia + 0.2;
|
||||||
|
if (FYIntertia > 0) then FYIntertia := FYIntertia - 0.2;
|
||||||
|
if (FYIntertia < 0) then FYIntertia := FYIntertia + 0.2;
|
||||||
|
FSpritePos.X := FSpritePos.X + round(FXIntertia);
|
||||||
|
FSpritePos.Y := FSpritePos.Y + round(FYIntertia);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Make sure that the sprite does not leave the image.
|
// Make sure that the sprite does not leave the image.
|
||||||
|
Loading…
Reference in New Issue
Block a user