提问人:Dxrk_ 提问时间:10/31/2023 最后编辑:Dxrk_ 更新时间:10/31/2023 访问量:39
SFML、C++、Makefile - 无效指令
SFML, C++, Makefile - Invalid instruction
问:
我正在使用 SFML 用 C++ 编写一个 Arkanoid。事实是,当游戏结束时,游戏的状态会从游戏的状态变为残局。游戏状态发生变化,在控制台上输入游戏记录,但不是几毫秒,并输入以下内容:make: *** [Makefile:19: all] 无效指令(已创建内存转储)。
若要运行代码,可以使用以下命令:make
操作系统 : Archlinux
Pkg 版本:make 4.4.1-2、sfml 2.6.0-3
游戏.cpp :
#include "../include/Game.hpp"
Game::Game()
{
initWindow();
initFont();
currentState = std::make_unique<GameState>(window, font);
}
void Game::initWindow()
{
window.create(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Arkanoid");
window.setFramerateLimit(60);
}
void Game::initFont()
{
if (!font.loadFromFile("fonts/mont_extralightdemo.ttf"))
exit(0);
}
void Game::changeState(std::unique_ptr<State> newState)
{
currentState = std::move(newState);
}
void Game::handleInput()
{
currentState->handleInput();
}
void Game::update()
{
currentState->update();
if (currentState->isGameOver()) {
int score = currentState->getScore();
std::unique_ptr<State> newState = std::make_unique<EndGameState>(window, font, score);
changeState(std::move(newState));
}
}
void Game::render()
{
window.clear();
currentState->render(window);
window.display();
}
void Game::run()
{
while (window.isOpen()) {
handleInput();
update();
render();
}
}
游戏状态.cpp :
#include "../include/GameState.hpp"
GameState::GameState(RenderWindow& windowGame, Font& fontGame)
: window(windowGame)
, font(fontGame)
, ball()
, board()
, blocks()
, text(fontGame)
, score(0)
{
}
void GameState::handleInput()
{
if (!ballMove) {
if (Keyboard::isKeyPressed(Keyboard::D)) {
board.moveRight();
ball.setPosition(Vector2f(board.getPosition().x + board.getSize().x / 2 - ball.getRadius(), ball.getPosition().y));
}
if (Keyboard::isKeyPressed(Keyboard::A)) {
board.moveLeft();
ball.setPosition(Vector2f(board.getPosition().x + board.getSize().x / 2 - ball.getRadius(), ball.getPosition().y));
}
} else if (ballMove) {
if (Keyboard::isKeyPressed(Keyboard::D)) {
board.moveRight();
}
if (Keyboard::isKeyPressed(Keyboard::A)) {
board.moveLeft();
}
}
while (window.pollEvent(ev)) {
switch (ev.key.code) {
case Keyboard::Escape:
window.close();
break;
case Keyboard::Space:
ballMove = true;
}
}
}
void GameState::update()
{
if (ballMove) {
ball.move();
if (ball.getGlobalBounds().intersects(board.getGlobalBounds()))
ball.setVelocityY(-(rand() % 7 + 3));
if (blocks.handleCollision(ball.getGlobalBounds())) {
ball.setVelocityY((rand() % 7 + 3));
score += 50;
}
if (ball.getPosition().y > board.getPosition().y)
setGameOver(true);
}
}
void GameState::render(RenderWindow& window)
{
text.draw(&window, score);
ball.draw(&window);
board.draw(&window);
blocks.draw(&window);
}
int GameState::getScore() {
return score;
}
bool GameState::isGameOver() const
{
return gameOver;
}
void GameState::setGameOver(bool value)
{
gameOver = value;
}
EndGameState.cpp :
#include "../include/EndGameState.hpp"
EndGameState::EndGameState(RenderWindow& windowGame, Font& fontGame, int& score)
: window(windowGame)
, text(fontGame)
, scoreFinal(score)
{
text.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 9);
}
void EndGameState::handleInput() {
while (window.pollEvent(ev)) {
switch (ev.key.code) {
case Keyboard::Escape:
window.close();
break;
}
}
}
void EndGameState::update() {
}
void EndGameState::render(sf::RenderWindow& window) {
text.draw(&window, scoreFinal);
}
bool EndGameState::isGameOver() const {
}
int EndGameState::getScore() {
}
文本游戏.cpp :
#include "../include/TextGame.hpp"
TextGame::TextGame(Font& font) {
text.setFont(font);
text.setCharacterSize(36);
text.setColor(Color::White);
text.setPosition(60, 30);
}
void TextGame::setPosition(float x, float y) {
text.setPosition(Vector2f(x, y));
}
void TextGame::setCharacterSize(int size) {
text.setCharacterSize(size);
}
void TextGame::draw(RenderWindow* window, int score) {
text.setString("Score : " + std::to_string(score));
window->draw(text);
}
MakeFile(生成文件):
CXX = g++
CXXFLAGS = -Wall -Wreorder
SFMLFLAGS = -lsfml-graphics -lsfml-system -lsfml-window
SRCDIR = src
INCDIR = include
OBJDIR = obj
BINDIR = bin
TARGET = main
SOURCES = $(wildcard $(SRCDIR)/*.cpp)
HEADERS = $(wildcard $(INCDIR)/*.hpp)
OBJECTS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))
.PHONY: all clean
all: $(BINDIR)/$(TARGET)
@clear
@./$(BINDIR)/$(TARGET)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HEADERS)
$(CXX) $(CXXFLAGS) -I$(INCDIR) -c -o $@ $<
$(BINDIR)/$(TARGET): $(OBJECTS)
$(CXX) $^ -o $@ $(SFMLFLAGS)
clean:
@rm -rf $(OBJDIR)/*
@rm -rf $(BINDIR)/*
我以为问题出在文本输出中,但我删除了这个变量,没有任何改变......(
答: 暂无答案
评论
@
clear
make --version