我正在制作一个只是为了实践的小游戏。我基本上想让我的角色(红色三角形)左右移动,跳跃等等。
在这种状态下,程序仍然非常小,我想知道'我该怎么做'。
我创建了var cell = Tableview.deqeueReusableCellWithIdentifier("cell")
和Player Class(QObject, QGraphicsPolygonItem)
。当我给Player类一个KeyPressEvent的方法时,它起作用了,但是我遇到了跳循环的问题:我想在我的跳循环中做一个场景> update()但是我不能因为场景是我的游戏类的属性。
然后,我尝试给游戏类Game Class(QGraphicsView)
,因此它会移动玩家*玩家属性。
所以基本上,我想要的是能够在我的for循环的每次迭代中看到我的rect更新的位置。 我能这样做吗?
希望这是有道理的,并且一如既往地感谢很多人!!
Player.h& Player.cpp
PressKeyEvent method
Game.h& Game.cpp
#pragma once
#include <QGraphicsPolygonItem>
#include <QBrush>
class Player :
public QObject, public QGraphicsPolygonItem
{
Q_OBJECT
public:
Player();
// Setters
void setIsJumping(bool);
// Getters
bool getIsJumping();
public slots:
private:
bool isJumping;
};
#include "Player.h"
Player::Player()
{
// ***************
// Draw the player
// ***************
QVector<QPointF> gemPoints;
gemPoints << QPointF(0, 2) << QPointF(1, 0) << QPointF(2, 2);
// Size
int SCALE_BY = 8;
for (size_t i = 0; i < gemPoints.size(); i++)
{
gemPoints[i] *= SCALE_BY;
}
// Create the polygon
QPolygonF gemModel(gemPoints);
setPolygon(gemModel);
setPos(400, 500);
// Color
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::red);
setBrush(brush);
// Make player focusable
this->setFlag(QGraphicsItem::ItemIsFocusable);
this->setFocus();
}
void Player::setIsJumping(bool jump)
{
if (jump == true)
{
isJumping = true;
}
else
{
isJumping = false;
}
}
bool Player::getIsJumping()
{
return isJumping;
}
Main.cpp的
#pragma once
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QKeyEvent>
#include "Player.h"
class Game :
public QGraphicsView
{
Q_OBJECT
public:
// Constructors
Game(QWidget* parent = NULL);
// Methods
void start();
void jump();
void keyPressEvent(QKeyEvent *event);
// Attributes
QGraphicsScene* scene;
Player* player;
};
#include "Game.h"
Game::Game(QWidget* parent)
{
// Set up the screen
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(1024, 768);
// Set up the scene
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 1024, 768);
setScene(scene);
}
void Game::start()
{
scene->clear();
player = new Player();
scene->addItem(player);
}
void Game::keyPressEvent(QKeyEvent * event)
{
if (event->key() == Qt::Key_Left)
{
player->setPos(x() - 3, y());
}
else if (event->key() == Qt::Key_Right)
{
player->setPos(x() + 3, y());
}
else if (event->key() == Qt::Key_Space)
{
for (size_t i = 0; i < 10; i++)
{
player->setIsJumping(true);
this->jump();
}
}
}
void Game::jump()
{
if (player->getIsJumping() == true)
{
for (size_t i = 0; i < 100; i++)
{
this->player->setPos(x(), y() - 0.1);
this->update();
}
player->setIsJumping(false);
}
}
问题在于我现在无法正确移动播放器,它只移动一次并且以一种奇怪的方式移动。任何帮助表示赞赏。
答案 0 :(得分:0)
问题在于,当您设置player
的位置时,您应该修改player
而不是this
的坐标。因此,只要您想移动player
,请使用:
player->setPos(player->x() - 3, player->y());
^ ^
和你在循环中跳跃一样:
player->setPos(player->x(), player->y() - 0.1);
^ ^
当您使用setPos(x()-3, y())
时,它会将玩家位置设置为QGraphicsView
的位置,这就是它从视图中消失的原因。祝你的游戏好运!