Shoot

by Nathan Horsley: Y12 Age ~17

Introduction

We include program Shoot because it is very different from our other contributions. It is simple, with little code, but Nathan has made good use of the timers, images and labels to create a game that is difficult to resist.

Screenshot from a completed game

Screenshot from a completed game

You can download here a zip file containing the code of uShoot.pas, the form file and the three images. However, we encourage you to try to develop something similar yourself instead.

The Code of uShoot.pas

unit uShoot;
{
    Copyright (c) 2013 Nathan Horsley
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls;

type
  TfrmShoot = class(TForm)
    Image1, Image2, Image3 : TImage;
    lblScoreCaption, lblScore, lblHeadshotRed, lblHeadshotGrey,  lblTimeCaption, lblTimeRemaining : TLabel;
    tmrMovement, tmrCountdown : TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure tmrMovementTimer(Sender: TObject);
    procedure tmrCountdownTimer(Sender: TObject);
  end;
var
  frmShoot: TfrmShoot;

implementation

{$R *.lfm}

var
  Time, Score : integer;

procedure TfrmShoot.tmrMovementTimer(Sender : TObject);
begin
  Image1.Top := Random(frmShoot.Height - Image3.Top);
  Image2.Top := Image1.Top - 23;
  Image1.Left := Random(frmShoot.Width);
  Image2.Left := Image1.Left + 24;
  lblScore.Caption := Inttostr(Score);
  lblHeadshotRed.Visible := False;
end;

procedure TfrmShoot.tmrCountdownTimer(Sender: TObject);
begin
  Time := Time - 1;
  lblTimeRemaining.Caption := InttoStr(Time);
  if Time = 0 then
    begin
      tmrMovement.Enabled := False;
      tmrCountdown.Enabled := False;
      ShowMessage('Your score was: ' + Inttostr(Score));
      Close;
    end;
end;

procedure TfrmShoot.FormCreate(Sender: TObject);
begin
  Score := 0;
  Time := 10;
  lblScore.caption := InttoStr(Score);
  lblHeadshotRed.Visible := False;
end;

procedure TfrmShoot.Image1Click(Sender: TObject);
begin
  Score := Score + 5;
end;

procedure TfrmShoot.Image2Click(Sender: TObject);
begin
  Score := Score + 60;
  lblHeadshotRed.Visible := True;
end;

end.

Programming - a skill for life!

Student programs to inspire you!