gpt4 book ai didi

c++ - 即使是最小的更新后 QGraphicsScene 也会崩溃

转载 作者:行者123 更新时间:2023-11-30 04:51:57 26 4
gpt4 key购买 nike

我目前正在使用 Qt 开发一个简单的项目。它基于流行的游戏“Ludo”。但是我在屏幕上进行任何更改时遇到了烦人的问题。棋盘绘图效果很好,但足以取消注释带有 play() 函数的行并保证发生灾难。只是我无法在 Player::turn() 中设置字段的像素图来显示棋子的移动。场景变得平淡无奇,每个绘制的区域都消失了。另一方面,由于输出,游戏逻辑正确地运行。不幸的是,它完全不与 GUI 合作。

我附上了“之前-之后”的图片和必要的代码。

main.cpp

QApplication a(argc, argv);

Board *scene = new Board();

QGraphicsView *view = new QGraphicsView(scene);
view->resize(1200, 1000);
view->show();

scene->draw();
// scene->play();

return a.exec();

棋盘::游戏()

dice = new Dice();
unsigned int i=0;
while(true) {

bool playMore = players.at(i)->turn(dice, players, fieldsToPlay);
if(playMore) {

i++;
if(i == 4)
i=0;
}
else break;
}

播放器::转()

int result;
std::cout << "Player " << colour << " ";

if(hasPawnOnField) {

// TODO: check if field has pawn
result = dice->roll();
std::cout << result << '\n';

while(result) {

pawns.at(0)->currentField->set_Pixmap(QPixmap(":/img/border.png"));
pawns.at(0)->currentField->setPawn(nullptr);
bool finito = pawns.at(0)->move(fieldsToPlay, baseAndFinish);
pawns.at(0)->currentField->setPawn(pawns.at(0));


char col = pawns.at(0)->getColour();
switch (col) {

case 'b':
pawns.at(0)->currentField->set_Pixmap(QPixmap(":/img/bluepawn.png"));
break;
case 'r':
pawns.at(0)->currentField->set_Pixmap(QPixmap(":/img/redpawn.png"));
break;
case 'y':
pawns.at(0)->currentField->set_Pixmap(QPixmap(":/img/yellowpawn.png"));
break;
case 'g':
pawns.at(0)->currentField->set_Pixmap(QPixmap(":/img/greenpawn.png"));
break;
}

if(finito) {

finishedPawns++;
delete pawns.at(0);
pawns.erase(pawns.begin());
hasPawnOnField = false;
break;
}

result--;
}
if(finishedPawns == 4) // condition which ends the game
return 0;
}
else {

int attempts = 3;

while(attempts) {

result = dice->roll();
std::cout << result << '\n';
if(result == 6) {

hasPawnOnField = true;
if(start->getPawn()) {

char col = start->getPawn()->getColour();
switch (col) {

case 'b':
players.at(0)->pawns.at(0)->setField(fieldsToPlay.at(0));
players.at(0)->pawns.at(0)->zero();
fieldsToPlay.at(0)->setPawn(players.at(0)->pawns.at(0));
fieldsToPlay.at(0)->set_Pixmap(QPixmap(":/img/bluepawn.png"));
break;
case 'r':
players.at(1)->pawns.at(0)->setField(fieldsToPlay.at(12));
players.at(1)->pawns.at(0)->zero();
fieldsToPlay.at(12)->setPawn(players.at(1)->pawns.at(0));
fieldsToPlay.at(12)->set_Pixmap(QPixmap(":/img/redpawn.png"));
break;
case 'y':
players.at(2)->pawns.at(0)->setField(fieldsToPlay.at(24));
players.at(2)->pawns.at(0)->zero();
fieldsToPlay.at(24)->setPawn(players.at(2)->pawns.at(0));
fieldsToPlay.at(24)->set_Pixmap(QPixmap(":/img/yellowpawn.png"));
break;
case 'g':
players.at(3)->pawns.at(0)->setField(fieldsToPlay.at(36));
players.at(3)->pawns.at(0)->zero();
fieldsToPlay.at(36)->setPawn(players.at(3)->pawns.at(0));
fieldsToPlay.at(36)->set_Pixmap(QPixmap(":/img/greenpawn.png"));
break;
}
}
pawns.at(0)->setField(start);
start->setPawn(pawns.at(0));
start->set_Pixmap(baseAndFinish.at(finishedPawns)->getPixmap());

baseAndFinish.at(finishedPawns)->set_Pixmap(QPixmap(":/img/border.png"));
break;
}

attempts--;
}
}

兵::移动()

passedFields++;

if(passedFields >= 48) {

currentField = basFin.at(4+(passedFields-48));

if(passedFields == FINISH) {
FINISH--;
delete basFin.at(basFin.size()-1);
basFin.pop_back();
return true;
}

}
else
currentField = fieldsToPlay.at( (startID+passedFields)%48);

return false;

棋盘.h

class Board : public QGraphicsScene {

Q_OBJECT

public:

Board();
virtual ~Board();
void draw();
void drawField(std::vector<Field *> &vec, Vector &current, Vector dir, QString image);
void play();

static const int rect_size = 70;

Vector directions[4] = {Vector (0, -rect_size), //up
Vector (rect_size, 0), //right
Vector (0, rect_size), //down
Vector (-rect_size, 0) }; //left

private:
std::vector<Field *> fieldsToPlay;
std::vector<Player *> players;

std::vector<Field *> fieldsBlue; //fields 0-3 basement, fields 4-7 finish
std::vector<Field *> fieldsRed;
std::vector<Field *> fieldsYellow;
std::vector<Field *> fieldsGreen;

Dice *dice;
QTimer *timer;
};

更新像素图前的场景

之后的场景

我做错了什么?如何在运行时更新我的​​场景而不会崩溃?

最佳答案

与其使用无限循环,不如使用 QTimer(我看到 Board 已经有一个 QTimer * 成员,所以也许一直以来的意图)。与其使用显式计时器,不如使用 QTimer::singleShot一次“插入”游戏向前迈出一步。

首先,让玩家计数器/索引 i 成为 Board 的成员,并在构造函数中初始化它和 dice...

Board::Board ()
: dice(new Dice)
, i(0)
{
...
}

现在更改 Board::play 以便它为当前玩家调用 Player::turn 并在需要时重新设置计时器...

void Board::play ()
{

/*
* Give the current player their turn.
*/
bool playMore = players.at(i)->turn(dice, players, fieldsToPlay);
if (playMore) {

/*
* The game hasn't yet finished so update the player index
* and rearm the timer with a lambda to make another call
* to play() in 1 second.
*/
i++;
if (i == 4)
i=0;
QTimer::singleShot(1000,
[this]()
{
play();
});
}
}

我还没有测试过上面的内容,但它至少显示了你应该追求的目标的基本轮廓。

关于c++ - 即使是最小的更新后 QGraphicsScene 也会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54673694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com