Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help on Asteroids Game!  (Read 7120 times)

0 Members and 1 Guest are viewing this topic.

Ultranor

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help on Asteroids Game!
« on: March 20, 2011, 07:44:39 pm »
I just started doing my first Asteroids game as a project for school. I got the asteroids up and running but im now stuck on doing the spaceship. I need it to rotate, move and shoot bullets according to the position the spaceship cannon is rotated. Can anyone help me or provide me with a link to a solution for it? Thanks in advance! Im using c++ and SFML for it.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Help on Asteroids Game!
« Reply #1 on: March 20, 2011, 09:08:18 pm »
You can save the angle of your spaceship (initializing it in the direction you want : up, left, down, right, ...) and use some maths formulas.

You also need :
Sprite::SetRotation()
Sprite::Rotate (perhaps)

To know how to calculate a position having an angle and another position and to calculate an angle having 2 positions.

Ultranor

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help on Asteroids Game!
« Reply #2 on: March 22, 2011, 08:00:26 pm »
Thanks for your help! I managed to get the spaceship up and running but now im stuck on making it shoot bullets. I have been coding and reading for hours and I still can't any decent tutotial for creating and shooting bullets. Any pro tips or any tutorials on doing that?

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Help on Asteroids Game!
« Reply #3 on: March 22, 2011, 08:20:44 pm »
Quote from: "Ultranor"
Thanks for your help! I managed to get the spaceship up and running but now im stuck on making it shoot bullets. I have been coding and reading for hours and I still can't any decent tutotial for creating and shooting bullets. Any pro tips or any tutorials on doing that?


I don't think so. A bullet is just a sprite. If you can shot a lot of bullet, just do a list or a tab or a container to add bullet in it. When the player press the fire key, add a bullet in it (initialized with position, angle and maybe speed) et draw your bullets.

Don't forget to delete your bullets if you're making dynamic allocation !

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Help on Asteroids Game!
« Reply #4 on: March 23, 2011, 11:09:17 am »
Quote from: "Ultranor"
Thanks for your help! I managed to get the spaceship up and running but now im stuck on making it shoot bullets. I have been coding and reading for hours and I still can't any decent tutotial for creating and shooting bullets. Any pro tips or any tutorials on doing that?


There is no "proper" way to do it, every project is different. The simplest way to do it would be to create a Bullet class which you instantiate with the position and direction of your spaceship. Have it stored somewhere (like a vector) and have a manager update the position of the sprite associated to every Bullet object every from.

However things can get much more complicated, depending on what you need. If this looks like what you want to do, then implement that and try, perhaps you'll need to change your data structure, or inherit your Bullet class from a more abstract one, but you can always see that later :)

Gibgezr

  • Newbie
  • *
  • Posts: 33
    • View Profile
Help on Asteroids Game!
« Reply #5 on: April 01, 2011, 05:35:14 pm »
Here is some example code:

Simple shot class:
Code: [Select]

class Shot
{
public:
Vect2d pos; //position in pixels of the shot
Vect2d motion; //direction and speed of the shot
float distance_travelled; //how far the shot has travelled over it's lifetime so far

Shot(void);
};


to collide shots with things, do something like:
Code: [Select]

bool Ship::CheckShotCollision(Asteroid* theAsteroid)
{
for(int i = shots.size() - 1;i >= 0; --i)
{
//did we collide?
if( (shots[i]->pos - theAsteroid->pos).LengthSqr() <= theAsteroid->radius_squared)
{
//we collided with this shot
delete shots[i];
shots.erase(shots.begin() + i);
return true;
}
}

return false; //if we got here, no collision
}


Make a list of shots somewhere (like in a Ship class)
Code: [Select]

float time_since_last_shot;

std::vector<Shot *> shots;


To fire a new shot, do something like:
Code: [Select]

bool Ship::Shoot(void)
{
if(shots.size() >= MAX_SHOTS) return false;
if(time_since_last_shot < TIME_BETWEEN_SHOTS) return false;

//add a new shot
Shot *nshot = new Shot();
//create its motion vector from the ship's facing angle
nshot->motion.x = -sin(angle);
nshot->motion.y = -cos(angle);
nshot->pos = pos + nshot->motion * 16.f; //move the shot to the front of the ship
nshot->motion *= SHOT_SPEED; //scale the motion up to speed
shots.push_back(nshot);

time_since_last_shot = 0.f;
return true;
}


To draw them, do:
Code: [Select]

for(int i = 0; i < theShip.shots.size(); ++i)
{
theShotsSprite->SetPosition(theShip.shots[i]->pos.x, theShip.shots[i]->pos.y);
App->Draw(*theShotsSprite);
}