Hi, ive been coding a game, a bit like asteroids, but i have come around a weird problem, i have 2 variables, a AccelerationX and AccelerationY. I have a line of code outside the if statements for the keys and in the loop, but for some reason it only moves when i press a key. I think its because its only changing when the variable changes and i cant find out how to fix it.
What im trying to do is make it so it constantly moves and the Acceleration Variables go up or down as I press up or down, and the character moves constantly with the values of the acceleration.
Here is what i have so far.
Main.cpp
#include <SFML/Graphics.hpp>
#include "iostream"
#include "Main.h"
#include "Ship.h"
#include "Test.h"
#include "Backround.h"
using namespace std; // Allowing the use of std
static float FrameSelection = 60.0f;
bool FrameRateMenu = false;
int Output()
{
cout << FrameSelection << endl;
cin.get();
return 0;
};
int Frames3()
{
sf::Time TickSpeed = sf::seconds(1.0f) / FrameSelection;
cout << TickSpeed.asSeconds() << endl;
Output();
return 0;
};
int Input()
{
while (FrameRateMenu = true);
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
FrameSelection += 30;
Frames3();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if (FrameSelection != 30)
{
FrameSelection -= 30;
Frames3();
}
if (FrameSelection < 30)
{
FrameSelection = 30;
}
}
}
return 0;
};
// - This is For window ( Selecting Window Size and Creating it )
// Variables
// Setting Window Size
int Window::WindowSizeH;
int Window::WindowSizeL;
bool FirstRun = true;
bool DrawCheck = false;
bool ColorAssigned = false;
int RCol = 0;
int GCol = 0;
int BCol = 0;
int ShipRCol;
int ShipGCol;
int ShipBCol;
char Response;
sf::RenderWindow Window::RWindow;
sf::View Window::MainView;
Window::Window()
{
RWindow.create(sf::VideoMode(WindowSizeL,WindowSizeH), "RenderWindow");
MainView.reset(sf::FloatRect(0, 0, WindowSizeL, WindowSizeH));
MainView.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
// This while loop is our main loop
while (RWindow.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (RWindow.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
RWindow.close();
}
// And under here is everything we want in our main loop
RWindow.setView(MainView);
MainView.setCenter(Ship::PlayerPos);
RWindow.clear(sf::Color::Black);
// Draw Checks
Background();
Ship();
RWindow.display();
}
}
int WindowI()
{
int static WindowSizeH = 100;
int static WindowSizeL = 100;
int static WindowSizeLSet = 0;
int static WindowSizeHSet = 0;
WindowSize();
return 0;
}
int WindowSize()
{
cout << "Please Insert Window Size" << endl;
cout << "Insert Window Length" << endl;
cin >> WindowSizeLSet;
cout << "Insert Window Height" << endl;
cin >> WindowSizeHSet;
Window::WindowSizeL = WindowSizeLSet;
Window::WindowSizeH = WindowSizeHSet;
Window();
return 0;
};
// Window Creation
int Debug()
{
cout << "I Work" << endl;
return 0;
}
// - Main
int main()
{
Frames3();
WindowI();
};
Main.h
#pragma once
int WindowSize();
int static WindowSizeLSet;
int static WindowSizeHSet;
class Window
{
public:
Window();
static sf::RenderWindow RWindow;
static sf::View MainView;
int static WindowSizeH;
int static WindowSizeL;
};
Ship.cpp
#include "Ship.h"
#include <SFML/Graphics.hpp>
#include "Main.h"
#include "iostream"
using namespace std;
float Ship::AccelX;
float Ship::AccelY;
float Ship::rotation;
sf::Vector2f Ship::PlayerPos;
sf::CircleShape Ship::PlayerShip;
int check = 0;
int RotationMovement()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
Ship::PlayerShip.rotate(-0.6);
cout << "Left" << endl;
Ship::rotation = Ship::PlayerShip.getRotation();
cout << Ship::rotation << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Ship::PlayerShip.rotate(0.6);
Ship::rotation = Ship::PlayerShip.getRotation();
cout << "Right" << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
Ship::AccelY += cos(Ship::rotation) * 0.6 * -1;
Ship::AccelX += sin(Ship::rotation) * 0.6 * -1;
cout << "Acceleration X" << endl;
cout << Ship::AccelX << endl;
cout << "Acceleration Y" << endl;
cout << Ship::AccelY << endl;
cout << "Up" << endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
Ship::AccelY += cos(Ship::rotation) * 0.6;
Ship::AccelX += sin(Ship::rotation) * 0.6;
cout << "Down" << endl;
}
return 0;
};
int Movement()
{
check += 1;
cout << Ship::AccelX << endl;
cout << Ship::AccelY << endl;
cout << check << endl;
Ship::PlayerShip.setPosition(+Ship::AccelX, +Ship::AccelY);
return 0;
}
Ship::Ship()
{
ShipSize = Window::WindowSizeH / 30;
PlayerShip.setRadius(ShipSize);
PlayerShip.setPointCount(3);
PlayerShip.setOrigin(20, 20);
PlayerShip.setPosition(Window::WindowSizeL / 2, Window::WindowSizeH / 2);
if (ColorAssigned == false)
{
cout << "Choose Your Ships R Color" << endl;
cin >> RCol;
cout << "Choose Your Ships G Color" << endl;
cin >> GCol;
cout << "Choose Your Ships B Color" << endl;
cin >> BCol;
ColorAssigned = true;
}
// Input
Movement();
RotationMovement();
PlayerShip.setOutlineColor(sf::Color(RCol, GCol, BCol));
PlayerShip.setOutlineThickness(2);
PlayerShip.setFillColor(sf::Color(0, 0, 0));
Window::RWindow.draw(PlayerShip);
PlayerPos = PlayerShip.getPosition();
}
Ship.h
#pragma once
#include <SFML/Graphics.hpp>
class Ship
{
public:
Ship();
static sf::Vector2f PlayerPos;
int ShipSize;
static sf::CircleShape PlayerShip;
static float AccelX;
static float AccelY;
static float rotation;
};
bool extern ColorAssigned;
bool extern DrawCheck;
int extern RCol;
int extern GCol;
int extern BCol;
Any help would be appreciated, and also the cos and sin values are just a place holder and once i get the constant movement i should be able to fix that aswell.