gpt4 book ai didi

c++ - 具有无响应 keyPressEvent 的 QGraphicsPixmapItem

转载 作者:行者123 更新时间:2023-11-28 02:30:23 27 4
gpt4 key购买 nike

我正在尝试实现一个基本游戏...

我按下键盘按钮,但我添加到我的 QGraphicsScene 的 QGraphicsPixmapItem不动,我实现了一个keyPressedEvent函数...

我想在按下某个键时移动像素图项目。

代码如下....

marioChar.h(我的像素图项的头文件)

 class marioChar : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
bool flying;
explicit marioChar(QPixmap pic);
void keyPressEvent(QKeyEvent *event);

signals:

public slots:
};

这是 keypressEvent 处理程序的实现:

   void marioChar::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Right)
{
if(x()<380)
{
this->setPos(this->x()+20,this->y());
}
}
}

This is part of the game class where i add the pixmap item to the scene



game::game(int difficulty_Level)
{

set_Level(difficulty_Level);
set_Num_Of_Coins(0);
set_Score(0);
QGraphicsScene *scene = new QGraphicsScene();
header = new QGraphicsTextItem();
header->setZValue(1000);
timer = new QTimer();
time = new QTime();
time->start();
updateDisplay();
scene->addItem(header);

connect(timer,SIGNAL(timeout()),this,SLOT(updateDisplay()));
timer->start(500);

QGraphicsView *view = new QGraphicsView(scene);
scene->setSceneRect(0,0,1019,475);

QColor skyBlue;
skyBlue.setRgb(135,206,235);
view->setBackgroundBrush(QBrush(skyBlue));

QGraphicsRectItem *floor = new QGraphicsRectItem(0,460,1024,20);
floor->setBrush(Qt::black);
scene->addItem(floor);

player= new marioChar(QPixmap("MarioF.png"));
player->setPos(0,330);
player->setZValue(1003);
scene->addItem(player);

view->setFixedSize(1024,480);
view->show();
player->setFocus();
}

提前致谢

最佳答案

关于c++ - 具有无响应 keyPressEvent 的 QGraphicsPixmapItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168390/

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