1. 程式人生 > 其它 >Delphi 經典遊戲程式設計40例 的學習 1

Delphi 經典遊戲程式設計40例 的學習 1

2021年11月20日

unit Unit1;

interface

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

type
  TRei40_01 = class(TForm)
    img1: TImage;
    img2: TImage;
    tmr1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rei40_01: TRei40_01;
  Xadd : ShortInt =1;
  Yadd : ShortInt =1;

implementation

{$R *.dfm}

procedure TRei40_01.FormCreate(Sender: TObject);
begin
  Rei40_01.Width :=640;
  Rei40_01.Height :=480;

  img1.Height := 432;
  img1.Width := 592;

  img1.Left := (ClientWidth-img1.Width) div 2;
  img1.Top := (ClientHeight- img1.height) div 2;

  img2.Height :=32;
  img2.Width :=32;

  img2.Left :=320;
  img2.Top :=240 ;

end;

procedure TRei40_01.tmr1Timer(Sender: TObject);
begin
  if img2.Left <= img1.Left then
  Xadd :=1
  else if img2.Left + img2.Width >= img1.Left + img1.Width
  then  Xadd :=-1;

  if img2.Top <= img1.Top then
  Yadd :=1
  else if img2.Top + img2.Height >=  img1.Top + img1.Height
  then Yadd :=-1;

  img2.Left := img2.Left + Xadd;
  img2.Top :=img2.Top + Yadd;
end;

end.

  一個簡單的程式,

學習到的 IMAGE 元件 屬性的簡單設定 ,載入 圖片,圖示。

圖片居中放置 的演算法。