C++ simple 2d shooter

Dev-C++ 4.9.9.2 & Allegro

Page 1 of 1

3 Replies - 13372 Views - Last Post: 18 November 2009 - 01:14 PM Rate Topic: -----

#1 devcom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-November 09

C++ simple 2d shooter

Posted 11 November 2009 - 09:26 AM

Hi,

Im making a 2d shooter game and need help in some parts of it ( as i have some kind of blockade and i dont know how to do this ).
So here are the things i cant work out:

* how to make good / optimized bullet movement and createment
* good colision checking
* how to make moving world, like when you get closer to end of window the map moves
* AI ( but that in future )

Im using Dev-C++ 4.9.9.2 and Allegro.

Now.. I dont want from you guys a whole code,i made some games before but this shooter is something i cant figure out how to make, i need just some pointers and sample codes, rest i will figure or ill ask again :)

If you need something else to help me, please post :)
Here is my code i got so far:

#include <allegro.h>
#include <stdio.h>
#include <math.h>


#define GFX_WIDTH 800
#define GFX_HEIGHT 600
#define PI 3,1415

#define ESC 27
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
#define DOWNLEFT 10
#define UPLEFT 8
#define DOWNRIGHT 12
#define UPRIGHT 9

#define BLACK 101
#define RED   104

void init();
void deinit();
void moveBullet(int);
void checkKeys();
void movePlayer(int,char);
void moveBullets();
void createNewBullet();
inline void drawCroshair(int);

struct vec {
	   int x;
	   int y;
	   };
	   
struct controller {
	   vec pos;
	   // future vars //
	   };

struct bullets {
	   vec pos;
	   float angle;
	   vec vector;
	   bool live;
	   };
	   
vec mousepos;
bool gameRunning = true;   
controller player;
int bulletsCount = 0;
bullets bullet[1000]; // max 1000 bullets on screen

int main() 
{
  player.pos.x = 100;
  player.pos.y = 100;
  init();
  
  while(gameRunning){				  
  
				   checkKeys();  
				   
				   drawCroshair(BLACK);
				   
				   mousepos.x = mouse_x;
				   mousepos.y = mouse_y;
				   
				   drawCroshair(RED);
				   
				   if(mouse_b & 1){createNewBullet();}
				   moveBullets();
				   
				   
				   release_screen();
				   clear_keybuf(); 
				   rest(0);
			 
  }

  
  deinit();
  return 0;
  
  
}
END_OF_MAIN()


void moveBullets()
{
 int i;
 
 for(i = 0; i <= bulletsCount - 1; i++)
 {
	   if(!bullet[i].live){break;}
	   moveBullet(i);	  
 }			
}

void moveBullet(int id)
{
 int tx,ty;
 int speed = 5;
 
 if(bullet[id].pos.x < 0 || bullet[id].pos.y < 0 || bullet[id].pos.x > GFX_WIDTH || bullet[id].pos.y > GFX_HEIGHT){bullet[id].live = false;return;}
 tx = bullet[id].pos.x + cos( bullet[id].angle ) * speed;
 ty = bullet[id].pos.y + sin( bullet[id].angle ) * speed;
 
 circlefill(screen,bullet[id].pos.x,bullet[id].pos.x,2,makecol(0,0,0));
 
 bullet[id].pos.x = tx;
 bullet[id].pos.y = ty;
 
 circlefill(screen,bullet[id].pos.x,bullet[id].pos.x,2,makecol(255,123,1239));
 
}


void createNewBullet()
{
   int i;

   bulletsCount++;
   for(i = 0;i <= bulletsCount - 1;i++){
   
   if(!bullet[i].live){
						
						bullet[i].pos.x = player.pos.x;
						bullet[i].pos.y = player.pos.y;
						
						bullet[i].vector.x = mousepos.x - bullet[i].pos.x;
						bullet[i].vector.y = mousepos.y - bullet[i].pos.y;
						
						bullet[i].angle = -atan(  (float)bullet[i].vector.y / (float)bullet[i].vector.x  ) * 180 / (float)PI;
						
						bullet[i].live = true;
						
						break;
						}					   
		 
   }	 
	   
}

inline void drawCroshair(int color)
{
	   
if(	abs(mousepos.x - player.pos.x) <= 33 && abs(mousepos.y - player.pos.y) <= 33){return;}

if(color == BLACK){  circlefill(screen,mousepos.x,mousepos.y,3,makecol(0,0,0)); }	   
if(color == RED){	circlefill(screen,mousepos.x,mousepos.y,3,makecol(255,0,0)); } 
  
}

void checkKeys()
{
	 int pkey;
	 
	 pkey = 0;
	 if(  key[KEY_LEFT] ){pkey = LEFT;}
	 if(  key[KEY_RIGHT]){pkey = RIGHT;}
	 if(  key[KEY_UP]   ){pkey = UP;}
	 if(  key[KEY_DOWN] ){pkey = DOWN;}
	 if(  key[KEY_DOWN] && key[KEY_LEFT]){pkey = DOWNLEFT;}
	 if(  key[KEY_UP] && key[KEY_LEFT]){pkey = UPLEFT;}
	 if(  key[KEY_DOWN] && key[KEY_RIGHT]){pkey = DOWNRIGHT;}
	 if(  key[KEY_UP] && key[KEY_RIGHT]){pkey = UPRIGHT;}
	 
	 if(  key[KEY_ESC]  ){pkey = ESC;}
	 
	 switch(pkey) {
		 case ESC:
			  gameRunning = false;
			  break;
			  
		 case DOWNLEFT:
			  movePlayer(-1,'c');
			  break;
		 case DOWNRIGHT:
			  movePlayer(1,'C');
			  break;
		 case UPLEFT:
			  movePlayer(-1,'C');
			  break;
		 case UPRIGHT:
			  movePlayer(1,'c');
			  break;
			  
		 case LEFT:
			  movePlayer(-1,'x');
			  break;
		 case UP:
			  movePlayer(-1,'y');
			  break;
		 case DOWN:
			  movePlayer(1,'y');
			  break;
		 case RIGHT:
			  movePlayer(1,'x');
			  break;
		 };
		 
}

void movePlayer(int Move,char kind)
{
 
			   circlefill(screen,player.pos.x,player.pos.y,30,makecol(0,0,0));
			   
			   if(kind == 'x'){player.pos.x += Move;}
			   if(kind == 'y'){player.pos.y += Move;}
			   if(kind == 'c'){player.pos.x += Move;player.pos.y += -Move;}
			   if(kind == 'C'){player.pos.x += Move;player.pos.y += Move;}
							
			   circlefill(screen,player.pos.x,player.pos.y,30,makecol(255,123,1239));
			
}


void init() 
{
  int depth, res;
  
  allegro_init();
  
  depth = desktop_color_depth();
  if (depth == 0) depth = 32;
  
  set_color_depth(depth);
  
  res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_WIDTH, GFX_HEIGHT, 0, 0);
  
  if (res != 0) {
	allegro_message(allegro_error);
	exit(-1);
  }

  install_timer();
  install_keyboard();
  install_mouse();
}

void deinit() 
{
  clear_keybuf();
}


This post has been edited by devcom: 11 November 2009 - 09:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ simple 2d shooter

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: C++ simple 2d shooter

Posted 11 November 2009 - 09:31 AM

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

#3 devcom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-November 09

Re: C++ simple 2d shooter

Posted 17 November 2009 - 06:00 AM

Ok i've done big part of what i wanted but i have new bug witch im working on 2 days and i cant get it so please help..

Situation is like this:

Here is part of my code:
SPAWNING
... 
bulletsCount++; 
while(bullet[i].live){i++;} 

bullet[i].live = true; 

and some meaningless now vars here 



MOVING

void moveBullets() 
{ 

int i; 

for(i = 0; i <= bulletsCount - 1; i++) 
{ 
if(!bullet[i].live){continue;} 
moveBullet(i); 
} 

} 




void moveBullet(int id) 
{ 
... 

if(bullet[id].pos.x < -5 || bullet[id].pos.y < -5 || bullet[id].pos.x > GFX_WIDTH + 5 || bullet[id].pos.y > GFX_HEIGHT + 5){ 
bullet[id].live = false; 
bulletsCount--; // <--- 
return; 
} 

... 

} 



as you can see if some bullet is out of window the bulletsCount is decreasing but its also decreasing in moveBullets() function so some bullets will not move sometimes
i have no clue how to slove this
Was This Post Helpful? 0
  • +
  • -

#4 devcom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-November 09

Re: C++ simple 2d shooter

Posted 18 November 2009 - 01:14 PM

ok, i didnt done last bug but i have more simple questions for you:

* what technique is best for collisions
* what you think is best for server - client based networking ? i have zoidcom now but there can be better one

please answer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1