SRL Archive - Other Scripts

Defend Yourself!


danrox2004

2007-08-22 09:03:58

My first attempt at forms and canvases! Tell me if you like it.

The aim of the game is to last as long as you can and get as many kills as you can. Kill guys by clicking on them (shooting). As you go up levels the guys get smaller and move faster!

I am still working on this and it is nowhere near complete so check back regularly!

program game;    //by Danrox2004
const
wavelength = 1; //length of background wave thing
amplitude = 30; //amplitude of background wave thing
groundHeight = 100; //Height of ground
herox = 600; //distance from left of you
heroy = 50; //distance off ground of you
gunlength = 25; //length of the gun
guySpawnRate = 5000; //time between each guy is spawned, in ms
bulletSpeed = 10; //time between each bullet move, in ms
bulletJump = 20; //pixels the bullet is moved every bulletSpeed - balance it with bullet speed
bulletSize = 2; //size of the bullet
maxBloodSize = 20; //Maximum size of blood
killsToLevel = 3; //amount of kills you need to level

type
guy = record
x, speed, height: integer;
end;
bullet = record
currentLoc: tPoint;
startLoc: tPoint;
steps: integer;
angle: extended;
end;
bloodsplatter = record
x, y: integer;
size: integer;
end;
var
game : TForm;
menu: boolean;
canvas: tCanvas;
buffer: integer;
refreshTimer, moveTimer, guyTimer, bulletTimer: tTimer;
guys: array of Guy;
level: integer;
angle: extended;
bullets: array of bullet;
Blood: array of bloodSplatter;
kills: integer;

Var
SineArray: Array[0..360] Of Extended;
CoSineArray: Array[0..360] Of Extended;

procedure LoadCoSineArray;
var
i: Integer;
begin
for i := 0 to 360 do
begin
SineArray[i] := Sin(i * Pi / 180);
CoSineArray[i] := Cos(i * Pi / 180);
end;
end;

function Sine(Degrees: Integer): Extended;
begin
Result := SineArray[Trunc(FixD(Degrees))];
end;

function CoSine(Degrees: Integer): Extended;
begin
Result := CoSineArray[trunc(FixD(Degrees))];
end;

function getHeight(x: integer): integer;
begin
result := trunc(sine(x * wavelength)*amplitude) + (game.ClientHeight - groundHeight);
end;

procedure mouseMove(sender: tObject; Shift: TShiftState; x, y: integer);
begin
angle := arcTan2((y - (getHeight(herox) - heroy)), (x - herox));
end;

procedure showMenu;
begin
end;

procedure keyPress(sender: tObject);
begin

{if(not Menu) then
showMenu
else
showGame;}
end;

procedure drawGuy(x, height: integer);
var
y: integer;
begin
y := getHeight(x);
Canvas.Pen.Color := 16777215;
Canvas.Pen.Width := 2;
Canvas.MoveTo(x - height * 5, y); //legs
Canvas.LineTo(x, y - height * 10);
Canvas.LineTo(x + height * 5, y);
Canvas.MoveTo(x, y - height * 10);
Canvas.LineTo(x, y - height * 20);
Canvas.LineTo(x - height * 5, y - height * 15);
Canvas.MoveTo(x, y - height * 20);
Canvas.LineTo(x + height * 5, y - height * 15);
Canvas.Ellipse(x - height * 5, y - height * 30, x + height * 5, y - height * 20);
Canvas.FloodFill(x, y - height * 25, 16777215, 1);
end;

procedure drawBackGround;
var
i: integer;
begin
canvas.Pen.Color := 3182887;
Canvas.Pen.Width := 1;
Canvas.Brush.Color := 3182887;
Canvas.Brush.Style := bsSolid;
canvas.MoveTo(0, getHeight(0));
for i := 1 to 700 do
begin
canvas.LineTo(i, getHeight(i));
end;
canvas.FloodFill(1, 499, 3182887, 1);
//canvas.FloodFill(1, 1, 13337706, 1);
end;

procedure drawMouse;
var
x, y: integer;
height: integer;
begin
height := getHeight(herox) - heroy;
x := Trunc(cos(angle) * gunLength);
y := Trunc(sin(angle) * gunLength);
canvas.Pen.Color := 5731566;
canvas.Pen.Width := 2;
canvas.MoveTo(herox, height);
canvas.LineTo(herox + x, Height + y);
end;

procedure addBullet(angle: extended; x, y: integer);
var
i: integer;
begin
i := length(Bullets);
setArrayLength(Bullets, i + 1);
Bullets[i].CurrentLoc := intToPoint(x, y);
Bullets[i].StartLoc := Bullets[i].CurrentLoc;
Bullets[i].angle := angle;
end;

procedure mouseClick(Sender: TObject);
var
x, y: integer;
begin
x := Trunc(cos(angle) * gunLength + herox);
y := Trunc(sin(angle) * gunLength + (getHeight(herox) - heroy));
addBullet(angle, x, y);
end;

procedure clearCanvas;
begin
canvas := getBitmapCanvas(bitmapFromString(700, 500, ''));
end;

procedure drawHero;
begin
//canvas.
end;

procedure drawAllGuys;
var
i: integer;
begin
for i := 0 to length(guys) - 1 do
begin
drawGuy(guys[i].x, guys[i].height);
end;
end;

procedure drawBullets;
var
i: integer;
begin
Canvas.Pen.Color := 12417106;
Canvas.Pen.Width := 1;
for i := 0 to length(Bullets) - 1 do
canvas.Ellipse(Bullets[i].currentLoc.x - bulletSize, Bullets[i].currentLoc.y - bulletSize, Bullets[i].currentLoc.x + bulletSize, Bullets[i].currentLoc.y + bulletSize);
end;

procedure removeBlood(Num: integer);
var
i: integer;
tempBlood: array of BloodSplatter;
begin
for i := 0 to length(Blood) - 1 do
begin
if(i <> Num) then
begin
setArrayLength(tempBlood, length(tempBlood) + 1);
tempBlood[length(tempBlood) - 1] := Blood[i];
end;
end;
Blood := tempBlood;
end;

procedure drawBlood(start: integer);
var
i: integer;
begin
canvas.Brush.Color := 5000376;
canvas.Brush.Style := bsSolid;
for i := start to length(Blood) - 1 do
begin
canvas.Ellipse(blood[i].x - blood[i].size, blood[i].y - blood[i].size, blood[i].x + blood[i].size, blood[i].y + blood[i].size);
inc(Blood[i].size);
if(Blood[i].size > maxBloodSize) then
begin
removeBlood(i);
drawBlood(i);
Exit;
end;
end;
end;

procedure addBlood(x, y: integer);
var
i: integer;
begin
i := length(Blood);
setArrayLength(Blood, i + 1);
Blood[i].x := x;
Blood[i].y := y;
Blood[i].size := 1;
end;

procedure paint(sender: tObject);
begin
clearCanvas;
drawBackGround;
drawAllGuys
drawBullets;
drawHero;
drawMouse;
drawBlood(0);
copyCanvas(canvas, game.canvas, 0, 0, 700, 500, 0, 0, 700, 500);
end;

procedure removeGuy(guyNum: integer);
var
i: integer;
tempGuys: array of guy;
begin
for i := 0 to length(guys) - 1 do
begin
if(i <> guyNum) then
begin
setArrayLength(tempGuys, length(tempGuys) + 1);
tempGuys[length(tempGuys) - 1] := guys[i];
end;
end;
guys := tempGuys;
end;

procedure moveGuy(sender: tObject);
var
i: integer;
begin
for i := 0 to length(guys) - 1 do
begin
guys[i].x := guys[i].x + guys[i].speed;
if(guys[i].x >= herox - 20 - gunlength) then
begin
guys[i].x := herox - 20 - gunlength;
Continue;
end;
end;
end;

procedure addKills;
begin
inc(kills);
if((kills mod killsToLevel) = 0) then
begin
inc(level);
end;
end;

procedure removeBullet(Num: integer);
var
i: integer;
tempBullets: array of bullet;
begin
for i := 0 to length(Bullets) - 1 do
begin
if(i <> Num) then
begin
setArrayLength(tempBullets, length(tempBullets) + 1);
tempBullets[length(tempBullets) - 1] := Bullets[i];
end;
end;
Bullets := tempBullets;
end;

function hitGuy(x, y: integer; var num, xxx, yyy: integer): Boolean;
var
yy: integer;
begin
for num := 0 to length(guys) - 1 do
begin
yy := getHeight(guys[num].x);
if(inRange(x, guys[num].x - guys[num].height * 5, guys[num].x + guys[num].height * 5) and inRange(y, yy - guys[num].height * 30, yy)) then
begin
xxx := guys[num].x;
yyy := y;
result := True;
Exit;
end;
end;
end;

procedure moveBullet2(start: integer);
var
i, j, a, b: integer;
begin
for i := start to length(Bullets) - 1 do
begin
bullets[i].currentLoc.x := bullets[i].StartLoc.x + trunc(coS(bullets[i].angle) * bulletJump * Bullets[i].steps);
bullets[i].currentLoc.y := bullets[i].StartLoc.y + trunc(Sin(bullets[i].angle) * bulletJump * Bullets[i].steps);
inc(bullets[i].steps);
if(bullets[i].currentLoc.y >= getHeight(Bullets[i].currentLoc.x)) then
begin
removeBullet(i);
MoveBullet2(i);
Exit;
end;
if(hitGuy(bullets[i].CurrentLoc.x, bullets[i].currentLoc.y, j, a, b)) then
begin
removeBullet(i);
moveBullet2(i);
removeGuy(j);
addBlood(a, b);
AddKills;
Exit;
end;
if(not (inRange(bullets[i].currentLoc.x, 0, 700) or inRange(bullets[i].currentLoc.x, 0, 500))) then
begin
removeBullet(i);
moveBullet2(i);
Exit;
end;
end;
end;

procedure moveBullet(sender: tobject);
begin
moveBullet2(0);
end;

procedure addGuy(sender: tObject);
var
i: integer;
begin
i := length(guys);
setArrayLength(guys, i + 1);
guys[i].x := -20;
guys[i].height := max(5 - level, 1);
guys[i].speed := level * 2 + random(3);
end;

procedure InitForm;
begin
game := CreateForm;
game.BorderIcons := [biMinimize, biMaximize];
game.BorderStyle := bsSingle;
game.Caption := 'Danrox2004'#39's Game';
game.ClientHeight := 500;
game.ClientWidth := 700;
game.Color := clBtnFace;
game.Font.Color := clWindowText;
game.Font.Height := -11;
game.Font.Name := 'MS Sans Serif';
game.Font.Style := [];
game.Visible := False;
game.PixelsPerInch := 96;
game.Position := poScreenCenter;
game.OnMouseMove := @mouseMove;
game.OnClick := @mouseClick;
//game.OnKeyPress := @keyPress;
//game.onClick := @clicks;
//game.onPaint := @paint;
RefreshTimer:= TTimer.Create(game);
RefreshTimer.Interval:= 100;
RefreshTimer.OnTimer:= @Paint;
MoveTimer := TTimer.Create(game);
MoveTimer.Interval := 100;
MoveTimer.OnTimer := @MoveGuy;
GuyTimer := TTimer.Create(game);
GuyTimer.Interval := guySpawnRate;
GuyTimer.OnTimer := @addGuy;
BulletTimer := TTimer.Create(game);
BulletTimer.Interval := BulletSpeed;
BulletTimer.OnTimer := @moveBullet;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
game.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;


procedure setupVars;
begin
loadCoSineArray;
menu := True;
buffer := bitmapFromString(700, 500, '');
canvas := getBitmapCanvas(buffer);
level := 1;
end;

procedure freeAllTimers;
begin
refreshTimer.Free;
moveTimer.Free;
guyTimer.Free;
BulletTimer.Free;
end;

procedure safeFreeAllTimers;
var
v: tVariantArray;
begin
setArrayLength(v, 0);
threadsafeCall('freeAllTimers', v);
end;

begin
setupVars;
try
SafeInitForm;
SafeShowFormModal;
finally
safeFreeAllTimers;
freeForm(game);
except
writeln('Error loading form!');
end;
end.

Dan Cardin

2007-08-22 11:00:27

nice :D, next u gotta add a way to win or lose or something

yanix

2007-08-28 14:31:25

Wtf? no close thingy…

Rune Hacker

2007-08-28 16:26:09

Pretty cool but it should be closable and there should be a way to win/lose and a point system. Where did you learn about the trunc, cos, sine, and all of that mathematical stuff btw because games like this use that but i never understood what they meant and how they were used?

danrox2004

2007-08-29 06:05:59

From: Rune Hacker
Pretty cool but it should be closable and there should be a way to win/lose and a point system. Where did you learn about the trunc, cos, sine, and all of that mathematical stuff btw because games like this use that but i never understood what they meant and how they were used?


I just found it out from the internet… Its used for trigonometry for finding angles and stuff based on ratios.

Oh and its really easy to add that but im getting bored of this… I was thinking of rewriting it but couldn't be bothered… Im gonna start working on another game though =) It will be much better, im thinking of either doing a worms 2d or a birds eye view game where you walk around.. like pokemon lol.

Edit: Oh and by the way - trunc truncates the decimals off a float - rounding it down.