2017-02-21 55 views
0

我想创建一个简单的SFML游戏,你控制一辆汽车,你必须躲避从屏幕顶部掉下来的物体。当我运行我的程序时,它变得没有反应,但是当我关闭它时,屏幕上没有错误消息。SFML C++道奇游戏无反应

main.cpp中:

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include "Paddle.h" 
#include "Obstacle.h" 
using namespace sf; 
using namespace std; 

int main() 
{ 
RenderWindow gameDisplay(VideoMode(1336, 900), "Dodge"); 

bool GameOn = true; 

Texture player; 
if (!player.loadFromFile("SFML_game_car.png")) 
{ 

} 

Sprite playercar; 
playercar.setTexture(player); 
playercar.setPosition(200,700); 
playercar.setRotation(270); 

Texture obstacle1; 
if (!obstacle1.loadFromFile("obstacle.jpg")) 
{ 
    //error 
} 

Sprite firstwall; 
firstwall.setTexture(obstacle1); 


Obstacle1 obstacle1object; 
obstacle1object.obstacle1_spawn(firstwall, GameOn, gameDisplay); 



while (gameDisplay.isOpen()) 
{ 
    Event evnt; 
    while (gameDisplay.pollEvent(evnt)) 
    { 
     switch (evnt.type) 
     { 
     case Event::Closed: 
      gameDisplay.close(); 
     case Event::KeyPressed: 
      if (Keyboard::isKeyPressed(Keyboard::Q)) 
       gameDisplay.close(); 

     } 
    } 


    while (GameOn == true) 
    { 
     if (Keyboard::isKeyPressed(Keyboard::Right)) 
     { 
      Paddle paddlerightobject; 
      paddlerightobject.paddle_move_right(playercar); 
     } 

     if (Keyboard::isKeyPressed(Keyboard::Left)) 
     { 
      Paddle paddleleftobject; 
      paddleleftobject.paddle_move_left(playercar); 
     } 



     gameDisplay.clear(); 
     gameDisplay.draw(playercar); 
     gameDisplay.draw(firstwall); 
     gameDisplay.display(); 

    } 


} 

return 0; 
} 

Paddle.h

#include <SFML/Graphics.hpp> 
#include <iostream> 
using namespace sf; 
using namespace std; 

#pragma once 

class Paddle 
    { 
    public: 
    void paddle_move_right(Sprite &playercar) 
{ 
    if (playercar.getPosition().x < 1150) 
     playercar.move(0.3f, 0.0f); 
} 

void paddle_move_left(Sprite &playercar) 
{ 
    if (playercar.getPosition().x > 100) 
     playercar.move(-0.3f, 0.0f); 
    } 
}; 

Obstacle.h

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include <cstdlib> 
#include <stdlib.h> 
#include <ctime> 
using namespace sf; 
using namespace std; 
int RandPosx; 
#pragma once 

class Obstacle1 
{ 
public: 
void obstacle1_spawn(Sprite &firstwall, bool &GameOn, RenderWindow &gameDisplay) 
{ 
    srand(time(0)); 
    RandPosx = (rand() % 300)+ 1; 
    cout << RandPosx << endl; 
    cout << firstwall.getPosition().y << endl; 
    firstwall.setPosition(300, 100); 

    while (firstwall.getPosition().y < 1000) 
    { 
     firstwall.move(0.0f, 0.5f); 
     if (firstwall.getPosition().y > 1000) 
     { 
      GameOn = false; 
      gameDisplay.close(); 
     } 
    } 



    } 
}; 
+1

你是否使用调试器完成了代码?在一个非常天真的第一眼看来,它看起来像'while(GameOn == true)'从来都不是真的,但我怀疑你会很容易地发现问题跨越代码,直到你到达你挂起。 – George

回答

0

应用程序将不会进入while (gameDisplay.pollEvent(evnt),因为这将是当游戏开始跳过,并且因为while (GameOn == true)内部没有设置GameOnfalse,应用程序被有效地卡在这个循环中。因此,没有事件可以轮询,导致无响应。

+0

好的,谢谢我会尽力修复它:) –

+0

@JosephH我建议你尝试在乔布斯的评论中建议的调试模式下通过你的程序的步骤! –