• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Lickatoad

Allegro Shooting Projectile help

7 posts in this topic

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;
};


 

 

 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
}
1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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?

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0