Sorry it took so long. I was hunting down problems with destroying the bullets. Here is the complete code (I had to make my own player sprite):
#include "DarkGDK.h"
#include <vector>
int count=0;
using std::vector;
#define MAX_BULLETS 30
#define WallUpID 300
#define WallDownID 301
#define WallLeftID 302
#define WallRightID 303
int Round;
int EnemyNumber;
int GetFreeSpriteID ( void )
{
int id = 0;
while ( dbSpriteExist ( ++id ) );
return id;
}
struct BULLET{
public:
BULLET *Next,*Previous;
float x,y;
float Direction,Speed;
void make(int Px,int Py,float D,float S);
void destroy();
};
struct Player {
void Init();
void Update();
void Die();
void Shoot(int mx, int my, float S);
void Destroy();
int money, ammo, PlayerID;
float speed;
float x, y, width, height;
int velx, vely;
BULLET *LastBullet,*FirstBullet;
void MoveBullets();
}player;
void Player::MoveBullets(){
BULLET *B=FirstBullet->Next;
while (B!=NULL){
//move it
B->x+=dbCos(B->Direction)*B->Speed;
B->y+=dbSin(B->Direction)*B->Speed;
//check for collisions.....
bool Destroy=false;
//..... check for anything that will cause the bullet to need to be destoyed. this would set Destroy=true;
if ((B->x<0)||(B->x>1024)||(B->y<0)||(B->y>768)) Destroy=true;
if (!Destroy){
//display
dbDot(B->x, B->y);
//here we go to the next bullet
B=B->Next;
}
else {
BULLET *OldNext=B->Next;
B->destroy();
B=OldNext;
count--;
}
}
}
void BULLET::make(int Px,int Py,float D,float S){
Next=new BULLET;
Next->Next=NULL;
Next->Previous=player.LastBullet;
Next->x=Px;
Next->y=Py;
Next->Direction=D;
Next->Speed=S;
player.LastBullet=Next;
}
void BULLET::destroy(){
if ((Next!=NULL)&&(Previous!=NULL)){//in the middle
Previous->Next=Next;
Next->Previous=Previous;
Next=Previous=NULL;
delete this;
}
else if ((Next==NULL)&&(Previous!=NULL)){//last
Previous->Next=NULL;
player.LastBullet=Previous;
Next=Previous=NULL;
delete this;
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetImageColorKey(255, 0, 255);
dbDisableEscapeKey ( );
dbSetDisplayModeAntialias(1024, 768, 32, 1, 0, 0);
dbSetWindowPosition( 0, 0);
dbSetWindowTitle("GAMENAME - BLABLABLA");
player.Init();
while ( LoopGDK ( ) )
{
dbCLS(RGB(0, 0, 0));
player.Update();
if ( dbEscapeKey ( ) )
break;
dbText(0,0,dbStr(dbScreenFPS()));
dbText(0,40,dbStr(count));
dbSync ( );
}
for ( int i = 1; i < 30; i ++)
{
dbDeleteImage(i);
dbDeleteSprite(i);
}
player.Destroy();
return;
}
void Player::Init()
{
PlayerID = GetFreeSpriteID();
money = 1000;
speed = 3;
x = 100;
y = 100;
width = 10;
height = 10;
FirstBullet=new BULLET;
FirstBullet->Next=NULL;
FirstBullet->Previous=NULL;
LastBullet=FirstBullet;
dbLoadImage("pointer.png", PlayerID);
dbSprite(PlayerID, x, y, PlayerID);
}
void Player::Update()
{
//Movement
if(dbUpKey() == 1)
{
dbMoveSprite(PlayerID, speed);
if(dbSpriteCollision(PlayerID, WallUpID))
{
dbRotateSprite(PlayerID,180);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallDownID))
{
dbRotateSprite(PlayerID,180);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallLeftID))
{
dbRotateSprite(PlayerID,180);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallRightID))
{
dbRotateSprite(PlayerID,180);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
}
if(dbRightKey() == 1)
{
dbRotateSprite(PlayerID,90);
dbMoveSprite(PlayerID,speed);
dbRotateSprite(PlayerID,0);
if(dbSpriteCollision(PlayerID, WallUpID))
{
dbRotateSprite(PlayerID,270);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallDownID))
{
dbRotateSprite(PlayerID,270);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallLeftID))
{
dbRotateSprite(PlayerID,270);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallRightID))
{
dbRotateSprite(PlayerID,270);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
}
if(dbLeftKey() == 1)
{
dbRotateSprite(PlayerID,270);
dbMoveSprite(PlayerID,speed);
dbRotateSprite(PlayerID,0);
if(dbSpriteCollision(PlayerID, WallUpID))
{
dbRotateSprite(PlayerID,90);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallDownID))
{
dbRotateSprite(PlayerID,90);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallLeftID))
{
dbRotateSprite(PlayerID,90);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
if(dbSpriteCollision(PlayerID, WallRightID))
{
dbRotateSprite(PlayerID,90);
dbMoveSprite(PlayerID, speed);
dbRotateSprite(PlayerID,0);
}
}
if (dbDownKey() == 1)
{
dbRotateSprite(PlayerID,180);
dbMoveSprite(PlayerID,speed);
dbRotateSprite(PlayerID,0);
//COLLISION DOWN
if(dbSpriteCollision(PlayerID, WallUpID))
{
dbMoveSprite(PlayerID,speed);
}
if(dbSpriteCollision(PlayerID, WallRightID))
{
dbMoveSprite(PlayerID,speed);
}
if(dbSpriteCollision(PlayerID, WallLeftID))
{
dbMoveSprite(PlayerID,speed);
}
if(dbSpriteCollision(PlayerID, WallDownID))
{
dbMoveSprite(PlayerID,speed);
}
}
x=dbSpriteX(PlayerID);
y=dbSpriteY(PlayerID);
if(dbMouseClick())
{
Shoot(dbMouseX(), dbMouseY(), 0.5);
}
MoveBullets();
}
void Player::Shoot(int mx, int my, float S)
{
float D=dbATANFULL((my-y),(mx-x));
LastBullet->make(x, y, D, S);
count++;
}
void Player::Destroy()
{
//delete them all
while (FirstBullet->Next!=NULL) FirstBullet->Next->destroy();
delete FirstBullet;
}
I know you are going to have questions, so here is an attempt to answer some ahead of time.
-I had to get rid of the BULLETS::movebullets() function. I think there's a problem with destroying a pointer while you are using it...? So I made a new function Player::MoveBullets(). It handles it beautifully.
-I put a "count" variable in there to keep track of creation and destruction. When all the bullets are off the screen, this should be 0.
The fastest code is the code never written.