我正在尝试创建一个简单的SFML游戏,您可以在其中控制汽车,并且您必须躲避从屏幕顶部掉落的物体。当我运行我的程序时它变得没有响应但是当我关闭它时屏幕上没有错误消息。
main.cpp中:
for(int temp=0;temp<NUMBER_OF_KEYS;temp++)
{
g_hash_table_insert(hashtable, key, 255);
}
Paddle.h
#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;
}
Obstacle.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);
}
};
答案 0 :(得分:0)
应用程序永远不会输入while (gameDisplay.pollEvent(evnt)
,因为在游戏开始时会跳过此项,并且由于while (GameOn == true)
内部没有任何内容将GameOn
设置为false
,因此应用程序被有效地卡在这个循环中。因此,不能轮询任何事件,导致无响应。