- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Link to project有趣的部分应该在gameengine.cpp的“launchSplash”-function和splashanimation.cpp中
游戏在可接受的范围内随机创建气泡。玩家的工作是射出带有水滴的泡泡。水滴从游戏画面的中下部发射。网格仅用于调试,稍后将消失,但它使区域可视化更容易。
向泡泡射击水滴会破坏泡泡,但当水滴碰到泡泡或游戏的上边界时,水滴就会消失。水滴射向鼠标点击的方向。
我正在尝试为基本的泡泡射击游戏创建碰撞检测,但我不确定如何以一种巧妙的方式检测碰撞。
游戏板看起来像这样game board ,水滴从屏幕中下部向光标方向射出。
最终我会让水滴从墙上弹射出来,但目前我不屑于弄清楚如何首先检测碰撞。
游戏板是 500x600 单位(宽 x 高),所以水滴的射点是 (250, 600)。
射水滴的时候,我用
void GameEngine::launchSplash(int clickX, int clickY){
// <my long method of calculating the coordinates for the water drop's path>
graphicalGameBoard_.animateSplash(graphicalGameBoard_.width()/2, graphicalGameBoard_.height(), xDestination, yDestination);
}
哪里xDestination
和 yDestionation
是水滴在不受阻碍的情况下最终会到达的地方。水滴将在 x=0/x=500 和/或 y=0/y=600 结束,但我认为这无关紧要。
气泡被添加到游戏板上
board_.clear();
for(int y = 0; y < HEIGHT; ++y)
{
std::vector< std::shared_ptr<Bubble> > row;
for(int x = 0; x < WIDTH; ++x)
{
std::shared_ptr<Bubble> newBubble = nullptr;
// There will be bubbles only in the top 3/4 of the board only in the middle
// (i.e. not in the first two and last two columns).
if (y < HEIGHT*3/4 && x > 1 && x < WIDTH-2)
{
// Generate random numbers using the enumearation type defined in
// file bubble.hh. The value COLOR_COUNT is used to represent no bubble.
std::uniform_int_distribution<int> distribution(RED,COLOR_COUNT);
// If you want no empty squares, change the initialization to:
// std::uniform_int_distribution<int> distribution(RED,BLUE);
Color color = static_cast<Color>(distribution(randomEngine_));
if(color != COLOR_COUNT) {
newBubble = std::make_shared<Bubble>(x, y, color);
}
}
row.push_back(newBubble);
}
board_.push_back(row);
}
在gameengine.cpp
绘制板和水滴被射向气泡。
水滴是用
绘制的SplashAnimation::SplashAnimation(GameBoard* scene, QPointF startPoint, QPointF endPoint):
QVariantAnimation(0),
scene_(scene),
item_()
{
scene_->addItem(&item_);
// The animation runs for the given duration and moves the splash
// smoothly from startpoint to endpoint.
setDuration(2000);
setKeyValueAt(0, QPointF(startPoint));
setKeyValueAt(1, QPointF(endPoint));
}
我想有两种方法可以做到这一点:内置的 QT 碰撞检测或进行单独的计算。我无法使用 QT Collision,而且我尝试手动检测碰撞的效果并不理想。
我已经有一个检测特定单元格气泡对象的函数,但它在列/行中而不是原始坐标 (500x600) 中。
std::shared_ptr<Bubble> GameEngine::bubbleAt(int x, int y) const
{
if (0 <= x and x < WIDTH and 0 <= y and y < HEIGHT){
return board_.at(y).at(x);
}
else{
return nullptr;
}
}
编辑:目前我正在尝试做这样的事情,但我担心它对游戏来说会有点沉重,因为它迭代了太多(或没有?):
for (int i = 0; i<600;++i)
{
xfract = (xDestination+250.0)/600.0;
yfract = (600.0-yDestination)/600.0;
xStep = xfract*i;
yStep = yfract*i;
if (xStep >= 50){
thisX = xStep/50-5;
}else{
thisX=5;
}
if (yStep >= 50){
thisY = 11-yStep/50 + 1;
}else{
thisY = 11;
}
thisX = abs(thisX);
if (bubbleAt(thisX, thisY)!=nullptr){
endX = xfract*i;
endY = yfract*i;
i = 600;
std::cout << "collision at x: "<<thisX<< " y: "<<thisY<<std::endl;
std::cout << "collision at x: "<<xStep<< " y: "<<yStep<<std::endl;
std::cout << graphicalGameBoard_.width() << " " << graphicalGameBoard_.height()<<std::endl;
removeBubble(thisX, thisY);
graphicalGameBoard_.removeBubble(thisX, thisY);
endY = 600-endY;
}
}
graphicalGameBoard_.animateSplash(graphicalGameBoard_.width()/2, graphicalGameBoard_.height(), endX, endY);
我试图将步骤分成小部分,并检查当前步骤中是否有气泡,直到水滴到达终点或发现气泡。
这在计算方面仅在我的一侧有效,但动画不合时宜,右侧 (x>250) 碰撞检测由于某种原因根本不起作用(它在右侧不可能的位置击中看似随机的气泡)。
编辑^2:以下是我为使用 QT 的实际碰撞检测而尝试过的东西:
在 splashanimation.cpp 中,使用
绘制水滴SplashAnimation::SplashAnimation(GameBoard* scene, QPointF startPoint, QPointF endPoint):
QVariantAnimation(0),
scene_(scene),
item_()
{
scene_->addItem(&item_);
// The animation runs for the given duration and moves the splash
// smoothly from startpoint to endpoint.
setDuration(2000);
setKeyValueAt(0, QPointF(startPoint));
setKeyValueAt(1, QPointF(endPoint));
}
SplashAnimation::~SplashAnimation()
{
scene_->removeItem(&item_);
}
void SplashAnimation::updateCurrentValue(QVariant const& value)
{
item_.setPos(value.toPointF());
}
其中 scene 是 QGraphicsScene,并且是 this
的父级包含气泡。
我在 gameboard.cpp(它是气泡和动画的父级)和 splash.cpp(它为水滴设置动画)上都试过了,但它们都给我同样的编译错误。
QGraphicsItem::QGraphicsItem();
给予
错误:无法调用构造函数 ?QGraphicsItem::QGraphicsItem?直接 [-fpermissive] QGraphicsItem::QGraphicsItem(); ^
QList<QGraphicsItem *> list = collidingItems() ;
给出 error: ?collidingItems? was not declared in this scope
QList<QGraphicsItem *> list = collidingItems() ;
^
QList<QGraphicsItem *> list = QGraphicsItem::collidingItems() ;
给出 error: cannot call member function ?QList<QGraphicsItem*> QGraphicsItem::collidingItems(Qt::ItemSelectionMode) const? without object
QList<QGraphicsItem *> list = QGraphicsItem::collidingItems() ;
^
我也尝试添加参数,但我想尝试的任何方法都效果不佳。
最佳答案
在这个回答中,我将为您提供一些用于实现解决方案的建议:
避免使用以下指令,使用信号,这是 Qt 最强大的元素之一,并且事件循环可以完成工作。
while (animations_.state() == QAbstractAnimation::Running)
{
QCoreApplication::processEvents(QEventLoop::AllEvents);
}
QGraphicsScene
使用支持浮点的坐标,因此场景的坐标由 QPointF
处理。
使用上述方法,如果您要发送点信息,请在信号中使用 QPointF
而不是 int, int
。
使用Qt提供的方法,例如:
if (0 <= clickPosition.x() and clickPosition.x() <= GRID_SIDE*WIDTH and
0 <= clickPosition.y() and clickPosition.y() <= GRID_SIDE*HEIGHT)
可以简化为:
if(sceneRect().contains(event->scenePos()))
该实现的优点是它更具可读性
我不明白为什么你不能实现collidingItems,可能是你.pro的配置问题,
使用以下命令添加gui模块、核心和小部件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
还可以使用我之前的 answer 实现动画
完整的功能代码可以在以下link找到.
关于c++ - 2 QGraphicsPixmapItems 之间的 QT 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138172/
我目前正在 Python 中使用 PyQt4,但我无法在网上找到任何有关将 PyQt UI 上的 GraphicsView 类中的 2 个不同项目组合在一起的信息。这可能吗? 目标是将一个 QGrap
我目前正在 Python 中使用 PyQt4,但我无法在网上找到任何有关将 PyQt UI 上的 GraphicsView 类中的 2 个不同项目组合在一起的信息。这可能吗? 目标是将一个 QGrap
我不久前开始使用 PyQt4,现在我尝试使用 QGraphicsItemAnimation 创建一个小动画。我以前从未使用 PyQt4 这样做过,我总是使用 pygame。必须有不同的图层,qpixm
QGraphicsPixmapItem 和 QGraphicsItem 一样,有一个方法 update(x0, y0, width, height),以便仅在 QGraphicsScene 上部分重绘
我在缩放和转换 QGraphicsPixmapItem 时遇到问题。我创建了图形编辑器,我在其中使用初始大小为 100,100 的 QGraphicsItem,之后我通过 setTransform()
我希望我的 QGraphicsPixmapItem 在 QGraphicScene 上变得可选(即可以更一般的方式点击),但它不是。我实际上是在修改 Qt 的 Diagram Scene sample
我正在使用 QGraphicsScene/View 开发自定义小部件,但我之前没有相关经验。 自定义小部件是一个图像查看器,需要跟踪鼠标移动并向其父对话框/窗口发送信号。信号将是鼠标光标下像素的位置及
我创建了这样的东西。我创建了一个 QGraphicsPixmapItem 对象,可以将其添加到 QGraphicsView 中。我想将端口添加到我创建的这些对象中。我想在这些端口和我创建的对象之间建立
我正在尝试在 PyQt5 中为 QGraphicsPixmapItem 制作动画。程序按原样崩溃,没有任何错误消息,删除有关变量“anime”的行使程序正常工作。 这是 QGraphicsPixMap
首先,我是一个完全的初学者所以请原谅我,如果这个问题很愚蠢或被误导了。我有以下问题,我创建这样的图形 setPixmap(QPixmap(":Graphics/Terrain")); (这是一个正方形
我需要一个对象,它结合了 QGraphicsPixmapItem 和 QGraphicsTextItem(图像 + 文本)的特性。 多重继承是最好的方法还是有任何禁忌症?如果有,您有什么建议? 最佳答
我正在开发一个以 Qt 4.8 作为 GUI 框架的 C++ 应用程序:一个子类 QGraphicsView 渲染一个子类 QGraphicsScene,其中包含一个或多个子类 QGraphicsPi
我想在我的QGraphicsScene中使用QPropertyAnimation为QGraphicsPixmapItem设置动画。 QPixmap pixmap(filePath); QGraphic
我有两个QGraphicsPixmapItem,它们的边缘都是透明的,而且它们不是矩形。当我尝试使用 QGraphicsItem::collidingItems() 时,它只检查它们的边界矩形是否发生
我正在尝试创建自己的 QGraphicsPixmapItem,我可以在其中激活悬停模式,并且我想在悬停在项目的矩形上时绘制黑色边框,并在离开矩形空间时恢复正常。 我开始了这段代码,但不知道接下来要做什
我编写了一个小代码来使用 QtCreator 测试 QGraphicsView 的功能。 代码非常简单,只是创建了一个继承自 QGraphicsView 的类,上面有一个 QGraphicsScene
我继承了 QGraphicsView(类 Display)并实现了从 QTableView 拖放。 QGraphicsScene 中有预先存在的项目,并且 scene()->itemsBounding
我正在尝试实现一个基本游戏... 我按下键盘按钮,但我添加到我的 QGraphicsScene 的 QGraphicsPixmapItem不动,我实现了一个keyPressedEvent函数... 我
我正在使用 QGraphicsScene,我想要一些在移动时发出信号的项目。 不幸的是,QGraphicsPixmapItem 没有任何信号,所以我将它子类化: class InteractiveGr
我在 QGraphicsScene 中有一个 QGraphicsPixmapItem,然后我在 QGraphicsView 中放大并旋转它。如果没有旋转或者我没有放大太多,一切都很好,但是当我放大(像
我是一名优秀的程序员,十分优秀!