2 Replies - 3256 Views - Last Post: 04 December 2009 - 08:55 PM Rate Topic: -----

#1 krodar82   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-December 09

Help with code for shooting a bullet in a space game

Posted 04 December 2009 - 05:30 PM

Hi making a game in Dark GDK C++. I'm trying to figure out what I'm doing wrong with getting a space ship, which you control, to shoot a bullet towards an enemy that will be coming down the screen from the top. I have it right now so that the bullet will end up at the top of the screen but you can't see it actually move up the screen. I think I'm missing some type of delay but when I put it in the function the program won't run. Here is what I have for the bullet code.
 #include "DarkGDK.h"


const int SPACE_BACKGROUND_IMAGE = 1;
const int JET_IMAGE = 2;
const int BULLET_IMAGE = 3;
const int ENEMY_IMAGE = 4;


const int SPACE_BACKGROUND_SPRITE = 1;
const int JET_SPRITE = 2;
const int BULLET_SPRITE = 3;
const int ENEMY_SPRITE = 4;


const int BACKGROUND_SOUND = 1;
const int HIT_SOUND = 2;
const int SHOOT_SOUND = 3;


const int TOTAL_ENEMY = 20;
const int JET_MOVE = 5;
const float ACCELERATION = 0.5;
const int REFRESH_RATE = 60;

void initializeLoad();
void introScreen();
void createSprites(int&, int&, int&, int&, int&, int&);
void moveJet(int&);
void moveEnemy(int&, int& );
void moveBullet(int&, int&, int&, int&, bool&);
void checkCollisions(int&, int&, int&, int&, int&, int&, int&, bool&);
void render(int, int, int, int, int, int );
void summaryScreen(int);
void resetEnemy(int, int&, int&);
void resetBullet(int&, int&, bool&);
void deleteSprites();





void DarkGDK()
{
	int enemies = TOTAL_ENEMY;
	int hit = 0;
	int enemyX, enemyY;
	int jetX, jetY;
	int bulletX, bulletY;
	int time = 0;
	bool bulletFired = false;
	
	

	initializeLoad();

	introScreen();

	createSprites(enemyX, enemyY, jetX, jetY, bulletX, bulletY);

	while (LoopGDK() && enemies > 0 )
	{
		moveJet(jetX);

		moveBullet(bulletX, bulletY, jetX, jetY, bulletFired);
		
		moveEnemy(enemyY, time);

		checkCollisions(bulletX, bulletY, enemyX, enemyY, hit, enemies, time, bulletFired);

		render(jetX, jetY, enemyX, enemyY, bulletX, bulletY);

	}

summaryScreen(hit);
}

void initializeLoad()
{
	dbSetImageColorKey(0, 255, 0);

	
	dbLoadImage("spacepic.bmp", SPACE_BACKGROUND_IMAGE);
	dbLoadImage("ufo.bmp", ENEMY_IMAGE);
	dbLoadImage("jet.bmp", JET_IMAGE);
	dbLoadImage("bullet.bmp", BULLET_IMAGE);
	
	

	dbLoadMusic("gamemusic.mp3", BACKGROUND_SOUND);
	dbLoadSound("explosion.wav", HIT_SOUND);
	dbLoadSound("shoot.wav", SHOOT_SOUND);
}

void introScreen()
{
	dbSetWindowTitle("Alien Attack");

	dbCLS();

	dbSetTextFont("Arial");
	dbSetTextSize(36);

	dbSetCursor(40, 200);
	dbPrint("PREPARE TO KILL SOME ALIENS!");
	dbWaitKey();
	
	dbLoopMusic(BACKGROUND_SOUND);
	dbSyncOn();
	dbSyncRate(REFRESH_RATE);
	
}

void createSprites(int &enemyX, int &enemyY, int &jetX, int &jetY, int &bulletX, int &bulletY)
	{

dbSprite(SPACE_BACKGROUND_SPRITE, 0, 0, SPACE_BACKGROUND_IMAGE);
dbSprite(JET_SPRITE, 0, 0, JET_IMAGE);
dbSprite(ENEMY_SPRITE, 0, 0, ENEMY_IMAGE);
dbSprite(BULLET_SPRITE, 0, 0, BULLET_IMAGE);


dbHideSprite(BULLET_SPRITE);

dbRandomize( dbTimer() );

enemyX = dbRND( dbScreenWidth() - dbSpriteWidth(ENEMY_SPRITE) );

enemyY = 0;

jetX = dbRND( dbScreenWidth() - dbSpriteWidth(JET_SPRITE) );

jetY = dbScreenHeight() - dbSpriteHeight(JET_SPRITE);
 
bulletX = jetX;

bulletY = jetY;

}

void moveBullet(int &bulletX,int &bulletY, int &jetX, int &jetY, bool &bulletFired)
{
	

if (dbSpaceKey() )
	{
		dbShowSprite(BULLET_SPRITE);

		dbPlaySound(SHOOT_SOUND);

		bulletX = jetX;
		bulletY = jetY;
		bulletFired = true;

		}
		if (bulletFired = true)
		{
			for(int i = 640; i >= 0; i--)
			{
			bulletY = i;
			
			}
		}
}



I've messed around quite a bit with the move bullet function and recently put in the boolean to check for if the spacebar was pressed but I haven't had much luck, anyone have any ideas what I should be doing?

Thanks for the help, I appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with code for shooting a bullet in a space game

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Help with code for shooting a bullet in a space game

Posted 04 December 2009 - 06:13 PM

Moving to Game Programming.
Was This Post Helpful? 0
  • +
  • -

#3 frostyraver   User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 195
  • Joined: 21-April 09

Re: Help with code for shooting a bullet in a space game

Posted 04 December 2009 - 08:55 PM

		if (dbSpaceKey())
		{
			dbShowSprite(BULLET_SPRITE);

			dbPlaySound(SHOOT_SOUND);

			bulletX = jetX;
			bulletY = jetY;
			bulletFired = true;
		}
		if (bulletFired = true)
		{
			for(int i = 640; i >= 0; i--)
			{
				bulletY = i;
			}
		}




From what i can see you shouldn't need "if the bullet fired = true" after the decention of the space bar that sets it to fire = true.

What you could do is:
		if (dbSpaceKey())
		{
			dbShowSprite(BULLET_SPRITE);

			dbPlaySound(SHOOT_SOUND);

			bulletX = jetX;
			bulletY = jetY;

			for(int i = 640; i >= 0; i--)
			{
				bulletY = i;
			}
		}


You added a bool to my knowledge for no real reason, this will fire when you hit the space key.

I've never done dark basic before i'm just stabbing in the dark here.

EDIT: I see it's going straight to the top, if this was in XNA all I would do is insert a game.time reference to sync it with the current game time. See if that will work for you, from what I know dark basic goes off a game time also.

Like instead of

for(int i = 640; i >= 0; i--)


minusing i for no timer you will need to set up something like

for(int i = 640; i >= 0; i-=game.time(milseconds))



I don't know the syntax for it, but something like this should minus the game each milsecond.

This post has been edited by frostyraver: 04 December 2009 - 09:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1