qt写贪吃蛇

本文介绍了使用Qt框架编写贪吃蛇游戏的过程。首先,通过宏定义设置游戏界面大小,创建一个QLabel矩阵。接着,利用一个二维整数数组来管理每个QLabel的状态,通过数组值控制标签的颜色和其他属性,以此实现游戏逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实刚才我写过一次这篇文章,然后我要发表,一直提示我选择文章类型,后来才发现,左上角有个原创或转载什么的选项,于是一大段内心澎湃又细致的文字就这么没了,第二次写,虽然是第一篇,感觉还是没第一次好,废话不多说。



用qt写贪吃蛇,源于我们开的windows程序设计课,这是一个小任务。


1.宏定义max_x,max_y,给整个界面定义,一个qlabel矩阵

2.int定义一个二维数组,对应每一个label,通过值为0,1,2,来控制,label 的颜色什么的(之前那篇文章可谓声情并茂,然而,万恶的复制粘贴失败,什么都一场空了)

Labels[i][j]->setStyleSheet("background:pink");我定义的普通界面为粉色,蛇身为蓝色,没有墙,不过过程中还是有墙的,撞到边界就会gameover哒
3.然后定义蛇<pre name="code" class="plain"> QList<QPoint>snake;
   
4.通过qtimer来使蛇动起来

5.判断蛇有没有碰到墙,有没有咬到自己,这些都要gameover
6.判断蛇有没有吃到食物,吃到蛇身要变长。



接下来说一些自己遇到的疑惑:
疑惑一:qlist是什么,自行百度,是一种装不同类型数据的容器,也是在使用和看代码过程中理解,什么叫容器的

<span style="white-space:pre">	</span>
疑惑二:为什么不直接装int在qlist里面,这样可以直接判断对应的label怎么表示啊,后来一想,因为蛇要动起来,蛇身要变长什么的,如果用int,就不能取出蛇头的横纵坐标和蛇尾的横纵坐标,就不好控制移动了
<span style="white-space:pre">	</span>例如,qpoint.x();qpoint.y(),可以获取横纵坐标;qpoint.setx(值),qpoint.sety(值)来设置该点的横纵坐标;QPoint newpoint = QPoint(1,0); oldpoint += newpoint;这些都是可以的。
疑惑三:蛇怎么动,对此,我在纸上画图,终于想明白它的本质,其实都是假象<img alt="尴尬" src="http://static.blog.youkuaiyun.com/xheditor/xheditor_emot/default/awkward.gif" />(老师说的),因为,很多人一开始不理解,就是觉得,怎么让蛇头往前面挪一个,蛇头后面的往前面挪一格,然后整体往前面挪,其实不是这样的,其实整个游戏,不都是建立在一开始你定义的label上吗?有的人问,到底是label在移动还是qpoint在移动,本质上,什么都没有动,只是通过qpoint取出它的横纵坐标,可以得到对应的二维数组的值和label,从而,使label,变成粉色或者蓝色,或者
好的,下面是一份简单的 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 类来实现游戏界面,其中包括两条蛇和一个食物。玩家可以使用键盘控制其中一条蛇的移动,另一条蛇则以固定速度自动移动。当一条蛇吃到食物后,它的身体会变长,并且会重新生成一个食物。如果两条蛇相撞,则游戏结束。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值