Enums 不同的暂停和主屏幕

Enums 不同的暂停和主屏幕,enums,coding-style,sfml,Enums,Coding Style,Sfml,我是一个非常缺乏经验的程序员,如果有人能帮我解决这个问题,那就太好了 #include "SFML\Graphics.hpp" #include "main.h" #include <sstream> #include "asteroid.h" enum gameState { MENU, GAMEPLAY, PAUSED }; HRGN hRgn; int main() { sf::RenderWindow window(sf::VideoMod

我是一个非常缺乏经验的程序员,如果有人能帮我解决这个问题,那就太好了

#include "SFML\Graphics.hpp"
#include "main.h"
#include <sstream>
#include "asteroid.h"

enum gameState {
    MENU,
    GAMEPLAY,
    PAUSED
};
HRGN hRgn;
int main()
{
    sf::RenderWindow 
window(sf::VideoMode(1930, 1080), "Crossy 
Space");

sf::Event event;
sf::CircleShape circle;
circle.setRadius(200);
circle.setPosition(100, 50);
circle.setFillColor(sf::Color::Green);

sf::CircleShape lefteye;
lefteye.setPointCount(3);
lefteye.setRadius(50);
lefteye.setFillColor(sf::Color::Black);
lefteye.setPosition(150, 120);

sf::CircleShape righteye;
righteye.setPointCount(3);
righteye.setRadius(50);
righteye.setPosition(340, 120);
righteye.setFillColor(sf::Color::Black);

sf::ConvexShape mouth;
mouth.setFillColor(sf::Color::Black);
mouth.setPointCount(5);
mouth.setPoint(0, sf::Vector2f(150, 300));
mouth.setPoint(1, sf::Vector2f(450, 300));
mouth.setPoint(2, sf::Vector2f(400, 350));
mouth.setPoint(3, sf::Vector2f(300, 375));
mouth.setPoint(4, sf::Vector2f(200, 350));

sf::Texture tex1;
tex1.loadFromFile("images/player_ship.png");
sf::Sprite pship;
pship.setTexture(tex1);
pship.setPosition(960, 900);

const int asteroids = 14;
asteroid a[asteroids] = { asteroid(1920, 800), asteroid(1920, 800), asteroid(1920, 650), asteroid(1920, 650), asteroid(1920, 500),asteroid(1920, 500), asteroid(1920, 350), asteroid(1920, 350), asteroid(1920, 200),asteroid(1920, 200),asteroid(1920, 50),asteroid(1920, 50), asteroid(1920, -100), asteroid(1920, -100) };

sf::Texture tex;
tex.loadFromFile("images/small_asteroid.png");

sf::Sprite spr[asteroids];

for (int i = 0; i < asteroids; i++)
{
    spr[i].setTexture(tex);
    spr[i].setPosition(a[i].x, a[i].y);
}


sf::Texture tex3;
tex3.loadFromFile("images/1080.jpg");
sf::Sprite space;
space.setPosition(0, 0);
space.setTexture(tex3);

sf::Texture tex4;
tex4.loadFromFile("images/vertical.jpg");
sf::Sprite lLine;
lLine.setPosition(-320, 0);
lLine.setTexture(tex4);

sf::Texture lTex;
lTex.loadFromFile("images/level-logo.png");
sf::Sprite fLevel;
fLevel.setPosition(850, 450);
fLevel.setTexture(lTex);

sf::Texture textu;
textu.loadFromFile("images/moon.jpg");
sf::Sprite black;
black.setPosition(0, 0);
black.setTexture(textu);

sf::Texture textur;
textur.loadFromFile("images/pause.png");
sf::Sprite pause;
pause.setPosition(100, 100);
pause.setTexture(textur);


int score = 0;


sf::Font candal;
candal.loadFromFile("Candal.ttf");
sf::Text text;


std::ostringstream ssScore;

ssScore << "Score: " << score;
text.setFont(candal);
text.setString(ssScore.str());
text.setCharacterSize(24);
text.setFillColor(sf::Color::White);
text.setPosition(0, 0);



std::ostringstream ssScore2;
sf::Font candal1;
candal1.loadFromFile("Candal.ttf");
sf::Text text1;
text1.setFont(candal);
text1.setString(ssScore2.str());
text1.setCharacterSize(80);
text1.setFillColor(sf::Color::White);
text1.setPosition(870, 700);



std::ostringstream sLevel;
sf::Font font;
font.loadFromFile("Candal.ttf");
sf::Text text3;
text3.setFont(font);
text3.setString(sLevel.str());
text3.setCharacterSize(110);
text3.setFillColor(sf::Color::Black);
text3.setPosition(1230, 400);


sf::Text myText;

sf::Font myFont;
myFont.loadFromFile("BLACK.ttf");

myText.setFont(myFont);
myText.setString("WELCOME TO CROSSY SPACE");
myText.setCharacterSize(100);
myText.setPosition(200, 400);
//myText.setColor(sf::Color::White);

/*asteroid(1920, 800), asteroid(1920, 800),
asteroid(1920, 650), asteroid(1920, 650),
asteroid(1920, 500), asteroid(1920, 500),
asteroid(1920, 350), asteroid(1920, 350),
asteroid(1920, 200), asteroid(1920, 200),
asteroid(1920, 50), asteroid(1920, 50),
asteroid(1920, -100), asteroid(1920, -100)*/



sf::RectangleShape laneOne;
laneOne.setSize(sf::Vector2f(2000, 4));
laneOne.setPosition(0, 80);
laneOne.setFillColor(sf::Color::White);




sf::RectangleShape laneTwo;
laneTwo.setSize(sf::Vector2f(2000, 4));
laneTwo.setPosition(0, 230);
laneTwo.setFillColor(sf::Color::White);



sf::RectangleShape laneThree;
laneThree.setSize(sf::Vector2f(2000, 4));
laneThree.setPosition(0, 380);
laneThree.setFillColor(sf::Color::White);



sf::RectangleShape laneFour;
laneFour.setSize(sf::Vector2f(2000, 4));
laneFour.setPosition(0, 530);
laneFour.setFillColor(sf::Color::White);




sf::RectangleShape laneFive;
laneFive.setSize(sf::Vector2f(2000, 4));
laneFive.setPosition(0, 680);
laneFive.setFillColor(sf::Color::White);



sf::RectangleShape laneSix;
laneSix.setSize(sf::Vector2f(2000, 4));
laneSix.setPosition(0, 830);
laneSix.setFillColor(sf::Color::White);

/*window.draw(black);
window.draw(myText);*/

int level = 1;
sLevel << level;
int speed = -6;
sf::Clock clock;
int passNum = 0;
bool playing = true;

gameState currentState = MENU;
while (window.isOpen()) {
    string scoreString = getScore();
    int highScore = stoi(scoreString);
    if (highScore < score) {
        saveScore(score);
    }
    if (pship.getPosition().y < 50) {
        level += 1;
        speed -= 4;
        for (int i = 0; i < asteroids; i++)
        {
            a[i].reset(speed);
            pship.setPosition(960, 900);
        }
        passNum = 0;
    }
    for (int i = 0; i < asteroids; i++) {
        if (spr[i].getGlobalBounds().intersects(lLine.getGlobalBounds()))
        {
            spr[i].setPosition(a[i].x, a[i].y);
        }
    }
    if (currentState == GAMEPLAY) {
        const float moveSpeed = 8;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
            pship.move({ 0, -moveSpeed });
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
            pship.move({ moveSpeed, 0 });
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
            pship.move({ -moveSpeed, 0 });
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
            pship.move({ 0, moveSpeed });
        }
    }
    /*window.draw(black);
    window.draw(myText);*/
    //Event Loop:
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            window.close();
        }
        if (event.type == sf::Event::KeyPressed) {
        }
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
        if (currentState == MENU) {
            currentState = GAMEPLAY;
        }
        else {
            currentState = MENU;
        }
    }

    for (int i = 0; i < asteroids; i++) {
        if (pship.getGlobalBounds().intersects(spr[i].getGlobalBounds()))
        {
            score = 0;
            level = 1;
            speed = -6;
            spr[i].setPosition(a[i].x, a[i].y);
            pship.setPosition(960, 900);
            passNum = 0;
        }
    }

    //logic
    if (currentState == GAMEPLAY) {
        for (int i = 0; i < asteroids; i++) {
            a[i].move();
            if (a[i].x < 0) {
                a[i].reset(speed);
            }
            spr[i].setPosition(a[i].x, a[i].y);
        }
    }

    for (int i = 0; i < asteroids; i++) {
        if (pship.getPosition().y < a[passNum].y)
        {
            score += 5;
            passNum = (passNum + 2) % asteroids;
        }
    }

    //render objects.
    if (currentState == MENU) {
        window.draw(black);
        window.draw(myText);
    }
    if (currentState == PAUSED) {
        window.draw(pause);
    }
    if (currentState == GAMEPLAY) {
        text3.setString(sLevel.str());
        sLevel.str("");
        sLevel << level;

        ssScore.str("");
        ssScore << "Score: " << score << endl;
        ssScore << "Highscore: " << scoreString;
        text.setString(ssScore.str());
        window.draw(space);
        window.draw(lLine);
        window.draw(text);
        window.draw(text1);
        window.draw(text3);

        window.draw(laneOne);

        window.draw(laneTwo);

        window.draw(laneThree);

        window.draw(laneFour);

        window.draw(laneFive);

        window.draw(laneSix);

        window.draw(fLevel);
        window.draw(pship);
        for (int i = 0; i < asteroids; i++) {
            window.draw(spr[i]);
        }
    }
    window.display();
    window.clear();


    //slows down asteroids.
    sf::Time curTime = clock.getElapsedTime();
    while (curTime.asMicroseconds() < 16667) {
        curTime = clock.getElapsedTime();
    }
    clock.restart();
 }

但是,如果有人能将这个问题修复并将其发送回去,那就太好了,谢谢。

我编写了一个简短的程序,向您展示如何使用枚举。启动应用程序时,它处于状态菜单中,并显示圆形和文本“菜单”。通常你会从菜单状态进入游戏状态。但在我的小应用程序中,你需要点击回车键,它会从游戏状态变为暂停状态,并显示文本。我想你明白了,我希望这有助于你进一步阐述解决方案

#include <SFML\Graphics.hpp>
#include <sstream>

enum gameState {
    MENU,
    GAMEPLAY,
    PAUSED
};

int main(){
  sf::RenderWindow window(sf::VideoMode(800, 600), "Crossy Space");

  sf::Font font;
  font.loadFromFile("arial.ttf");

  // Start with the GAME_OVER
  gameState gameState = gameState::MENU;

  while (window.isOpen()){
    // check all the window's events that were triggered since the last iteration of the loop
    sf::Event event;
    while (window.pollEvent(event)){
      // "close requested" event: we close the window
      if (event.type == sf::Event::Closed)
          window.close();
      if (event.type == sf::Event::KeyPressed){
        // Pause a game while playing
        if (event.key.code == sf::Keyboard::P && gameState == gameState::MENU){
          gameState = gameState::PAUSED;
        }
      }
    }

    if (gameState == gameState::MENU){
      window.clear();
      sf::Text text("Menu", font);
      text.move(100.f, 200.f);
      window.draw(text);
      sf::CircleShape shape(50);
      shape.setFillColor(sf::Color(100, 250, 50));
      window.draw(shape);
    } else if (gameState == gameState::PAUSED){
      window.clear();
      sf::Text text("Pause", font);
      text.move(100.f, 200.f);
      window.draw(text);
    }
    window.display();
  }
  return 0;
}
#包括
#包括
枚举配子状态{
菜单,
游戏性,
停顿
};
int main(){
渲染窗口(sf::视频模式(800600),“交叉空间”);
sf::字体;
loadFromFile(“arial.ttf”);
//从游戏结束开始
gameState gameState=gameState::菜单;
while(window.isOpen()){
//检查自上次循环迭代以来触发的所有窗口事件
sf::事件;
while(window.pollEvent(事件)){
//“请求关闭”事件:我们关闭窗口
如果(event.type==sf::event::Closed)
window.close();
if(event.type==sf::event::KeyPressed){
//玩游戏时暂停游戏
if(event.key.code==sf::Keyboard::P&&gameState==gameState::MENU){
游戏状态=游戏状态::暂停;
}
}
}
如果(游戏状态==游戏状态::菜单){
window.clear();
sf::文本(“菜单”,字体);
移动文本(100.f,200.f);
窗口。绘制(文本);
sf::圆形(50);
setFillColor(sf::Color(100250,50));
窗口。绘制(形状);
}否则如果(游戏状态==游戏状态::暂停){
window.clear();
sf::文本文本(“暂停”,字体);
移动文本(100.f,200.f);
窗口。绘制(文本);
}
window.display();
}
返回0;
}
#include <SFML\Graphics.hpp>
#include <sstream>

enum gameState {
    MENU,
    GAMEPLAY,
    PAUSED
};

int main(){
  sf::RenderWindow window(sf::VideoMode(800, 600), "Crossy Space");

  sf::Font font;
  font.loadFromFile("arial.ttf");

  // Start with the GAME_OVER
  gameState gameState = gameState::MENU;

  while (window.isOpen()){
    // check all the window's events that were triggered since the last iteration of the loop
    sf::Event event;
    while (window.pollEvent(event)){
      // "close requested" event: we close the window
      if (event.type == sf::Event::Closed)
          window.close();
      if (event.type == sf::Event::KeyPressed){
        // Pause a game while playing
        if (event.key.code == sf::Keyboard::P && gameState == gameState::MENU){
          gameState = gameState::PAUSED;
        }
      }
    }

    if (gameState == gameState::MENU){
      window.clear();
      sf::Text text("Menu", font);
      text.move(100.f, 200.f);
      window.draw(text);
      sf::CircleShape shape(50);
      shape.setFillColor(sf::Color(100, 250, 50));
      window.draw(shape);
    } else if (gameState == gameState::PAUSED){
      window.clear();
      sf::Text text("Pause", font);
      text.move(100.f, 200.f);
      window.draw(text);
    }
    window.display();
  }
  return 0;
}