Results 1 to 10 of 31

Thread: Dynamic in-game object creation and manipulation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    No, not so weird, SilverWarior, after all you are right: if I would like help from an English-speaking programmer community, the best is when I write my codes totally in English in order to better understand.
    Therefore today I have made a small program, which is focusing the "bullet appearing" problem:
    Code:
    unit Unit1;
    
    
    {$mode objfpc}{$H+}
    
    
    interface
    
    
    uses
      Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
    
    
    type
    
    
      { TForm1 }
    
    
      TForm1 = class(TForm)
        Timer1: TTimer;
        procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { private declarations }
      public
        { public declarations }
      end;
    
    
    type TGameObject=class(TObject)
      private
      public
        xplace,yplace,celx,cely,movedir: integer;
        itsimage: TBitmap;
        canshoot: boolean;
        shoottimer: byte;
        constructor create(xplacehere,yplacehere: longint; imagehere: Tbitmap);
    end;
    
    
    var
      Form1: TForm1;
      backgroundimg,bulletimg,spaceshipimg: TBitMap;
      spaceship: TGameObject;
      bullet: array [0..100] of TGameObject;
      numofbullets: integer;
    
    
    implementation
    
    
    {$R *.lfm}
    
    
    constructor TGameObject.create(xplacehere,yplacehere: integer; imagehere: Tbitmap);
    begin
            xplace:=xplacehere;
            yplace:=yplacehere;
            itsimage:=imagehere;
    end;
    
    
    { TForm1 }
    
    
    procedure TForm1.FormCreate(Sender: TObject);
    var initialmovedir: shortint;
    begin
      backgroundimg := TBitmap.Create;
      backgroundimg.LoadFromFile('kepek/urhatter.bmp');
      bulletimg:=TBitMap.Create;
      bulletimg.LoadFromFile('kepek/ell_lov.bmp');
      bulletimg.transparent:=true;
      spaceshipimg:=TBitMap.Create;
      spaceshipimg.LoadFromFile('kepek/ell1.bmp');
      spaceshipimg.transparent:=true;
      randomize;
      if round(random(2))=1 then initialmovedir:=-2 else initialmovedir:=2;
      spaceship:=TGameObject.create(Form1.ClientWidth div 2,spaceshipimg.height,spaceshipimg);
      spaceship.movedir:=initialmovedir;
      spaceship.shoottimer:=100;
      spaceship.canshoot:=false;
      numofbullets:=0;
    end;
    
    
    procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    begin
      backgroundimg.free;
      bulletimg.free;
      spaceshipimg.free;
    end;
    
    
    procedure TForm1.Timer1Timer(Sender: TObject);
    var i,j: integer;
    begin
      Form1.Canvas.StretchDraw(Rect(0, 0, ClientWidth, ClientHeight), backgroundimg);
      if spaceship.shoottimer>0 then dec(spaceship.shoottimer) else spaceship.canshoot:=true;
      if (spaceship.xplace+spaceship.movedir>=ClientWidth-spaceshipimg.width) or (spaceship.xplace+spaceship.movedir<=1) then
      begin
           if spaceship.yplace+spaceshipimg.height<ClientHeight then
              spaceship.yplace:=spaceship.yplace+spaceshipimg.height
           else
               spaceship.yplace:=spaceshipimg.height;
           spaceship.movedir:=-spaceship.movedir;
      end
      else
      begin
           spaceship.xplace:=spaceship.xplace+spaceship.movedir;
      end;
      Form1.Canvas.Draw(spaceship.xplace,spaceship.yplace,spaceshipimg);
      if (round(random(50))=1) and (spaceship.canshoot=true) then
      begin
           bullet[numofbullets]:=TGameObject.create(spaceship.xplace,spaceship.yplace,bulletimg); {!!!WRONG!!!}
           inc(numofbullets);
           spaceship.canshoot:=false;
           spaceship.shoottimer:=100;
      end;
    
    
      if numofbullets>0 then
      begin
           i:=0;
           while i<numofbullets-1 do
           begin
                if bullet[i]<>nil then
                begin
                    if bullet[i].yplace>ClientHeight then
                    begin
                        freeandnil(bullet[i]);
                        if i=numofbullets-1 then
                                dec(numofbullets)
                        else
                        begin
                                for j:=i+1 to numofbullets-1 do
                                    bullet[j-1]:=bullet[j];
                                dec(numofbullets);
                                dec(i);
                        end;
                    end
                    else
                    begin
                        bullet[i].yplace:=bullet[i].yplace+4;
                        Form1.Canvas.Draw(bullet[i].xplace,bullet[i].yplace,bulletimg);
                    end;
                end;
                inc(i);
           end;
      end;
    end;
    
    
    end.
    Maybe somebody can help me...

  2. #2
    The last loop is still bothering me, it is needlessly complicated and could make errors. Like i suggested earlier, here's how it would look:
    Code:
      for i:=numofbullets-1 downto 0 do
      begin
        bullet[i].yplace:=bullet[i].yplace+4;
        if bullet[i].yplace>ClientHeight then
        begin
          bullet[i].Free;
          dec(numofbullets);
          bullet[i]:=bullet[numofbullets];
        else begin
          Form1.Canvas.Draw(bullet[i].xplace, bullet[i].yplace, bulletimg);
        end;
      end;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •