Jump to content

  • Log In with Google      Sign In   
  • Create Account

Allegro Shooting Projectile help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Lickatoad   Members   -  Reputation: 118

Like
0Likes
Like

Posted 14 February 2013 - 06:37 AM

Hi all, i'm new to programming, C++ and Allegro and am a bit stuck. I've got a tank shooting projectiles 4 directions but when I change player direction, the bullet changes mid flight.I know it's bullet[i].bulletX linking to my tank.x but if I try and change the bullets x/y coord the bullet stays still. I'm not sure where to go from here so any help would be appreciated.

Thanks in advance.

 

(The section of code is at the bottom of main.cs attached, void UpdateBullet(Bullet bullet[], int size, TankPlayer &tank) )

 

 

main.cs

#include <allegro5\allegro5.h> 
#include <allegro5\allegro_native_dialog.h> 
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_ttf.h>
//#include <allegro5\allegro_font.h>

#include "objects.h"

const int WIDTH = 800;
const int HEIGHT = 400;

const int NUM_BULLETS = 5;

enum KEYS {UP, DOWN, LEFT, RIGHT, SPACE, ENTER};
bool keys[6] = {false, false, false, false, false, false};

void InitTank(TankPlayer &tank);
void Drawtank(TankPlayer &tank);
void MoveTankUp(TankPlayer &tank);
void MoveTankDown(TankPlayer &tank);
void MoveTankLeft(TankPlayer &tank);
void MoveTankRight(TankPlayer &tank);

void InitBullet(Bullet bullet[], int size, TankPlayer &tank);
void DrawBullet(Bullet bullet[], int size);
void FireBullet(Bullet bullet[], int size, TankPlayer &tank);
void UpdateBullet(Bullet bullet[], int size, TankPlayer &tank);


int main()
{
	//Variables
	bool done = false;
	bool draw = true;
	const int FPS = 60.0;

	//Object Variables
	TankPlayer tank;
	Bullet bullets[5];

	//Allegro Variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL; 
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_FONT *font18 = NULL;
	ALLEGRO_BITMAP *tankimage = NULL;


	//Allegro Init
	if(!al_init())
		al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 
	
	display = al_create_display(WIDTH, HEIGHT);

	if(!display)
		al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL);
		

	//al_set_window_position(display, 200, 200);
	

	//Install addons
	al_init_primitives_addon(); 
	al_install_keyboard();
	//al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();



	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS);

	//srand(time(NULL));
	
	//tankimage = al_load_bitmap("tank.png");
	//al_convert_mask_to_alpha(tankimage, al_map_rgb(255,0,255));	
	
	font18 = al_load_font("arial.ttf", 18, 0);

	//Game Init
	InitTank(tank);
	InitBullet(bullets, NUM_BULLETS, tank);


	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_display_event_source(display));

	al_start_timer(timer);
	while(!done)
	{
		ALLEGRO_EVENT events; 
		al_wait_for_event(event_queue, &events);

		if(events.type == ALLEGRO_EVENT_TIMER)
		{
			draw = true;			
			if(keys[UP])
				MoveTankUp(tank);				
			if(keys[DOWN])
				MoveTankDown(tank);				
			if(keys[LEFT])
				MoveTankLeft(tank);
			if(keys[RIGHT])	
				MoveTankRight(tank);
			
			UpdateBullet(bullets, NUM_BULLETS, tank);
			
			/*if(!isGameOver)
				{

				}*/

		}
		else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}

		else if(events.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(events.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;			
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				FireBullet(bullets, NUM_BULLETS, tank);
				break;
			case ALLEGRO_KEY_ENTER:
				keys[ENTER] = true;
				break;
			}
		}
		else if(events.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(events.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;			
			}
		}

		if(draw && al_is_event_queue_empty(event_queue))
		{
			draw = false;

			//al_draw_rectangle(x, y, x + 10, y + 10, al_map_rgb(44, 117, 255), 1);
			Drawtank(tank);
			DrawBullet(bullets, NUM_BULLETS);

			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));

//			al_draw_textf(font18, al_map_rgb(255,0,255), 5, 5, 0, "yooyoyoyo");

		}
	}
	al_destroy_display(display);
	al_destroy_timer(timer);
	al_destroy_event_queue(event_queue);
	
	return 0;
}



void InitTank(TankPlayer &tank)
{
	tank.ID = PLAYER;
	tank.x = 20;
	tank.y = HEIGHT / 2;
	tank.speed = 5;
	tank.dir = 1;	
	tank.boundX = 6;
	tank.boundY = 7;

}
void Drawtank(TankPlayer &tank)
{
	
    int dir = tank.dir;
    int x = tank.x;
	int y = tank.y;

    //draw tank body and turret
    al_draw_filled_rectangle(x-11, y-11, x+11, y+11, al_map_rgb(255,0,255));
    al_draw_filled_rectangle(x-6, y-6, x+6, y+6, al_map_rgb(255,0,255));
    
    //draw the treads based on orientation
    if (dir == 0 || dir == 2)
    {
        al_draw_filled_rectangle(x-16, y-16, x-11, y+16, al_map_rgb(255,0,255));
        al_draw_filled_rectangle(x+11, y-16, x+16, y+16, al_map_rgb(255,0,255));
    }
    else
    if (dir == 1 || dir == 3)
    {
        al_draw_filled_rectangle(x-16, y-16, x+16, y-11, al_map_rgb(255,0,255));
        al_draw_filled_rectangle(x-16, y+16, x+16, y+11, al_map_rgb(255,0,255));
    }
    
    //draw the turret based on direction
    switch (dir)
    {
        case 0:
            al_draw_filled_rectangle(x-1, y, x+1, y-16, al_map_rgb(255,0,255));
            break;
        case 1:
            al_draw_filled_rectangle(x, y-1, x+16, y+1, al_map_rgb(255,0,255));
            break;
        case 2:
            al_draw_filled_rectangle(x-1, y, x+1, y+16, al_map_rgb(255,0,255));
            break;
        case 3:
            al_draw_filled_rectangle(x, y-1, x-16, y+1, al_map_rgb(255,0,025));
            break;
    }
}

void MoveTankUp(TankPlayer &tank)
{
	tank.y -= tank.speed;
	if(tank.y < 0)
		tank.y = 0;
	tank.dir = 0;
}
void MoveTankDown(TankPlayer &tank)
{
	tank.y += tank.speed;
	if(tank.y > HEIGHT)
		tank.y = HEIGHT;
	tank.dir = 2;

}
void MoveTankLeft(TankPlayer &tank)
{	
	tank.x -= tank.speed;
	if(tank.x < 0)
		tank.x = 0;
	tank.dir = 3;
}
void MoveTankRight(TankPlayer &tank)
{
	tank.x += tank.speed;
	if (tank.x > WIDTH)
		tank.x = WIDTH;
	tank.dir = 1;
}



void InitBullet(Bullet bullet[], int size, TankPlayer &tank)
{
	for(int i = 0; i < size; i++)
	{
	bullet[i].ID = BULLET;
	bullet[i].bulletSpeed = 10;	
	bullet[i].live = false;
	bullet[i].bulletdir = 1;
	
	bullet[i].bulletdir = tank.dir;
	}	
}


void DrawBullet(Bullet bullet[], int size)
{
	for (int i = 0; i < size; i++)
		al_draw_filled_circle(bullet[i].bulletX- 1, bullet[i].bulletY - 1, 3, al_map_rgb(200,200,200));
}

void FireBullet(Bullet bullet[], int size, TankPlayer &tank)
{		

	 switch (tank.dir)
    {
        case 0:
            for( int i = 0; i < size; i++)
				{	
					if(!bullet[i].live)
					{
						bullet[i].bulletX = tank.x;
						bullet[i].bulletY = tank.y + 17;
						bullet[i].live = true;
						break;
					}	
				}
            break;
        case 1:
            for( int i = 0; i < size; i++)
				{	
					if(!bullet[i].live)
					{
						bullet[i].bulletX = tank.x + 17;
						bullet[i].bulletY = tank.y;
						bullet[i].live = true;
						break;
					}	
				}
            break;
        case 2:
            for( int i = 0; i < size; i++)
				{	
					if(!bullet[i].live)
					{
						bullet[i].bulletX = tank.x;
						bullet[i].bulletY = tank.y - 17;
						bullet[i].live = true;
						break;
					}	
				}
            break;
        case 3:
            for( int i = 0; i < size; i++)
				{	
					if(!bullet[i].live)
					{
						bullet[i].bulletX = tank.x - 17;
						bullet[i].bulletY = tank.y;
						bullet[i].live = true;
						break;
					}	
				}
            break;
    }

}

void UpdateBullet(Bullet bullet[], int size, TankPlayer &tank)

{	
    switch (tank.dir)
    {
        case 0: //up
						for(int i = 0; i < size; i++)
				{		
					if(bullet[i].live)
					{						
						bullet[i].bulletY -= bullet[i].bulletSpeed;
						if(bullet[i].bulletY < 0)
						bullet[i].live = false;
					}
				}
            break;
        case 1: //right
						for(int i = 0; i < size; i++)
				{		
					if(bullet[i].live)
					{
						bullet[i].bulletX += bullet[i].bulletSpeed;
						if(bullet[i].bulletX > WIDTH)
						bullet[i].live = false;
					}
				}
            break;
        case 2: //down
            for(int i = 0; i < size; i++)
				{		
					if(bullet[i].live)
					{
						bullet[i].bulletY += bullet[i].bulletSpeed;
						if(bullet[i].bulletY > HEIGHT)
						bullet[i].live = false;
					}
				}
            break;
        case 3: //left
            for(int i = 0; i < size; i++)
				{		
					if(bullet[i].live)
					{
						bullet[i].bulletX -= bullet[i].bulletSpeed;
						if(bullet[i].bulletX < 0)
						bullet[i].live = false;
					}
				}
            break;
    }
}

 

 

 

/////////////////////////////////////////////////////////////////////

Objects.h 

//////////////////////////////////////////////////////////////////////

enum IDS{PLAYER, BULLET};

struct TankPlayer
{
	int ID;
    int x;
	int y;
    int dir;
	int speed;

	int boundX;
	int boundY;

	//ALLEGRO_BITMAP *image;
};

struct Bullet
{
	int ID;
	int bulletX;
	int bulletY;
	int bulletSpeed;

	int bulletdir;

	bool live;
};


 

 

 



Sponsor:

#2 amrazek111   Members   -  Reputation: 692

Like
3Likes
Like

Posted 14 February 2013 - 07:25 AM

You've almost got it.  In UpdateBullet, you calculate its direction based on which way the tank is facing.  Change the switch to query the bullet's bulletdir instead.  Then copy the tankdir into the bullet's bulletdir every time you set up a new bullet.



#3 Lickatoad   Members   -  Reputation: 118

Like
0Likes
Like

Posted 14 February 2013 - 07:03 PM

Thanks very much for your help, i've nearly got it working. I've changed the bulletdir into the switch. 

Under init bullet if I manually enter the direction it fires perfectly but i'm now having a hard time getting the tanks direction in there. I've tried doing it 2 ways, as listed below.

 

    if (tank.dir == 0)
            bullet[i].bulletdir = 0;
    else if (tank.dir == 1)
            bullet[i].bulletdir = 1;
    else if (tank.dir == 2)
            bullet[i].bulletdir = 2;
    else if (tank.dir == 3)
            bullet[i].bulletdir = 3;


***** Or i've tried doing

bullet[i].bulletdir = tank.dir
 
 
 
 
 
*********Heres the updated switch (and I can probably remove the tankplayer &tank reference now)
 
void UpdateBullet(Bullet bullet[], int size, TankPlayer &tank)
 
{    
    for(int i = 0; i < size; i++)
 
    switch (bullet[i].bulletdir)
    {
        case 0: //up
                for(int i = 0; i < size; i++)
                {        
                    if(bullet[i].live)
                    {                        
                        bullet[i].bulletY -= bullet[i].bulletSpeed;
                        if(bullet[i].bulletY < 0)
                        bullet[i].live = false;
                    }
                }
            break;
        case 1: //right
                for(int i = 0; i < size; i++)
                {        
                    if(bullet[i].live)
                    {
                        bullet[i].bulletX += bullet[i].bulletSpeed;
                        if(bullet[i].bulletX > WIDTH)
                        bullet[i].live = false;
                    }
                }
            break;
        case 2: //down
            for(int i = 0; i < size; i++)
                {        
                    if(bullet[i].live)
                    {
                        bullet[i].bulletY += bullet[i].bulletSpeed;
                        if(bullet[i].bulletY > HEIGHT)
                        bullet[i].live = false;
                    }
                }
            break;
        case 3: //left
            for(int i = 0; i < size; i++)
                {        
                    if(bullet[i].live)
                    {
                        bullet[i].bulletX -= bullet[i].bulletSpeed;
                        if(bullet[i].bulletX < 0)
                        bullet[i].live = false;
                    }
                }
            break;
    }
}
 

 

 

 

 

Trying it either way only shoots right. 

 

Under InitTank where i've originally set the original tank direction to 1, it seems it's only keeping that figure when I shoot through the above statements and not updating it accordingly, or passing on the updated values.

 

I know it's going to be simple and i'm probably overthinking things. Thanks again for your help.


Edited by Lickatoad, 14 February 2013 - 07:10 PM.


#4 amrazek111   Members   -  Reputation: 692

Like
1Likes
Like

Posted 15 February 2013 - 01:31 AM

Thanks very much for your help, i've nearly got it working. I've changed the bulletdir into the switch. 

Under init bullet if I manually enter the direction it fires perfectly but i'm now having a hard time getting the tanks direction in there. I've tried doing it 2 ways, as listed below.

[...]

 

Trying it either way only shoots right.

 

You want to set the direction of the bullet at the time of firing, i.e. in FireBullet.  It always goes to the right because you set the bullet direction in InitBullets to 1 (right) and then never change it.  Add

bullet[i].bulletdir = tank.dir

To your for loops in FireBullet.

 

Also, consider improving readability a little.  For instance,

enum Direction {
NORTH = 0,
EAST = 1,
SOUTH = 2,
WEST = 3
}

 

And perhaps a function to locate a dead bullet index for you:

int FindDeadBulletIndex(Bullet bullet[], int size) {
    for (int i = 0; i < size; ++i)
        if (!bullet[i].live)
            return i;

    // didn't find an index; maybe delete old bullet or similar?
    return -1;
}

 

Then your new FireBullet might look like this:

void FireBullet(Bullet bullet[], int size, TankPlayer &tank) {        
    int index = FindDeadBulletIndex(bullet, size);

    if (index < 0)
        return; // no "open" bullet positions available

    bullet[index].live = true;
    bullet[index].bulletdir = tank.dir;
    
    // set bullet position to tank's position ...
    bullet[index].bulletX = tank.x;
    bullet[index].bulletY = tank.y;

    // ... or adjust position based on direction, if you want:
    if (tank.dir == NORTH) {
        bullet[index].bulletX = tank.x;
        bullet[index].bulletY = tank.y + 17;
    } else ... // and so on
}


#5 Lickatoad   Members   -  Reputation: 118

Like
0Likes
Like

Posted 15 February 2013 - 02:21 AM

Awesome! Thank works really well thank you and fires according to each direction. 
Just one last question, it could be due to the array used but if i'm firing in one direction then change to another the bullets sometimes change direction mid firing. Sometimes the bullets also fire diagonally then automatically correct themselves later too.


Other than that though I just wanted to say a big thanks, spent ages trying to figure that out :D



#6 amrazek111   Members   -  Reputation: 692

Like
1Likes
Like

Posted 15 February 2013 - 04:24 AM

Assuming your UpdateBullet function is the "updated" one from your second post, you've got a logic error in there.  Think about this: why are you iterating over all the bullets again after you've determined the direction bullet[i] is facing?  Shouldn't you be updating that one, single bullet based on its own direction?



#7 Lickatoad   Members   -  Reputation: 118

Like
0Likes
Like

Posted 15 February 2013 - 04:58 AM

Ok, I get what your saying and have an idea where to look and hopefully what to do next.

 

Thanks again for all your help, really appreciate it



#8 Lickatoad   Members   -  Reputation: 118

Like
0Likes
Like

Posted 15 February 2013 - 05:29 AM

Got it working great now, thanks again mate :D






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS