Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Lickatoad

Member Since 14 Feb 2013
Offline Last Active Private

Topics I've Started

Zelda style boomerang

21 February 2013 - 06:31 PM

Hi, i'm making a zelda like game and was trying to implement a boomerang where u shoot it, it goes a set distance and then returns to the user. I can shoot the bullet out ok but just not sure on how to implement the above.
 
Any suggestions would be great. Thanks

Allegro Shooting Projectile help

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


 

 

 


PARTNERS