School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,724 people online right now. Registration is fast and FREE... Join Now!




My Enemies Won't Fire

 

My Enemies Won't Fire, Using Dark GDK in a vertical shooter...But enemy planes will not fire

ovan35

13 Oct, 2009 - 08:56 PM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2009
Posts: 6

Alright I'm working on a vertical shooter using the Dark Gdk.My main problem is the enemy planes in the game.The movement is simple enough they move from the top of the screen to the bottom in a straight line..The planes are suppose to shoot a bullet at a random moment as they move down the screen.The Bullet will move straight down relative to the position of the plane that fired it.The problem right now is that for some reason only one plane will fire.Usually quite early in the game.After that one fires none of the other planes will fire.I can't figure out how to get it to fire again... Ok here is the code I have so far...

enemy.h- controls the creation of enemies as well as the firing of bullets

CODE

void enemy()
{
// in this loop we're going to create some enemy sprites
    for ( int i = 5; i < 9; i++ )
    {
        
        dbLoadImage ("enemy.bmp", i );

        // position our sprite at a random location
        
        
        dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
        dbScaleSprite(i,50); //scale the sprite by half
        
        
        
        
            
        
    }

}

void enemyFire()
{
        for (int i =5; i <9; i++)
    {
        
        int fire = dbRnd(100); //set a random value to fire
                       if(fire >85) //if fire is greater than 85
        {
            for(int j = 10; j < 15; j++)
            {
      //Create Bullets at the sprite
            dbSprite(j,dbSpriteX(i), dbSpriteY(i),4);
                        spriteX=dbSpriteX(i);
                        spriteY=dbSpriteY(i);
                        spriteshot=true;
                        
            }
            
        }
        
        
    }

}



moving.h- handles player movement, bullet and enemy movement

CODE

void moving()
{
    if(dbLeftKey() == 1) //If Left key is pressed
    {
        dbRotateSprite(2,90);
        dbMoveSprite(2,-3);
        dbRotateSprite(2,0);
    }
    if(dbRightKey() == 1) //If Right key has been pressed
    {
        dbRotateSprite(2,90);
        dbMoveSprite(2,3);
        dbRotateSprite(2,0);
    }
    if(dbUpKey() == 1) //If the up key has been pressed
    {
        dbMoveSprite(2,3);
    }
    if(dbDownKey() == 1) //If the Down Key has been pressed
    {
        dbMoveSprite(2,-3);
    }
    
    dbMoveSprite(3,5); //Move our bullet up the screen
for ( int i = 5; i < 9; i++ )
    {
        
        

    dbMoveSprite(i,-3); //move our enemy sprite down the screen
    }
for(int j = 10; j < 15; j++)
{
    dbMoveSprite(j, -5); //Move our enemy bullets down the screen
}
    

}

void collision()
{
    for(int i =5; i <9; i++)
    {
    if(dbSpriteCollision(3,i))
    {
        dbDeleteSprite(3);
        dbDeleteSprite(i);

        
        dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
        dbScaleSprite(i,50);
        
        
    }

    
    }

    
}

void shooting()
{
if(dbSpriteCollision(3,0) == 0) //If the bullet isn't collising with anything
{
    if(dbSpaceKey() ==1) //If the shift key is being pressed
    {
        dbStopSound(2);
        dbSprite(3,dbSpriteX(2)+ 25,dbSpriteY(2)+ 25,3);
        dbPlaySound(2);
    }

}
}
void border()
{
    for(int i =5; i <9; i++)
    {
    if(dbSpriteY(i) > 600)
    {
        
        
        dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
        dbScaleSprite(i,50);
    }
    }
}


        

        






and finally
main.cpp file
CODE


#include "DarkGDK.h"

//make these global
bool spriteshot;
int spriteX;
int spriteY; //these can also be floats
//---------------
#include "setup.h"
#include "moving.h"
#include "enemy.h"
#include "music.h"


// the main entry point for the application is this function


//---------------
void DarkGDK ( void )
{

    
    
    setup();
    load();
    create();
    enemy();
    enemyFire();
    music();
    playmusic();
    
    
    
    

    // now we come to our main loop, we call LoopGDK so some internal
    // work can be carried out by the GDK
    while ( LoopGDK ( ) )
    {
        
    
        moving();
        
        collision();
        border();
        shooting();
        
        

        // here we check if the escape key has been pressed, when it has
        // we will break out of the loop
        if ( dbEscapeKey ( ) )
            break;

        // here we make a call to update the contents of the screen
        dbSync ( );
    }

    


    

    // delete the backdrop image
    dbDeleteImage ( 1 );

    // and now everything is ready to return back to Windows
    return;
}



The part I think is the culprit is in the enemy.h file... the enemyFire() function but I can't quite figure out what to do about it.If anyone has a solution I would gladly try any suggestions on what to do.

User is offlineProfile CardPM
+Quote Post


modi123_1

RE: My Enemies Won't Fire

14 Oct, 2009 - 05:49 AM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,839



Thanked: 80 times
Dream Kudos: 150
My Contributions
I don't see a clear path on how your enemies will continue firing - since the call is not in teh while loop. Put it there and see what happens.
User is offlineProfile CardPM
+Quote Post

ovan35

RE: My Enemies Won't Fire

14 Oct, 2009 - 08:05 AM
Post #3

New D.I.C Head
*

Joined: 6 Oct, 2009
Posts: 6

I have tried it what happens when I do that is you can see a flickering bullet on each of the planes but the bullet dosen't go anywhere.It simply stays by the plane.This must be alot harder than I thought.No one seems to be able to give me an answer..Did I approach it wrong?Should I use something other than the for loop that I came up with?
User is offlineProfile CardPM
+Quote Post

modi123_1

RE: My Enemies Won't Fire

14 Oct, 2009 - 11:52 AM
Post #4

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,839



Thanked: 80 times
Dream Kudos: 150
My Contributions
QUOTE(ovan35 @ 14 Oct, 2009 - 10:05 AM) *

I have tried it what happens when I do that is you can see a flickering bullet on each of the planes but the bullet dosen't go anywhere.It simply stays by the plane.This must be alot harder than I thought.No one seems to be able to give me an answer..Did I approach it wrong?Should I use something other than the for loop that I came up with?


Are you updating the bullet's location once it is fired? I didn't see that anywhere either.
User is offlineProfile CardPM
+Quote Post

ovan35

RE: My Enemies Won't Fire

14 Oct, 2009 - 12:51 PM
Post #5

New D.I.C Head
*

Joined: 6 Oct, 2009
Posts: 6

QUOTE(modi123_1 @ 14 Oct, 2009 - 11:52 AM) *

QUOTE(ovan35 @ 14 Oct, 2009 - 10:05 AM) *

I have tried it what happens when I do that is you can see a flickering bullet on each of the planes but the bullet dosen't go anywhere.It simply stays by the plane.This must be alot harder than I thought.No one seems to be able to give me an answer..Did I approach it wrong?Should I use something other than the for loop that I came up with?


Are you updating the bullet's location once it is fired? I didn't see that anywhere either.


Yes I did in the moving.h file... the same way I had put in the enemies movement I put in the bullet movement

for(int j = 10; j < 15; j++)
{
dbMoveSprite(j, -5); //Move our enemy bullets down the screen
}

this will cycle through the bullets and should move all further down the screen.Just like a similar code moves the enemy planes down the screen. Like I said it works for the first bullet just fine.It must have something to do with the code responsible for making the bullet sprite..Maybe a way of delaying the creation of another bullet until the one fired exits the screen.I'm thinking right now it might be creating the same sprite over and over so fast that they never get to leave the plane. which is why you get the bullet flicker effect.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:08PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month