PDA

View Full Version : Adding BonusLives...

Wizard
01-10-2007, 12:04 PM
O/s: WinXP
Lib: DelphiX

Hi everyone. I'm feeling realy dumb as I'm sure that this is a question of logic, but I can't seem to figure it out :evil:

If points scored are 25000, 50000 etc. then the remaining lives must be incremented by 2 and this does not happen. If points reaches 25000 it works but not when points reaches 50000. Any thoughts?

In the Player collission:
Lives := Lives - 1;
LivesLeft := LivesLeft -1;

procedure TFormGame.BonusLife;
begin
if (Points = 25000) then
begin
Lives := LivesLeft + 2;
end
else if (Points = 50000) then
begin
Lives := LivesLeft + 2;
end

RenderPlay: dxdrawGame.Surface.Canvas.TextOut(0,715, 'Lives left: '+ IntToStr(Lives));

Starting state:
Lives := 3;
LivesLeft := 3;

pstudio
01-10-2007, 12:16 PM
What's the difference between lives and livesleft?

Assuming Points is an Integer I would rewrite TFormGame.BonusLife to

procedure TFormGame.BonusLife;
begin
if Points mod 25000 = 0 then
Lives := LivesLeft+2;
end;

Brainer
01-10-2007, 12:18 PM
I can't really understand the difference between Lives and LivesLeft. In my humble opinion, it should be like this:

procedure TFormGame.BonusLife;
begin
case Points of
25000: Inc(Lives, 2); // or Lives := Lives + 2;
50000: Inc(Lives, 2); // or Lives := Lives + 2;
end;
end;

This procedure should be constantly performed (i.e. in OnTimer event).

And a side note: change the name Lives on Lifes. ;)

Hope that helped a bit. :)

Wizard
01-10-2007, 12:49 PM
Thanks for the replies guys :-)

PStudio, with your solution the Lifes goes to 5 and stays that way ...?

Brainer, with your solution the lifes climbs to 168 when points reaches 25000....?

Points and Lifes are integers.

pstudio
01-10-2007, 12:58 PM
When do you call BonusLife (can we see some code)?

Btw. my code only works if you want the life variable to increase every 25000 points. If you want some more odd rules for when to increase lifes you can use brainers example.

wodzu
01-10-2007, 01:00 PM
Thanks for the replies guys :-)

PStudio, with your solution the Lifes goes to 5 and stays that way ...?

Becasue LivesLeft is not incremented.

It should be like this:

procedure TFormGame.BonusLife;
begin
if Points mod 25000 = 0 then
begin
Inc&#40;LivesLeft,2&#41;;
Lives &#58;= LivesLeft;
end;
end;

Wizard
01-10-2007, 01:10 PM
Wodzu, with your solution lives runs to 643?

procedure TFormGame.PlayingStateMachine;
var i,j : integer;
begin
case fPlayingState of
_psStarting : begin
for I:= DXSpriteEngine.Engine.AllCount-1 downto 0 do
begin
if (DXSpriteEngine.Engine.Items[i] <> nil) then
begin
end;
end;
BackGroundCreate;
Player := TPlayer.create(dxspriteEngine.Engine,dximageList,P layerImageIndex);
Attacker := TAttacker.create(dxspriteEngine.Engine,dximageList ,AttackerImageIndex);
SpaceShipDown := TSpaceShipDown.create(dxspriteEngine.Engine,dximag eList,SpaceShipDownImageIndex);
GreenShip := TGreenShip.create(dxspriteEngine.Engine,dximageLis t,GreenShipImageIndex);
Enemies := 15;
for j := 1 to Enemies do
begin
Enemy := TEnemy.create(dxspriteEngine.Engine,dximageList,En emyLeftImageIndex);
end;
disk := TDisk.Create(dxSpriteEngine.Engine);
Points := 0;
EnemyCount := 0;
SpaceMines := 0;
Attackers := 0;
GreenShips := 0;
Lives := 3;
LivesLeft := 3;
fPlayingState:=_psPlaying;
end;
_psPlaying : begin
FormGame.DxdrawGame.Surface.BltFast(0, 0, backscreen.clientrect,1, backscreen);
DxSpriteEngine.Engine.Move(1);
formGame.BonusLife;
if (player.Deaded) and (Lives <0>= 100000) then
begin
fGameState := _gsEnd;
fEndState:=_esPlaySelected;
formGame.DXWaveList.Items.Find('BlownUp').Play(Fal se);
for I:= DXSpriteEngine.Engine.AllCount-1 downto 0 do
begin
if (DXSpriteEngine.Engine.Items[i] <> nil) then
begin
end;
end;
formHigh.ShowModal;
end
else
if (Player.Deaded) and (Lives <= 0) then
begin
fGameState:=_gsEnd;
fEndState:=_esPlaySelected;
formGame.DXWaveList.Items.Find('BlownUp').Play(Fal se);
for I:= DXSpriteEngine.Engine.AllCount-1 downto 0 do
begin
if (DXSpriteEngine.Engine.Items[i] <nil> 0) and (Player.Deaded) then
begin
Player := TPlayer.create(dxspriteEngine.Engine,dximageList,P layerImageIndex);
formGame.dxWaveList.Items.Find('Bonus').Play(false );
end;
end;
end
end;

Huehnerschaender
01-10-2007, 01:12 PM
The problem seems to be that you all try to spent some extra lives when the points reach the EXACT value of 25000 points. But what happens when the player has 24999 Points, shoots an enemy and gains 10 points for this enemy? So, you need a boolean variable that tells you if you already gave bonus lifes for reaching 25000 points.

if &#40;Points >= 25000&#41; and not XtraLife25k then
begin
inc&#40;Lifes,2&#41;;
XtraLife25k &#58;= true;
end;
This way you will give 2 extra lifes ONCE and in EVERY CASE when the player reaches 25000 or more points.

Same should be done with 50000 then....

EDIT: You should of course use an array of bool when attaching this behaviour to every 25000 points.

pstudio
01-10-2007, 01:25 PM
Thanks for the replies guys :-)

PStudio, with your solution the Lifes goes to 5 and stays that way ...?

Becasue LivesLeft is not incremented.

It should be like this:

procedure TFormGame.BonusLife;
begin
if Points mod 25000 = 0 then
begin
Inc&#40;LivesLeft,2&#41;;
Lives &#58;= LivesLeft;
end;
end;

You're right. I just missed that part in my confussion as to why those to vars exists.

@Huehnerschaender
Actually he doesn't mention that 25001 is life giving score, but else you're right. Unless of course he's incrementing the score with a value that guarentees him to end on 25000.

@Wizard
As Dirk points out, you should make sure that the game doesn't increase your life vars. If I were you I would only call BonusLife when the score changes instead of doing it all the time.

Wizard
01-10-2007, 01:27 PM
Huehnerschaender

Thanks so much!!! It's working :-)

procedure TFormGame.BonusLife;
begin
if &#40;Points >= 25000&#41; and not XtraLife25000 then
begin
inc&#40;LiVes,2&#41;;
XtraLife25000 &#58;= TRUE;
end;

if &#40;Points >= 50000&#41; and not XtraLife50000 THEN
begin
inc&#40;LiVes,2&#41;;
XtraLife50000 &#58;= TRUE;
end;

I'll try to figure out how to do the array of bool.

VilleK
01-10-2007, 02:55 PM
Maybe something like this (not tested):

var
LastBonusScore &#58; integer; //Set to zero when starting a new game

procedure TFormGame.BonusLife;
begin
if &#40;Points >= LastBonusScore+25000&#41; then
begin
Inc&#40;Lives,2&#41;;
Inc&#40;LastBonusScore,25000&#41;;
end;
end;

It should give you a bonus every 25000 points.

Huehnerschaender
01-10-2007, 06:41 PM
Yep, this should work, too :) And you don't need the boolean variable... this is the better solution.

jasonf
01-10-2007, 08:23 PM
That solution is something like how I did my extra lives. (Edit: on re-reading this, it's pretty much exactly what Villek wrote :) )

I have a variables:

fScore &#58; integer;
fLives &#58; integer;
fNextLifeAt &#58; integer;

At the start of a new game
I initialise them

fScore &#58;= 0;
fLives &#58;= 3;
fNextLifeAt &#58;= 25000;

then in my game loop (or timer event),
I'd check

If fScore >= fNextLifeAt then
begin
inc&#40;fLives&#41;;
inc&#40;fNextLifeAt, 25000&#41;;
end;

Of course this isn't exactly how I do it, My extra lives are spawned and need collecting before they take effect and my system is Object Oriented, but the theory is the same.

Wizard
02-10-2007, 06:27 AM
You guys realy know your stuff :D

Thanks to everyone. VilleK, your solution works and seems to be the best :idea:

User137
02-10-2007, 06:57 AM
edit: *oops, delete this*