|
Game of 'Hitting Balloons' in Delphi
Posted Date:
Total Responses: 0
Posted By: Himadri H. Patel Member Level: Gold Points/Cash: 7
|
unit untHitBalloon;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Math, ComCtrls;
type TBalloonList = Record balloonChar: Char; position: TPoint; flag: Boolean; gotPoint: Boolean; End;
type TfrmHitBalloon = class(TForm) Panel1: TPanel; imgMain: TImage; btnstart: TButton; txtkeyhit: TEdit; StatusBar1: TStatusBar; lblScore: TLabel; lblskipped: TLabel; lbltotal: TLabel; procedure FormCreate(Sender: TObject); procedure StartGame; procedure DrawCircle(xc, yc, r: Single); procedure clearImage; procedure btnstartClick(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); function DrawBalloons():Boolean; procedure FormKeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end;
var frmHitBalloon: TfrmHitBalloon; hgt,wdth,balloonCount,Score,Skipped: Integer; Balloons: Array of TBalloonList; Radious: Single; isCont: Boolean;
implementation
{$R *.dfm}
procedure TfrmHitBalloon.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin isCont := False; end;
procedure TfrmHitBalloon.FormCreate(Sender: TObject); begin ReportMemoryLeaksOnShutdown := True; hgt := 400; wdth := 400; Radious := 15; balloonCount := 20; Score := 0; Skipped := 0; imgMain.Picture.Bitmap.Canvas.Font.Size := 15; imgMain.Height := hgt; imgMain.Width := wdth; imgMain.Picture.Bitmap.Height := hgt; imgMain.Picture.Bitmap.Width := wdth; end;
procedure TfrmHitBalloon.FormKeyPress(Sender: TObject; var Key: Char); var cnt: Integer; isKey: Boolean; begin isKey := False; for cnt := 0 to balloonCount - 1 do begin if Balloons[cnt].flag then begin if Balloons[cnt].balloonChar = key then begin isKey := True; Balloons[cnt].flag := False; Balloons[cnt].gotPoint := True; Balloons[cnt].position.Y := hgt; inc(Score,1); lblScore.Caption := 'Hitted : ' + inttostr(Score); Balloons[cnt + 1].flag := True; break; end; end; end; if not isKey then begin inc(Skipped,1); end; lblskipped.Caption := 'Skipped : ' + inttostr(Skipped); lbltotal.Caption := 'Score : ' + inttostr(Score * 2 - Skipped); end;
procedure TfrmHitBalloon.StartGame; var cnt,num,r: Integer; begin Randomize; SetLength(Balloons,balloonCount); //Fill the values of balloons... for cnt := 0 to balloonCount - 1 do begin num := Random(51); //65 to 90 A to Z //97 to 122 a to z if num < 26 then begin num := 65 + num; end else begin num := 97 + num mod 26; end; balloons[cnt].balloonChar := chr(num); Balloons[cnt].flag := False; Balloons[cnt].gotPoint := False; r := Round(Radious); Balloons[cnt].position.X := Random(wdth - 2 * r) + r; Balloons[cnt].position.Y := 0; Score := 0; Skipped := 0; end; isCont := True; Balloons[0].flag := True; while isCont do begin clearImage; isCont := DrawBalloons; imgMain.Repaint; Sleep(50); Application.ProcessMessages; end; ShowMessage('Your Score : ' + IntToStr(Score * 2 - Skipped)); end;
function TfrmHitBalloon.DrawBalloons():Boolean; var cnt: Integer; begin Result := False; for cnt := 0 to balloonCount - 1 do begin if Balloons[cnt].flag then begin inc(Balloons[cnt].position.Y,2); if Balloons[cnt].position.Y + Round(Radious) > hgt then begin inc(Skipped,1); lblskipped.Caption := 'Skipped : ' + inttostr(Skipped); lbltotal.Caption := 'Score : ' + inttostr(Score * 2 - Skipped); Balloons[cnt].flag := False; end else begin DrawCircle(Balloons[cnt].position.X,Balloons[cnt].position.Y,Radious); with imgMain.Picture.Bitmap.Canvas do begin TextOut(Balloons[cnt].position.X - TextWidth(balloons[cnt].balloonChar) div 2,Balloons[cnt].position.Y - TextHeight(balloons[cnt].balloonChar) div 2,balloons[cnt].balloonChar); end; end; Result := True; if (cnt <> balloonCount - 1) then begin if (Balloons[cnt].position.Y > hgt div 4) and not Balloons[cnt + 1].gotPoint then begin Balloons[cnt + 1].flag := True; end; end; end; end; end;
procedure TfrmHitBalloon.DrawCircle(xc, yc, r: Single); var cnt: Integer; x,y: Single; begin x := xc + r; y := yc; imgMain.Picture.Bitmap.Canvas.MoveTo(Round(x),Round(y)); for cnt := 0 to 360 do begin imgMain.Picture.Bitmap.Canvas.LineTo(Round(xc + r * cos(DegToRad(cnt))),Round(yc - r * sin(DegToRad(cnt)))); end; end;
procedure TfrmHitBalloon.btnstartClick(Sender: TObject); begin txtkeyhit.SetFocus; StartGame; end;
procedure TfrmHitBalloon.clearImage; begin imgMain.Picture.Bitmap.Canvas.Brush.Color := clWhite; imgMain.Picture.Bitmap.Canvas.Pen.Color := clWhite; imgMain.Picture.Bitmap.Canvas.Rectangle(imgMain.Picture.Bitmap.Canvas.ClipRect); imgMain.Picture.Bitmap.Canvas.Pen.Color := clBlack; end;
end.
|
Project Feedbacks
|
| Author: Vinay Arya | Member Level: Silver | Revenue Score:  | thnxxx.....
|
|
|
| Post Feedback |
|
|
You must Sign In to post a feedback.
|
|
|
|
|
|