차를 제어하고 화면 상단에서 떨어지는 물체를 피하는 간단한 SFML 게임을 만들려고합니다. 프로그램을 실행하면 반응이 없지만 화면을 닫으면 화면에 오류 메시지가 표시되지 않습니다.SFML C++ Dodge Game Unresponsive
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();
}
}
}
};
디버거를 사용하여 코드를 밟았습니까? 매우 순진한 언뜻보기에'while (GameOn == true)'는 결코 사실이 아니지만, 당신이 걸릴 때까지 코드를 단계적으로 진행하면서 문제를 쉽게 발견 할 수 있다고 생각합니다. – George