qt写贪吃蛇游戏,要求两条蛇
时间: 2023-07-05 15:03:53 浏览: 134
Qt编写贪吃蛇游戏
好的,下面是一份简单的 Qt 贪吃蛇游戏代码,其中包括两条蛇:
```cpp
#include <QtWidgets>
const int WIDTH = 400; // 游戏界面宽度
const int HEIGHT = 400; // 游戏界面高度
class Snake : public QGraphicsItem
{
public:
Snake(QPointF pos, qreal w, qreal h, QColor color, QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent), m_rect(pos.x(), pos.y(), w, h), m_color(color)
{}
QRectF boundingRect() const override
{
return m_rect;
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setBrush(QBrush(m_color));
painter->drawRect(m_rect);
}
void move(int dx, int dy)
{
m_rect.translate(dx, dy);
setPos(m_rect.topLeft());
}
private:
QRectF m_rect;
QColor m_color;
};
class Food : public QGraphicsItem
{
public:
Food(QPointF pos, qreal w, qreal h, QColor color, QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent), m_rect(pos.x(), pos.y(), w, h), m_color(color)
{}
QRectF boundingRect() const override
{
return m_rect;
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setBrush(QBrush(m_color));
painter->drawEllipse(m_rect);
}
private:
QRectF m_rect;
QColor m_color;
};
class Game : public QGraphicsView
{
public:
Game(QWidget *parent = nullptr)
: QGraphicsView(parent), m_scene(new QGraphicsScene(this)), m_timer(new QTimer(this))
{
setFixedSize(WIDTH, HEIGHT);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setScene(m_scene);
m_timer->setInterval(100);
connect(m_timer, &QTimer::timeout, this, &Game::moveSnakes);
}
void start()
{
m_snake1 = new Snake(QPointF(100, 100), 10, 10, Qt::blue, nullptr);
m_snake2 = new Snake(QPointF(300, 300), 10, 10, Qt::green, nullptr);
m_scene->addItem(m_snake1);
m_scene->addItem(m_snake2);
spawnFood();
m_timer->start();
}
private:
void keyPressEvent(QKeyEvent *event) override
{
switch (event->key()) {
case Qt::Key_Left:
m_snake1->move(-10, 0);
break;
case Qt::Key_Right:
m_snake1->move(10, 0);
break;
case Qt::Key_Up:
m_snake1->move(0, -10);
break;
case Qt::Key_Down:
m_snake1->move(0, 10);
break;
case Qt::Key_A:
m_snake2->move(-10, 0);
break;
case Qt::Key_D:
m_snake2->move(10, 0);
break;
case Qt::Key_W:
m_snake2->move(0, -10);
break;
case Qt::Key_S:
m_snake2->move(0, 10);
break;
}
}
void spawnFood()
{
qreal x = QRandomGenerator::global()->bounded(0, WIDTH - 10);
qreal y = QRandomGenerator::global()->bounded(0, HEIGHT - 10);
m_food = new Food(QPointF(x, y), 10, 10, Qt::red, nullptr);
m_scene->addItem(m_food);
}
void moveSnakes()
{
QList<QGraphicsItem*> items = m_scene->items();
for (QGraphicsItem *item : items) {
if (item == m_food) {
if (m_snake1->collidesWithItem(m_food)) {
m_snake1->setRect(m_snake1->rect().adjusted(-10, -10, 10, 10));
spawnFood();
} else if (m_snake2->collidesWithItem(m_food)) {
m_snake2->setRect(m_snake2->rect().adjusted(-10, -10, 10, 10));
spawnFood();
}
} else if (item == m_snake1 || item == m_snake2) {
QGraphicsItem::ShapeMode mode = QGraphicsItem::Type;
QList<QGraphicsItem*> collidingItems = item->collidingItems(mode);
for (QGraphicsItem *collidingItem : collidingItems) {
if (collidingItem == m_snake1 || collidingItem == m_snake2) {
m_timer->stop();
QMessageBox::information(this, "Game Over", "Game Over!");
return;
}
}
}
}
m_snake1->move(0, 10);
m_snake2->move(0, -10);
}
QGraphicsScene *m_scene;
QTimer *m_timer;
Snake *m_snake1;
Snake *m_snake2;
Food *m_food;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Game game;
game.show();
game.start();
return app.exec();
}
```
这份代码使用了 Qt 的 QGraphicsScene 和 QGraphicsItem 类来实现游戏界面,其中包括两条蛇和一个食物。玩家可以使用键盘控制其中一条蛇的移动,另一条蛇则以固定速度自动移动。当一条蛇吃到食物后,它的身体会变长,并且会重新生成一个食物。如果两条蛇相撞,则游戏结束。
阅读全文