benny2010 0 Newbie Poster

so heres the problem, i have been working on this asteroids game for a week or so now but i cannot for the life of me figure out how to work the direction properly. at the moment the only thing the ship does is move up the screen.

The functions needing to be sorted are the get input and update functions, ive given the whole code so you can get a clearer view of the set positions and velocities if you need them

If anyone could help with working out how to implement the directional capabilities of the ship i would be forever grateful....

anyway heres the code:

#include "wrapGame.h"
using namespace std;

WrapGame::WrapGame(sf::RenderWindow& app) : Game(app)
{
	
}

float WrapGame::getMoveIncrement()
{
	return moveIncrement;
}

void WrapGame::setMoveIncrement(float f)
{
	moveIncrement=f;
}

void WrapGame::initL1()
{
	gameState=intro;
	ballImage.LoadFromFile("myC++/images/asteroidBig.png");
	ballImage.CreateMaskFromColor(sf::Color(255,255,255,255));
	ballSprite=sf::Sprite(ballImage);
	ballSprite.SetScale(0.6, 0.6);
	ballSprite.SetCenter(ballSprite.GetSize().x/2,ballSprite.GetSize().y/2);

	//the ship is not sorted out yet
	shipImage.LoadFromFile("myC++/images/ship.png");
	shipSprite=sf::Sprite(shipImage);
	shipSprite.SetScale(0.8, 0.8);
	shipSprite.SetCenter(shipSprite.GetSize().x/2,shipSprite.GetSize().y/2);

	//sets the inital position and velocity of the ship
	s.setRadius(shipSprite.GetSize().x/2);
	s.setPosX(width/2);
	s.setPosY(height/2);
	s.setVelX(0);
	s.setVelY(-2);

	for(int i=0; i<10; i++)
	{
		b[i].setRadius(ballSprite.GetSize().x/2);
		b[i].setPosX(width);
		b[i].setPosY(rand()%height+b[i].getRadius());
		b[i].setVelX(rand()%60);
		b[i].setVelY(rand()%40);
	}		
}

//void WrapGame::initL2()
//{
//
//}

void WrapGame::draw()
{
	//draw backgound colour
	win.Clear(sf::Color(255,255,0));
	//set the ball sprite to where the ball is
	for(int i=0; i<10; i++)
	{
		ballSprite.SetPosition(b[i].getPosX(),b[i].getPosY());
		shipSprite.SetPosition(s.getPosX(), s.getPosY());
		//draw the sprite
		win.Draw(ballSprite);
		win.Draw(shipSprite);
	}
}

//void WrapGame::drawL2()
//{
//
//}

void WrapGame::getInput()
{
	sf::Event Event;
        while (win.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                win.Close();
            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                win.Close();

			//right arrow key : move ball right by passing the info that we want to move right to the update method
			if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
			{
				currentKey=sf::Key::Right;
				shipSprite.Rotate(-5);//need to fix the directional velocities
			}
			//left arrow: move left
			if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
			{
				currentKey=sf::Key::Left;
				shipSprite.Rotate(5);
				//fix the direction in which the ship travels
			}
			//up arrow : move up  you to do
			//if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
			//	currentKey=sf::Key::Down;

			//iF the up key is pressed the velocity is increased
			//need to fix the direction of the ship
			if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
			{
				currentKey=sf::Key::Up;
				
				s.setVelY(s.getVelY()*1.2);
				if(s.getVelY()<-200) //the velocity is limited to -200
				{
					s.setVelY(-200);
				}
			}

			//if the up key is released a new key state is initialised
			if((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == sf::Key::Up))
				currentKey=sf::Key::Down;				

			//if the gamestate is on the intro screen and p is pressed change to level1
			if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::P) && gameState==intro)
				gameState=level1;
				
        }
}

void WrapGame::update()
{
	double result, result2, rads, xcoord, ycoord;
	cout<<" "<<s.getVelY()<<"\r";
	//no user input - no need to  process user

	//update based on velocity of the ball
	float elapsedTime=Clock.GetElapsedTime();
	if(elapsedTime>1.0F/30.0F)//30 FPS
	{
		//at the moment this sets the ship up the screen on an increased velocity, need to work on setting it to increase steadily
		if (currentKey=sf::Key::Up)
		{
			s.setPosY(s.getPosY()+elapsedTime*s.getVelY());
			s.setPosX(s.getPosX()+elapsedTime*s.getVelX());
		}

		if(currentKey=sf::Key::Down)
		{
			s.setVelY(s.getVelY()*0.95);
			if(s.getVelY()!=-0.5)
			{
				s.setVelY(s.getVelY()-0.5);
				//s.setPosY(s.getPosY()+elapsedTime*s.getVelY());
			}
		}

		//the asteroid wrapping loop
		for(int i=0; i<11; i++)
		{
			b[i].setPosX(b[i].getPosX()+elapsedTime*b[i].getVelX());
			b[i].setPosY(b[i].getPosY()+elapsedTime*b[i].getVelY());

			//the screen wrap stuff
			Clock.Reset();
			
			//the asteroid wrap stuff
			if(b[i].getPosX()<0-b[i].getRadius()) {b[i].setPosX(width+b[i].getRadius());}
			else if(b[i].getPosX()>width+b[i].getRadius()) {b[i].setPosX(0-b[i].getRadius());}
			else if(b[i].getPosY()<0-b[i].getRadius()) {b[i].setPosY(height+b[i].getRadius());}
			else if(b[i].getPosY()>height+b[i].getRadius()) {b[i].setPosY(0-b[i].getRadius());}

			//collision detection
			//formula = d <= radius1+radius2
			//distance between points = square root of ((x2-x1)squared+(y2-y1)squared)
			xcoord=(b[i].getPosX()-s.getPosX()); //(x2-x1)
			ycoord=(b[i].getPosY()-s.getPosY()); //(y2-y1)
			result=(xcoord*xcoord)+(ycoord*ycoord); //X and Y calculations are squared
			result2 = sqrt (result); //the result is square rooted
			rads=(b[i].getRadius()+s.getRadius()); //the sum of both radius's is calculated

			if(result2<rads) //if the distance between the two points is less than the sum of the radius's
			{
				cout<<"HIT"<<endl;
			}
		}

		//the ship wrapping stuff
		s.setPosX(s.getPosX()+elapsedTime*s.getVelX());
		s.setPosY(s.getPosY()+elapsedTime*s.getVelY());

		if(s.getPosX()<0-s.getRadius()) {s.setPosX(width+s.getRadius());}
		else if(s.getPosX()>width+s.getRadius()) {s.setPosX(0-s.getRadius());}
		else if(s.getPosY()<0-s.getRadius()) {s.setPosY(height+s.getRadius());}
		else if(s.getPosY()>height+s.getRadius()) {s.setPosY(0-s.getRadius());}
	}	
}

void WrapGame::play()
{
	//welcome text
	string s("Welcome to Asteroids : Instruction screen goes here");
	sf::String Text1(s,sf::Font::GetDefaultFont(),20);
	Text1.SetCenter(Text1.GetRect().GetWidth()/2,Text1.GetRect().GetHeight()/2);
	Text1.SetColor(sf::Color(267, 155, 12));
	Text1.SetPosition(width/2, height/2-50);
	
	sf::String Text2("Press P to play",sf::Font::GetDefaultFont(),20);
	Text2.SetColor(sf::Color(255, 0, 0));
	Text2.SetCenter(Text2.GetRect().GetWidth()/2,Text2.GetRect().GetHeight()/2);
	Text2.SetPosition(width/2,height/2+25);

	switch(gameState) //determines the 
	{
	case intro:
		{
			win.Draw(Text1);
			win.Draw(Text2);
			getInput();
			break;
		}
	case level1:
		{
			//the game loop
			draw();//the offscreen buffer
			getInput();//from the user
			update();//integrate user input and any game movement which is automatic
			break;
		}
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.