i cant make the pause menu please help
here the code
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Enemy.h"
#include "Player.h"
#include"Background.h"
using namespace std;
using namespace sf;
class Button {
private:
Texture buttonTexture;
Sprite button;
Text buttonText;
Font buttonFont;
public:
Button() {}
void loadImageButton(String filename) {
if (!buttonTexture.loadFromFile(filename)) {
cout << "Could not load button file";
}
button.setTexture(buttonTexture);
}
//Text related
void setText(String text) {
buttonText.setString(text);
}
void setFont(String fontPath) {
if (!buttonFont.loadFromFile(fontPath)) {
cout << "Could not load font file";
return;
}
buttonText.setFont(buttonFont);
}
void setFontSize(int size) {
buttonText.setCharacterSize(size);
}
Text getText() {
return buttonText;
}
void setTextPosition(int x, int y) {
buttonText.setPosition(x, y);
}
//End
void setTexture(String filename) {
if (!buttonTexture.loadFromFile(filename)) {
cout << "Could not load button file";
}
button.setTexture(buttonTexture);
}
void setPosition(int x, int y) {
button.setPosition(x, y);
}
Sprite getButton() {
return button;
}
void draw(RenderWindow &window) {
window.draw(button);
window.draw(buttonText);
}
bool isClicked(int mouseX, int mouseY) {
return button.getGlobalBounds().contains(mouseX, mouseY);
}
};
enum GameState {
MAINMENU,
MAINGAME,
EXIT,
PAUSE
};
class CGame {
private:
GameState _gameState;
Button _playButton, _exitButton, _resetButton;
Text _pauseText;
SoundBuffer _clickedSoundBuffer;
Sound _clicked;
Music _backgroundMusic;
Font _pauseFont;
Clock clock;
Time time;
public:
CGame() {
if (!_pauseFont.loadFromFile("assets/fonts/kenvector_future.ttf")) {
cout << "Could not load font file";
return;
}
_pauseText.setFont(_pauseFont);
_pauseText.setCharacterSize(72);
_pauseText.setString("PAUSE");
_pauseText.setPosition(270, 100);
_gameState = GameState::MAINMENU;
_playButton.loadImageButton("assets/images/green_button04.png");
_playButton.setFont("assets/fonts/kenvector_future.ttf");
_playButton.setFontSize(48);
_playButton.setPosition(290, 200);
_playButton.setText("Play");
_playButton.setTextPosition(340, 195);
_exitButton.loadImageButton("assets/images/green_button04.png");
_exitButton.setFont("assets/fonts/kenvector_future.ttf");
_exitButton.setFontSize(48);
_exitButton.setPosition(290, 270);
_exitButton.setText("Exit");
_exitButton.setTextPosition(350, 265);
_resetButton.loadImageButton("assets/images/green_button04.png");
_resetButton.setFont("assets/fonts/kenvector_future.ttf");
_resetButton.setFontSize(48);
_resetButton.setPosition(290, 200);
_resetButton.setText("Reset");
_resetButton.setTextPosition(320, 195);
if (!_clickedSoundBuffer.loadFromFile("assets/sounds/click1.ogg")) {
cout << "Could not load clicked sound file" << endl;
}
_clicked.setBuffer(_clickedSoundBuffer);
if (!_backgroundMusic.openFromFile("assets/sounds/Start.wav")) {
cout << "Could not load background music file" << endl;
}
}
//START Game function
void start() {
RenderWindow window(VideoMode(800, 600), "Air Force Strike");
_backgroundMusic.play();
_backgroundMusic.setVolume(30);
while (window.isOpen()) {
if (_gameState == GameState::MAINMENU) {
mainMenu(window);
}
else if (_gameState == GameState::MAINGAME) {
mainGame(window);
}
else if (_gameState == GameState::EXIT) {
window.close();
}
/*else if (_gameState == GameState::PAUSE) {
pause(window);
}*/
}
}
void mainMenu(RenderWindow &window) {
//Event Handling
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
if (event.type == Event::MouseButtonReleased) {
if (_playButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::MAINGAME;
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::EXIT;
break;
}
}
if (event.type == Event::MouseButtonPressed) {
if (_playButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_playButton.loadImageButton("assets/images/green_button03.png");
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_exitButton.loadImageButton("assets/images/green_button03.png");
break;
}
}
}
Texture menuTex;
menuTex.loadFromFile("assets/background/back.png");
//Draw Objects
window.clear();
Background background(&menuTex);
window.draw(background.shape);
_playButton.draw(window);
_exitButton.draw(window);
window.display();
}
void mainGame(RenderWindow& window) {
//Init text
Font font;
font.loadFromFile("assets/fonts/kenvector_future.ttf");
//Init textures
Texture playerTex;
playerTex.loadFromFile("assets/PNG/Plane/Fly (2).png");
Texture enemyTex;
enemyTex.loadFromFile("assets/PNG/Monster/mons1.png");
Texture bulletTex;
bulletTex.loadFromFile("assets/PNG/Bullet/Bullet (1).png");
Texture backTex;
backTex.loadFromFile("assets/background/back.png");
//UI init
Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(20);
scoreText.setFillColor(Color::White);
scoreText.setPosition(10.f, 10.f);
Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(30);
gameOverText.setFillColor(Color::Red);
gameOverText.setPosition(100.f, window.getSize().y / 2);
gameOverText.setString("GAME OVER!");
//Player init
int score = 0;
Player player(&playerTex);
Clock shootTimer;
Text hpText;
hpText.setFont(font);
hpText.setCharacterSize(12);
hpText.setFillColor(Color::Green);
//Enemy init
Clock enemySpawnTimer;
vector<Enemy> enemies;
Text eHpText;
eHpText.setFont(font);
eHpText.setCharacterSize(12);
eHpText.setFillColor(Color::Red);
//Background
Background background(&backTex);
while (window.isOpen())
{
Event event;
time = clock.getElapsedTime();
clock.restart();
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
if (player.HP > 0)
{
//Player
if (Keyboard::isKeyPressed(Keyboard::W))
player.shape.move(0.f, -0.5f * time.asMilliseconds());
if (Keyboard::isKeyPressed(Keyboard::A))
player.shape.move(-0.5f * time.asMilliseconds(), 0.f);
if (Keyboard::isKeyPressed(Keyboard::S))
player.shape.move(0.f, 0.5f * time.asMilliseconds());
if (Keyboard::isKeyPressed(Keyboard::D))
player.shape.move(0.5f * time.asMilliseconds(), 0.f);
hpText.setPosition(player.shape.getPosition().x, player.shape.getPosition().y - hpText.getGlobalBounds().height);
hpText.setString(std::to_string(player.HP) + "/" + std::to_string(player.HPMax));
//Collision with window
if (player.shape.getPosition().x <= 0) //Left
player.shape.setPosition(0.f, player.shape.getPosition().y);
if (player.shape.getPosition().x >= window.getSize().x - player.shape.getGlobalBounds().width) //Right
player.shape.setPosition(window.getSize().x - player.shape.getGlobalBounds().width, player.shape.getPosition().y);
if (player.shape.getPosition().y <= 0) //Top
player.shape.setPosition(player.shape.getPosition().x, 0.f);
if (player.shape.getPosition().y >= window.getSize().y - player.shape.getGlobalBounds().height) //Bottom
player.shape.setPosition(player.shape.getPosition().x, window.getSize().y - player.shape.getGlobalBounds().height);
//Update Controls
if (Mouse::isButtonPressed(Mouse::Left) && shootTimer.getElapsedTime().asSeconds() >= 0.5f) //Shooting
{
player.bullets.push_back(Bullet(&bulletTex, player.shape.getPosition()));
//shootTimer = 0; //reset timer
shootTimer.restart();
}
//Bullets
for (size_t i = 0; i < player.bullets.size(); i++)
{
//Move
player.bullets[i].shape.move(1.f * time.asMilliseconds(), 0.f);
//Out of window bounds
if (player.bullets[i].shape.getPosition().x > window.getSize().x)
{
player.bullets.erase(player.bullets.begin() + i);
break;
}
//Enemy collision
for (size_t k = 0; k < enemies.size(); k++)
{
if (player.bullets[i].shape.getGlobalBounds().intersects(enemies[k].shape.getGlobalBounds()))
{
if (enemies[k].HP <= 1)
{
score += enemies[k].HPMax;
enemies.erase(enemies.begin() + k);
}
else
enemies[k].HP--; //ENEMY TAKE DAMAGE
player.bullets.erase(player.bullets.begin() + i);
break;
}
}
}
//Enemy
//enemy spawn
if (enemySpawnTimer.getElapsedTime().asSeconds() > 0.8f)
{
enemies.push_back(Enemy(&enemyTex, window.getSize()));
//enemySpawnTimer = 0; //reset timer
enemySpawnTimer.restart();
}
for (size_t i = 0; i < enemies.size(); i++)
{
enemies[i].shape.move(-0.3f * time.asMilliseconds(), 0.f);
if (enemies[i].shape.getPosition().x <= 0 - enemies[i].shape.getGlobalBounds().width)
{
enemies.erase(enemies.begin() + i);
break;
}
if (enemies[i].shape.getGlobalBounds().intersects(player.shape.getGlobalBounds()))
{
enemies.erase(enemies.begin() + i);
player.HP--; // PLAYER TAKE DAMAGE
break;
}
}
if (event.type == Event::KeyReleased) {
if (event.key.code == Keyboard::P) {
Texture pauseTex;
pauseTex.loadFromFile("assets/background/back.png");
Background background(&pauseTex);
//Draw Objects
window.clear();
window.draw(background.shape);
window.draw(_pauseText);
_resetButton.draw(window);
_exitButton.draw(window);
window.display();
if (event.type == Event::MouseButtonReleased) {
if (_resetButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::MAINGAME;
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::EXIT;
break;
}
}
if (event.type == Event::MouseButtonPressed) {
if (_resetButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_resetButton.loadImageButton("assets/images/green_button03.png");
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_exitButton.loadImageButton("assets/images/green_button03.png");
break;
}
}
}
/*if (event.key.code == Keyboard::R) {
reset();
}*/
}
//UI Update
scoreText.setString("Score: " + to_string(score));
}
//Draw ===================================================================== DRAW
window.clear();
window.draw(background.shape);
//player
window.draw(player.shape);
//Bullets
for (size_t i = 0; i < player.bullets.size(); i++)
{
window.draw(player.bullets[i].shape);
}
//enemy
for (size_t i = 0; i < enemies.size(); i++)
{
eHpText.setString(std::to_string(enemies[i].HP) + "/" + to_string(enemies[i].HPMax));
eHpText.setPosition(enemies[i].shape.getPosition().x, enemies[i].shape.getPosition().y - eHpText.getGlobalBounds().height);
window.draw(eHpText);
window.draw(enemies[i].shape);
}
//UI
window.draw(scoreText);
window.draw(hpText);
if (player.HP <= 0)
window.draw(gameOverText);
window.display();
}
}
void pause(RenderWindow& window) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
if (event.type == Event::MouseButtonReleased) {
_clicked.play();
}
if (event.type == Event::KeyReleased) {
if (event.key.code == Keyboard::P) {
_gameState = GameState::MAINGAME;
break;
}
}
if (event.type == Event::MouseButtonReleased) {
if (_resetButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::MAINGAME;
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_clicked.play();
_gameState = GameState::EXIT;
break;
}
}
if (event.type == Event::MouseButtonPressed) {
if (_resetButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_resetButton.loadImageButton("assets/images/green_button03.png");
break;
}
if (_exitButton.isClicked(event.mouseButton.x, event.mouseButton.y)) {
_exitButton.loadImageButton("assets/images/green_button03.png");
break;
}
}
}
Texture pauseTex;
pauseTex.loadFromFile("assets/background/back.png");
Background background(&pauseTex);
//Draw Objects
window.clear();
window.draw(background.shape);
window.draw(_pauseText);
_resetButton.draw(window);
_exitButton.draw(window);
window.display();
}
};