PDA

View Full Version : Player Shields how to do in delphix



Wizard
20-12-2006, 08:38 AM
Hi everyone, can anyone help me. I want my player to have shields i.e. only after 10 bombs has hit my player the player should die and a new player should be created. At the moment my player has three lives and when a bomb hits it the lives are decreased untill zero.

I played around with the shields in my code and also tried it in the TBomb.DoCollision procedure but it has no effect.

Thanks for a GREAT site :-)



procedure TForm1.PlayerShield;
begin
if form1.DXImageList1.Items.Find('fireball') = form1.DXImageList1.Items.Find('player') then
Player_Shields := form1.Player_Shields - 10;
if (form1.player_Shields = 0) then
player.Dead;
end;


procedure TForm1.StartMain;
begin
Splash;
Randomize;
EnimyCreate;
AttackerCreate;
PlayerCreate;
Player_Shields := 100;
PlayerShield;
BackGroundCreate;
Earthy := DXDraw1.Height - 500;
Earthx := DXDraw1.Width - 350;
Planetx := DXDraw1.Width - 700;
Planety := DXDraw1.Height -600;
PlanetStrangeX := DXDraw1.Width - 1000;
PlanetStrangeY :=DXDraw1.Height - 475;
Disk_Start_No := 1;
Disk_End_No := 1;
NewDisk;
SpaceShipDown_Start_No := 1;
SpaceShipDown_End_No := 1;
NewSpaceShipDown;
Lives := 3;
Points := 0;
SpaceMines := 0;
SpaceMines := 0;
end;


procedure TBomb.DoCollision(Sprite: TSprite; var Done: Boolean);
begin
If (Sprite is TPlayer) Then
begin
Sprite.Collisioned := False;
Sprite.Dead;
Dead;
Done := False;
end;
end;

grudzio
20-12-2006, 09:21 AM
How about something like this:



const
BOMB_DAMAGE = -10;

procedure TBomb.DoCollision((Sprite: TSprite; var Done: Boolean);
begin
if sprite is TPlayer then begin
(sprite as TPlayer).UpdateShields(BOMB_DAMAGE);
//other collision stuff
end;
end;

procedure TPlayer.UpdateShields)amount : integer);
begin
Inc(self.FShields,amount);
if self.FShields < 0 then
self.Dead;
end;

Wizard
20-12-2006, 09:58 AM
Thanks for the reply. Tried it but still no effect, maybe I'm doing something wrong (still learning). Please help me some more :-)

1) Ok, so I added a global constant before implementation:
const
BOMB_DAMAGE = -10;

2)My Player class now looks like this:

//This is the player class
TPlayer = class&#40;TImageSprite&#41;
private
FShields &#58; Integer;
FCounter&#58; Integer;
FMode&#58; Integer;
FTamaCount&#58; Integer;
FOldTamaTime&#58; Integer;
public
procedure UpdateShields&#40;amount &#58; integer&#41;;
procedure DoMove&#40;MoveCount&#58; Integer&#41;; override;
end;

3)And the 2 procedures looks as follows:


procedure TBomb.DoCollision&#40;Sprite&#58; TSprite; var Done&#58; Boolean&#41;;
begin
If &#40;Sprite is TPlayer&#41; Then
begin
&#40;Sprite as TPlayer&#41;.UpdateShields&#40;Bomb_Damage&#41;;
Sprite.Collisioned &#58;= False;
Sprite.Dead;
Dead;
Done &#58;= False;
end;
end;

procedure TPlayer.UpdateShields&#40;amount &#58; integer&#41;;
begin
Inc&#40;self.fShields,amount&#41;;
if self.FShields < 0 then
self.Dead;
end;

grudzio
20-12-2006, 10:44 AM
Remove the line


Sprite.Dead;

from the TBomb.DoCollision procedure.

Wizard
20-12-2006, 11:29 AM
ok, I removed the Sprite.Dead line in the doCollision procedure but still no effect.

My PlayerCreate and NewPlayer code:


procedure TForm1.PlayerCreate;
begin
Player &#58;= TPlayer.Create&#40;DXSpriteEngine1.Engine&#41;;
Player.Image &#58;= DXImageList1.Items.Find&#40;'Player'&#41;;
Player.X &#58;= 500;
Player.Y &#58;= 690;
Player.Width &#58;= Player.Image.Width;
Player.Height &#58;= Player.Image.Height;
Player.PixelCheck &#58;= False;
end;

function TForm1.NewPlayer;
begin
Player_No &#58;= Player_No + 1;
PlayerCreate;
Result &#58;= True;
Lives&#58;= Lives -1;
end;

The following is called in the DXTimer event:


if &#40;Player.Deaded&#41; then
NewPlayer
else
GameEnd;
GameWin;

Should FShields and amount have values assigned to them?

jasonf
20-12-2006, 11:47 AM
I think things are getting confusing because you're using -10 and counting up. But the test in UpdateShields is checking if shields < 0 when it should be checking > 0

I feel that this is the wrong way around. Things which are being damaged should lose value. Good things should be positive, bad things should be negative.

Initialise your player to have 10 shields and count down when hit.
Then test if your shields < 0, if so, make the player die.

Then later on, if your player collects a shield powerup, you can add shields to it instead of removing shields... which makes no sense to me.

Wizard
20-12-2006, 12:42 PM
I tried your suggestion to count down (dec) and still it's not working. My palyer has 3 lives and before a live expires I want it to take 10 bombs. I'll do some further testing...any further suggestions? :-)



const
BOMB_DAMAGE = 10;

function TForm1.NewPlayer;
const Bomb_Damage = 10; // does nothing?
begin
Player_No &#58;= Player_No + 1;
PlayerCreate;
Result &#58;= True;
Lives&#58;= Lives -1;
end;

procedure TBomb.DoCollision&#40;Sprite&#58; TSprite; var Done&#58; Boolean&#41;;
begin
If &#40;Sprite is TPlayer&#41; Then
begin
&#40;Sprite as TPlayer&#41;.UpdateShields&#40;Bomb_Damage&#41;;
Sprite.Collisioned &#58;= False;
//Sprite.Dead;
Dead;
Done &#58;= False;
end;
end;

procedure TPlayer.UpdateShields&#40;amount &#58; integer&#41;;
begin
dec&#40;self.fShields,amount&#41;; // was inc
if self.FShields < 0 then
self.Dead;
end;

grudzio
20-12-2006, 12:50 PM
I think things are getting confusing because you're using -10 and counting up. But the test in UpdateShields is checking if shields <0> 0

I feel that this is the wrong way around. Things which are being damaged should lose value. Good things should be positive, bad things should be negative.



It is done like you say. The Inc procedure in TPlayer.UpdateShields increases or decreases player's shields depending on the sign of the passed value. That is why the BOMB_DAMAGE is set to negative value.



Should FShields and amount have values assigned to them?

Each time you initialize player (TForm1.PlayerCreate procedure) you must set FShields to some initial value. The class constructor sets all integer fields to zero by default and that is why first bomb destroys the player.



The following is called in the DXTimer event:


if &#40;Player.Deaded&#41; then
NewPlayer
else
GameEnd;



I think this is not right but I might be wrong since I dont know the GameEnd procedure. I understand this if statement like this.
If player is destroyed create new player, if it is not then end game.
I would do that like this:


if Player.deaded then begin
if Lives > 0 then //if there are lives left create new player
NewPlayer
else //if not end the game
GameEnd;
end;
//continue with the game

Wizard
21-12-2006, 08:29 AM
Sorry guys, couln't get the shields to work :(

I have decided to give bonus lives for my little fighter at intervals of points scored i.e. 25000,50000 and so on.

It works well :)

grudzio
21-12-2006, 12:05 PM
I am sorry I could not help you.



I have decided to give bonus lives for my little fighter at intervals of points scored i.e. 25000,50000 and so on.

It works well

At last some good news :D.

Wizard
21-12-2006, 12:33 PM
No problem friend :D

I'm finished with my starfighter project ---- was a steep learning curve :-)

I now want to move on to a car racer game....any suggestions? Where can I find examples? Can I still use DelphiX?

Thanks for your time :lol: