dbzhang800:第一次是对的,不代表之后还是对的,要取决于你的代码。
根据实现方式不同,boundingRect() 和 shape() 可以是固定的,也可以是变化的。
.......
(2014-05-22 10:54) 
QRectF FOOD::boundingRect() const
{
return QRectF(x, y, FOOD_SIZE, FOOD_SIZE);
}
QPainterPath FOOD::shape() const
{
QPainterPath path;
// path.setFillRule(Qt::WindingFill);
path.addEllipse(x, y, FOOD_SIZE, FOOD_SIZE);
return path;
}
void FOOD::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// painter->setPen(Qt::NoPen);
// painter->setBrush(Qt::yellow);
// painter->drawEllipse(x, y, FOOD_SIZE, FOOD_SIZE);
painter->save();
painter->fillPath(shape(), Qt::yellow);
painter->restore();
}
这是食物shape boundingRect 还有paint的代码, 麻烦您帮忙检查一下好吗?x y就是食物生成的坐标,后面两个是食物的宽和高, 我还给food设置了data,
#define GAME_DATA 5
#define FOOD_TYPE 1
bool SNAKE::handleCollisions()
{
QList<QGraphicsItem *> collisions = collidingItems();
foreach (QGraphicsItem *collidingItem, collisions)
{
if (collidingItem->data(GAME_DATA) == FOOD_TYPE)
return true;
}
return false;
}
这是碰撞检测, 求帮忙看一下 万谢!