Projectiles

Started by
3 comments, last by Antonym 15 years, 4 months ago
I am trying to create a 2d asteroids-like game with c++ in directx and was wondering how do you keep track of the projectiles? I can think of a method of two but I'd like to know if there is an appropriate/good way of doing it?
Advertisement
#include <list>...std::list <YourProjectileClass> projectiles;
Containers are a new thing for me. I went through the page you linked and didn't wholly understood it. I have a couple of questions. How can you acces and/or change a particular element of the list? Can you make a list of structures? Can you pass this/a list/container as a parameter to a function?
Hey

I would probably go with using vectors.
I don't know if it's the best solution or the worst but i know it works, so i will try to explain it as best i can [rolleyes]

#include <vector>#include <iostream>using namespace std;vector<int> My_Vector;int Counter;int main(){	while(true)	{		//This will add a new int to the vector		My_Vector.push_back(Counter);		for(int a = 0; a<My_Vector.size();a++)		{			//The vector can then be accessed similarly to an array			//You can also access it by using My_Vector.at(index) 			//I'm not clear on the difference			cout<<My_Vector[a];		};		cout<<"\n";		Counter++;		system("Pause");	};};


So the above example shows how to use a vector it's not particularly useful but it's simple, Basically we declare a vector on this line
'std::vector<type> name'
and then on this line add a new object with the .push_back(object) function.
'My_Vector.push_back(Counter);'

i see no reason why a structure would not work as the type, but i've never tried too so i may be wrong, i know that Classes work and will show how to use them with vectors in the next example.

#include <vector>#include <iostream>using namespace std;void Add_Animal();class Animal{public:	void	eat();	void	sleep();	double	weight;	double	height;};vector<Animal> My_Vector;int main(){	while(true)	{		Add_Animal();		Add_Animal();		for(int a = 0;a<My_Vector.size();a++)		{			My_Vector[a].eat();			My_Vector[a].weight = My_Vector[a].weight + 1;		};		system("Pause");	};};void Add_Animal(){	Animal Temp;	Temp.height = 100;	Temp.weight = 100;	My_Vector.push_back(Temp);};void Animal::eat(){	cout<<"\n"<<"Animal Eating!";};void Animal::sleep(){	cout<<"\n"<<"Animal Sleeping!";};


Ofcourse you'll probably also like to know how to delete an object, this next example shows how to using basically the same code as above.
Note : the vector system will automatically move the other entries back if you delete an entry in the middle.

#include <vector>#include <iostream>using namespace std;void Add_Animal();void Delete_Animal(int index);class Animal{public:	void	eat();	void	sleep();	double	weight;	double	height;};vector<Animal> My_Vector;int main(){	while(true)	{		Add_Animal();		Add_Animal();		for(int a = 0;a<My_Vector.size();a++)		{			My_Vector[a].eat();			My_Vector[a].weight = My_Vector[a].weight + 1;		};		Delete_Animal(1);		system("Pause");	};};void Add_Animal(){	Animal Temp;	Temp.height = 100;	Temp.weight = 100;	My_Vector.push_back(Temp);};void Delete_Animal(int index){	My_Vector.erase(My_Vector.begin() + index);};void Animal::eat(){	cout<<"\n"<<"Animal Eating!";};void Animal::sleep(){	cout<<"\n"<<"Animal Sleeping!";};


Ok i hope this helped you and for your other question yes vectors can be passed as a parameter to a function if you need an example just ask, if you have any other questions i'll be happy to answer them aswell.

and if you need some more info Here

PS : i've probably made alot of mistakes in my code as i haven't been programming for that long.


Thanks a lot ^_^. Something I'd like to know though is, if you have a container of say structures and you wanted to change the value of one of the members of one of the structures contained how would that be done? I can think of a couple of ways but they all seem so messy.

Basicly what I have in mind is every time the ship shoots, a new missile 'object' is created and pushed back in the container. Then on the main loop using a for loop I would go through all objects in the container and update their position(which is a member of the object).

Edit: Nevermind got it to work although I still think it's unconventional
[source ="c++"]#include "global.h"void initGame(list<object>* gameObjects){	//ship	object obj;	obj.inc1 = 0.2f; 	obj.max1 = 10; 	obj.inc2 = 10; 	obj.max2 = 0.2f; 	obj.cooldown = 170; 	gameObjects->push_front(obj);	return;}void updateObjects(list<object>* gameObjects, input* inputData){		list<object>::iterator it;	for(it = gameObjects->begin(); it != gameObjects->end(); it++){		//Movement Speed		if(inputData->move_forward){			if(it->speed1 <= it->max1){				it->speed1 += it->inc1;			}		}		if(inputData->move_backward){			if(it->speed1 >= -(it->max1)){				it->speed1 -= it->inc1;			}		}		//Rotation Speed		if(inputData->turn_right){			if(it->speed2 <= it->max2){				it->speed2 += it->inc2;			}		}		if(inputData->turn_left){			if(it->speed2 >= -(it->max2)){				it->speed2 -= it->inc2;			}		}		//Movement		it->pos.x -= it->speed1 * sin(it->angle1 * DegtoRad);		it->pos.y -= it->speed1 * cos(it->angle1 * DegtoRad);		//Direction		it->angle1 -= it->speed2;		if(it->angle1 < 0) it->angle1 += 360;		if(it->angle1 > 360) it->angle1 -= 360;		//Rotation		it->angle2 += it->speed2;		if(it->angle2 < 0) it->angle2 += 360;		if(it->angle2 > 360) it->angle2 -= 360;		//Screen Edge Reached		if(it->pos.x < 0){			it->pos.x += SCREEN_WIDTH;		}		if(it->pos.x > SCREEN_WIDTH){			it->pos.x -= SCREEN_WIDTH;		}		if(it->pos.y < 0){			it->pos.y += SCREEN_HEIGHT;		}		if(it->pos.y > SCREEN_HEIGHT){			it->pos.y -= SCREEN_HEIGHT;		}				//Attack		DWORD timeNow = timeGetTime();		if(inputData->shooting == true){			if((timeNow - it->lastshoot) > it->cooldown){				object obj;				obj.pos = it->pos;				obj.angle1 = it->angle1;				obj.angle2 = it->angle2;				obj.speed1 = 10;							gameObjects->push_back(obj);				it->lastshoot = timeGetTime();			}		}	}	return;}


[Edited by - Antonym on December 12, 2008 10:17:13 AM]

This topic is closed to new replies.

Advertisement