Adding bullet class array

Thanks in advance for reading this and any help you can provide. If I'm missing anything you need me to post of my code let me know.

Yes I am new to programming.

I'm making a topdown space shooter, I need to add a bullet array, I've added the class, I've added the bullet structure and everything I need to shoot. It wont shoot though so I've missed something but I can't figure it out. Code posted below.

First section is main
Second is the SBULLET.H
third is the SBULLET.CPP

I took a lot of the menu and instruction code because it shouldn't be relevant

THIS BLOCK IS MY MAIN FILE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#pragma comment(lib, "winmm.lib")

#include <string>
#include <cmath>
#include <Windows.h>
#include <time.h>
#include <assert.h>
#include <crtdbg.h>
#include <iostream>

#include "AIE.h"

#include "Player.h"
#include "Enemy.h"
#include "Bullets.h"
#include "Background.h"
#include "SBullet.h"

#define KEY_RETURN 294
#define KEY_UP 283
#define KEY_DOWN 284

struct Plane
{
	//float XSpeed;
	//float YSpeed;
	float XPosition;
	float YPosition;
	float XMovementSpeed;
	float YMovementSpeed;
	float Width;
	float Height; 
};


Plane Plane1;

//create a player
Player player1;
Enemy enemy1;
Background Background1;
SBullet Bullet1;

//Game functions
void initGame();
void updateGame();	
void drawGame();
void resetGame();

//menu functions
void updateMenu(); 
void drawMenu();

//Screen X and Y values
const int iScreenWidth = 1280;
const int iScreenHeight = 780;

//Max score
const int maxScore = 10;

//Menu items
bool StartGame ();
bool Readme ();           
bool ExitGame ();

unsigned int iPlaneSprite = 0;
unsigned int iEnemySprite = 0;
unsigned int iBulletsSprite = 0;
unsigned int iBackgroundSprite = 0;
unsigned int Menu = 0;
unsigned int Instructions = 0;


	//Declaring an enum for Game States

	enum GameState
{
	//Game states
	MAIN_MENU,
	HIGHSCORES,		
	GAME_STATE,
	INSTRUCTIONS,
	END
};

GameState eCurrentState = MAIN_MENU;

int main( int argc, char* argv[] )
{	
	srand( time(NULL) );

	InitialiseBullets();

	bool spacePressed = false;

	unsigned int currentTime = timeGetTime();
	unsigned int lastTime = currentTime;
	float deltaTime = 0;

	//initialise framework
	Initialise( iScreenWidth, iScreenHeight, false );
	
	//create game sprites
	//Plane sprites
	iPlaneSprite = CreateSprite( "./images/Phoenix.png", 96, 96, true );
	iEnemySprite = CreateSprite( "./images/Viking.png", 96, 96, true );
	iBulletsSprite = CreateSprite( "./images/crate_sideup.png", 32, 32, true );
	iBackgroundSprite = CreateSprite( "./images/space_bg8574.png", 1280, 3600, true );
	//Menu Sprites
	Menu = CreateSprite( "./images/Menu.png", 1280, 780, true );
	Instructions = CreateSprite( "./images/Instructions.png", 1280, 780, true );

	//framework
	
	do 
	{
		

		ClearScreen();
		switch(eCurrentState)
		{
		// if space is pressed, shoot from the center of the screen
		if( IsKeyDown(' ') /*&& !spacePressed*/ )
		{
			float shootX = iScreenWidth / 2.0f;
			float shootY = iScreenHeight / 2.0f;

			// get the mouse pos
			int mousePosX, mousePosY;
			GetMouseLocation( mousePosX, mousePosY );

			// calculate direction to the the mouse from the shoot pos
			float dirToMouseX = mousePosX - shootX;
			float dirToMouseY = mousePosY - shootY;

			// normalise the direction
			float len = sqrt( dirToMouseX*dirToMouseX + dirToMouseY*dirToMouseY );
			dirToMouseX /= len;
			dirToMouseY /= len;

			// finally shoot
			Shoot( shootX, shootY, dirToMouseX, dirToMouseY, 300.0f);
		}
		case MAIN_MENU:
			updateMenu();
			drawMenu();
			break;

		case GAME_STATE:
			updateGame();
			drawGame();
			break;

		case HIGHSCORES:
			updateHighscores();
			drawHighscores();
			break;

		case INSTRUCTIONS:
			updateInstructions();
			drawInstructions();
			break;
		}

		UpdateBullets(deltaTime);
		DrawBullets();


		spacePressed = IsKeyDown( ' ' );
		Sleep(1);

	} while ( FrameworkUpdate() == false );
	
	//Sprite destroys
	DestroySprite(iPlaneSprite);
	DestroySprite(iEnemySprite);
	DestroySprite(iBulletsSprite);
	DestroySprite(iBackgroundSprite);
	DestroySprite(Instructions);
	DestroySprite(Menu);


	Shutdown();

	return 0;
}

void initGame()
{
	eCurrentState = GAME_STATE;
	Background1.Init(iBackgroundSprite);
	player1.Init(iPlaneSprite);
	enemy1.Init(iEnemySprite);
}

void updateGame()
{
	Background1.Update();
	player1.Update();
	enemy1.Update();
}
		
	//Collision for plane!


void drawGame()
{
	Background1.Draw();
	player1.Draw();
	enemy1.Draw();
}

	//Game reset function
void resetGame()
{
	Background1.Reset();
	player1.Reset();
	enemy1.Reset();
}




SBULLET.H FILE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef SBULLET_H
#define SBULLET_H

struct SBullet
{
	unsigned int spriteID;
	float xPos, yPos, xDir, yDir, speed;
	float timeAlive;
};

// constant int refering to the max number of bullets available on screen
const int MAX_BULLETS = 10;
extern SBullet *g_bullets[ MAX_BULLETS ];

void InitialiseBullets();

// creates a bullet on the heap and returns to the caller
SBullet *CreateBullet(float x, float y, float xDir, float yDir, float speed);

// destroys the created bullet
void DestroyBullet( SBullet *pBullet );

// shoot function will create a bullet and add to the g_bullet array
// returs true on success
// returns false if there are no available slots to fill in the array
bool Shoot(float x, float y, float xDir, float yDir, float speed);

void UpdateBullets(float deltaTime);
void DrawBullets();


#endif 




SBULLET.CPP FILE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "SBullet.h"
#include "Aie.h"

// define bullets array
SBullet *g_bullets[ MAX_BULLETS ];


void InitialiseBullets()
{
	for(int i=0; i<MAX_BULLETS; i++)
		g_bullets[i] = 0;
}

SBullet *CreateBullet(float x, float y, float xDir, float yDir, float speed)
{
	SBullet *bullet = new SBullet();
	bullet->xPos = x;
	bullet->yPos = y;
	bullet->xDir = xDir;
	bullet->yDir = yDir;
	bullet->speed = speed;
	bullet->spriteID = CreateSprite("./Images/crate_sideup.png", 16, 16);
	return bullet;
}

void DestroyBullet( SBullet *pBullet )
{
	DestroySprite( pBullet->spriteID );
	delete pBullet;
}

bool Shoot(float x, float y, float xDir, float yDir, float speed)
{
	// loop through bullets to find the first available bullet
	for(int i=0; i<MAX_BULLETS; i++)
	{
		if( g_bullets[i] == 0 )
		{
			g_bullets[i] = CreateBullet(x, y, xDir, yDir, speed);
			return true;
		}
	}

	// failed to shoot, bullet array was full
	return false;
}


void UpdateBullets(float deltaTime)
{
	for(int i=0; i<MAX_BULLETS; i++)
	{
		if( g_bullets[i] == 0 )
			continue;

		g_bullets[i]->xPos += g_bullets[i]->xDir * g_bullets[i]->speed * deltaTime;
		g_bullets[i]->yPos += g_bullets[i]->yDir * g_bullets[i]->speed * deltaTime;


		g_bullets[i]->timeAlive += deltaTime;
		
		// if the bullet has been alive for longer than 1 second, destroy
		if( g_bullets[i]->timeAlive > 2.0f )
		{
			DestroyBullet( g_bullets[i] );
			g_bullets[i] = 0;
		}
	}
}
void DrawBullets()
{
	for(int i=0; i<MAX_BULLETS; i++)
	{
		if( g_bullets[i] == 0 )
			continue;

		MoveSprite( g_bullets[i]->spriteID, g_bullets[i]->xPos, g_bullets[i]->yPos );
		DrawSprite( g_bullets[i]->spriteID );
	}
}
Last edited on
Why is the code from lines 123 to 143 inside a switch body? I think it won't be executed. Try moving it above the switch.

Also, the bullet speed seems very high (300.0 pixels/frame). Even if it is drawn you may not see it since it would cross the screen in only a few frames.
In my games with a frame rate of about 30 fps I use bullet speeds of 10 to 20 pixels/frame.

Please don't post the same problem in multiple forums. If you get no response after a while, just bump the thread until someone interested in the problem sees it.
Last edited on
I didn't even realize that I'd put it in the switch statement, I was just messing around with the placement, that seemed to help it was the speed of the bullets, I just couldn't see them. Thank you for that. :)

Sorry about that, I posted it here then thought I might be better posting it in the other forum, I wont do it again though. :)
Last edited on
I see float deltaTime = 0; on line 98. Where is the value changed? If it's still = 0 at line 165 then that may be why the bullet doesn't move.

EDIT: Also, where is the value of g_bullets[i]->timeAlive initialized?
Should you have bullet->timeAlive = 0; in your CreateBullet() function?
Last edited on
Yep that did it, thanks for that I typed up another lil bit

1
2
3
lastTime = currentTime;
currentTime = timeGetTime();
deltaTime = (currentTime - lastTime) / 1000.0f;


working perfectly now, I just need to make it shoot from player and that's done. :D

thank you so much!
You're welcome. I'm glad it turned out to be that easy.
Your code looks quite good for a beginner.
Thank you, it's bits from my already written up pong game and patched up with bits from a couple of other games where I wasn't certain on things. :)
Topic archived. No new replies allowed.