Hello guys !
I have made this game called \"Battleship\" . The thing is that i want to have a ship which can fire at planes with a flak canon and at other ships with aa torpedo. Well the main problem is that i cant figure out how can I make collision with those random moving sprites, what to do ? the second problem I have is that when i want to fire my flak or torpedo at once , one disables th other. I think its best that you try to write the code into your c++ and see for yourself.
I will put the code here so you can just \"copy - paste\" and a link where you can download the media (pictures). Also I will upload the whole project so you can download it but it has cca. 6 MB so as you want. Please guys I realy need your help , its for a school project !
link for media : http://www.2shared.com/file/J_KtBfJw/Media.html
#include \"DarkGDK.h\"
#include <time.h>
// loading all media needed for the game
void load_media()
{
dbLoadImage(\"Media//player.png\",1);
dbLoadImage(\"Media//plane.png\",2);
dbLoadImage(\"Media//oceanis.bmp\",3);
dbLoadImage(\"Media//torpedo.png\",4);
dbLoadImage(\"Media//flak.png\",5);
}
// this is the loop where we make the player(ship), and the number of planes which will be
// created and postioned randomly ond the screen, and also I made this gaps to make the distance
// between spawning planes max 1000 at Y , and 100 at X, i think you will understand
void game_construction(int number_planes)
{
int player=100;
int gap_y=1000;
int gap_x=100;
int Ygap=0;
int Xgap=0;
dbSprite (player,500,655,1);
for (int i=0 ; i < number_planes ; i++)
{
Ygap=dbSpriteY(i)+dbRnd(gap_y);
Xgap=dbSpriteX(i)+dbRnd(gap_x);
dbSprite(i,Xgap,Ygap,2);
}
}
//random integer x
int x=100;
// here is the function for the movement of the player
void movement(int player)
{
if (dbRightKey()==1){x+=2;}
if (dbLeftKey()==1){x-=2;}
dbSprite(player,x,dbSpriteY(player),1);
}
// here is the function for movement of the planes downwards
void movement_plane()
{
for(int i=0; i <10 ; i++)
{
dbMoveSprite(i,-5);
}
}
// this is the function for reseting the planes once they reached the end of the screen, I made it 750 beacuse
// I have set up reslution to 720 as you will probably see in the MAIN loop
void reset_plane()
{
for (int i=0; i<10 ; i++)
{
if(dbSpriteY(i) > 750)
{
dbSprite(i,dbRnd(1280),-100,2);
}
}
}
// this is the function for firing the torpedos, I had an idea that i will make this game with enemy ships and planes
// so the torpedo would be only for collision with the ships but i havent made that loop yet where the ships move
// from left to right, but the problem is that i want it to launch and go to the END of the screen when i hit CTRL
// button but this isnt the case, and also i want to make it not possible to shoot a new torpedo before this one hits the target
// or passes the end of the screen
void fire_torpedo()
{
int torpedo;
if( dbControlKey())
{
dbSprite(torpedo, x+45, 650, 4);
}
for(int i=0; i <5 ; i++)
{
dbMoveSprite(torpedo,1);
}
}
// same as torpedos, but this is used to shoot planes, but the problem is that it also stays on the screen when
// you press SPACE key and when you release it, then it starts moving. I want it to move immediatly as you press
// SPACE key and to go till te end of the screen or until it hits a target, as I explained before the \"fire_torpedo\"
// function
void fire_flak()
{
int flak;
if( dbSpaceKey())
{
dbSprite(flak, x+120, 550, 5);
}
for(int i=0; i <5 ; i++)
{
dbMoveSprite(flak,2);
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetDisplayMode ( 1280, 720, 32 );
// this is for reseting the loop so that the planes move constantly downwards
srand(time(NULL));
load_media();
game_construction(10);
// our main loop
while ( LoopGDK ( ) )
{
// these are the functions
movement(100);
movement_plane();
reset_plane();
fire_torpedo();
fire_flak();
dbPasteImage(3,0,0);
// update the screen
dbSync ( );
}
// return back to windows
return;
}