暂停和主屏幕不同

时间:2018-07-31 02:04:32

标签: enums coding-style sfml

我是一位经验不足的编码员,如果有人可以为我解决这个问题,那就太好了。

library(plotly)
ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "right",
  title = "second y axis"
)
p <- plot_ly() %>%
  add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
  add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
  layout(
    title = "Double Y Axis", yaxis2 = ay,
    xaxis = list(title="x")
  )

}

ATM,我正在尝试制作它,因此起始屏幕与暂停屏幕不同。在开始屏幕上,我看到了欢迎和地球图片。在暂停的屏幕上,我希望它具有暂停符号的纹理。我知道这与这一行有关

#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();
 }

但是,如果有人可以将其发送回固定的版本,以及您为修复该错误所做的工作,那就太好了。

1 个答案:

答案 0 :(得分:0)

我共同编写了一个简短的程序,向您展示如何使用枚举。启动应用程序时,它处于状态菜单中,并显示一个圆形和文本“菜单”。通常,您会从菜单状态转到游戏状态。但是在我的小应用程序中,您需要按Enter键,它从GAMEPLAY变为PAUSED并显示文本。我想您知道这个主意,希望对您进一步完善解决方案有帮助。

#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;
}