当前位置:网站首页>小白高薪捷径-Qt开发游戏—贪吃蛇
小白高薪捷径-Qt开发游戏—贪吃蛇
2022-07-29 05:07:00 【cpp编程】
- C/C++初学者的困惑
很多C/C++初学者,自学了很多年,考了很多证,开发水平仍停留在控制台小项目上,就业更是遥遥无期,进退两难。

C/C++的最强大的地方,当然是开发高性能框架、高性能服务器:
基于C++框架Tenserflow实现的阿尔法狗:

千万级并发服务器Nginx:

在问鼎C/C++技术之巅之前,有没有一个技术方向,让C/C++小白就能够快速高薪就业的呢?
小白高薪研发的最佳方向,莫不过于C++ Qt开发,已经广泛应用于物联网、嵌入式、音视频、图形处理!
来自研发一线的岗位召唤:


让我们从零起步,90分钟开发一个Qt版的贪食蛇,快速掌握C++ Qt开发!

【私信小编,获取视频版的完整教程】
- 创建项目

选择模板

选择编译器:
准备游戏素材
准备素材

添加数据成员
enum Direct{Left,Right,Up,Down};
QList<QRectF> snake;//贪吃蛇本体
int snakeNodeWidth = 20;
int snakeNodeHeight = 20;
QTimer *timer;
int time = 150;
int moveFlage = Up;
bool gameStart = false;
QRectF rewardNode;添加方法的声明
protected:
void paintEvent(QPaintEvent *);
void keyPressEvent(QKeyEvent *);
void addTop();
void addDown();
void addLeft();
void addRight();
void deleteLast();
bool checkContact();
void addNewReward();添加槽函数
protected slots:
void timeOut();构造函数
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
resize(600, 368);
snake.append(QRectF(300,180,snakeNodeWidth,snakeNodeHeight));
addTop();
addTop();
timer = new QTimer;
connect(timer, SIGNAL(timeout()),this,SLOT(timeOut()));
addNewReward();
}按键时间处理
#include <QKeyEvent>
void Widget::keyPressEvent(QKeyEvent *event)
{
switch(event->key()){
case Qt::Key_Up:
if(moveFlage != Down){
moveFlage = Up;
}
break;
case Qt::Key_Down:
if(moveFlage != Up){
moveFlage = Down;
}
break;
case Qt::Key_Right:
if(moveFlage != Left){
moveFlage = Right;
}
break;
case Qt::Key_Left:
if(moveFlage != Right){
moveFlage = Left;
}
break;
case Qt::Key_Space:
if(!gameStart){
timer->start(time);
gameStart = true;
}else {
timer->stop();
gameStart = false;
}
break;
default:
break;
}
}超时事件处理
void Widget::timeOut()
{
int count = 1;
if (snake.at(0).intersects(rewardNode)) {
addNewReward();
count++;
}
while (count--) {
switch (moveFlage) {
case Up:
addTop();
break;
case Down:
addDown();
break;
case Right:
addRight();
break;
case Left:
addLeft();
break;
default:
break;
}
}
deleteLast();
update();
}添加新方块
void Widget::addNewReward() {
rewardNode = QRectF(
qrand()%(this->width()/20)*20,
qrand()%(this->height()/20)*20,
snakeNodeWidth,
snakeNodeWidth);
}各个方向的运动实现
//向上移动
void Widget::addTop()
{
QPointF leftTop;
QPointF rightBotom;
if(snake.at(0).y()-snakeNodeHeight < 0){
leftTop = QPointF(
snake.at(0).x(), //左上角x坐标
this->height()-snakeNodeHeight); //左上角y坐标
rightBotom = QPointF(
snake.at(0).x()+snakeNodeWidth,
this->height());
}else{
leftTop = QPointF(snake.at(0).x(),
snake.at(0).y() - snakeNodeHeight);
rightBotom = snake.at(0).topRight();
}
snake.insert(0, QRectF(leftTop, rightBotom));
}
//向下移动
void Widget::addDown()
{
QPointF leftTop;
QPointF rightBotom;
if(snake.at(0).y()+snakeNodeHeight*2 > this->height()){
leftTop = QPointF(snake.at(0).x(), 0);
rightBotom = QPointF(snake.at(0).x()+snakeNodeWidth, snakeNodeHeight);
}else{
leftTop = snake.at(0).bottomLeft();
rightBotom = snake.at(0).bottomRight() + QPointF(0, snakeNodeHeight);
}
snake.insert(0, QRectF(leftTop, rightBotom));
}
//向左移动
void Widget::addLeft()
{
QPointF leftTop;
QPointF rightBotom;
if(snake.at(0).x()-snakeNodeWidth < 0){
leftTop = QPointF(this->width() -snakeNodeWidth, snake[0].y());
}else{
leftTop = snake[0].topLeft() - QPointF(snakeNodeWidth, 0);
}
rightBotom = leftTop + QPointF(snakeNodeWidth, snakeNodeHeight);
snake.insert(0, QRectF(leftTop, rightBotom));
}
//向右移动
void Widget::addRight()
{
QPointF leftTop;
QPointF rightBotom;
if(snake.at(0).x()+snakeNodeWidth*2 > this->width()){
leftTop = QPointF(0, snake[0].y());
}else{
leftTop = snake[0].topRight();
}
rightBotom = leftTop + QPointF(snakeNodeWidth, snakeNodeHeight);
snake.insert(0, QRectF(leftTop, rightBotom));
}
//删除结尾数据
void Widget::deleteLast()
{
snake.removeLast();
}绘制贪食蛇
#include <QPainter>
#include <QPen>
#include <QBrush>
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen; //画笔,用于绘制轮廓
QBrush brush; //画刷,用于填充
QPixmap pix;
pix.load("D:/tmp/snake.jpg");
painter.drawPixmap(0,0, 600, 368,pix);
//反锯齿
painter.setRenderHint(QPainter::Antialiasing);
pen.setColor(Qt::black);
brush.setColor(Qt::darkMagenta);
brush.setStyle(Qt::SolidPattern);
painter.setPen(pen);
painter.setBrush(brush);
for(int i=0; i<snake.length(); i++){
painter.drawRect(snake.at(i));
}
brush.setColor(Qt::red);
painter.setBrush(brush);
painter.drawEllipse(rewardNode);
pen.setColor(Qt::black);
painter.setPen(pen);
QFont font("微软雅黑", 12,QFont::ExtraLight,false);
painter.setFont(font);
painter.drawText(560, 32, QString("%1").arg(snake.length()));
if(checkContact()){
QFont font("方正舒体",30,QFont::ExtraLight,false);
painter.setFont(font);
painter.drawText((this->width()-300)/2,(this->height()-30)/2,QString("GAME OVER!"));
timer->stop();
}
QWidget::paintEvent(event);
}失败检测
//判断蛇头是否和蛇身相撞, 以及蛇身和蛇身相撞
bool Widget::checkContact()
{
for(int i=0; i<snake.length();i++) {
for(int j=i+1; j<snake.length(); j++){
if(snake.at(i) == snake.at(j)){
return true;
}
}
}
return false;
}游戏结束效果

今天的分享就到这里了,大家要好好学C语言/C++哟~
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)加下方群获取哦~
C语言C++编程学习交流圈子,QQ群:763855696【点击进入】
C语言从入门到精通(C语言入门C语言教程C语言零基础C语言基础C语言学习C
边栏推荐
- 三层项目的架构分析及构造方法的参数名称注入
- What if excel is stuck and not saved? The solution of Excel not saved but stuck
- Diagram of odoo development tutorial
- 那个准时上下班,从不愿意加班加点的人,在我前面升职了...
- ARFoundation入门教程10-平面检测和放置
- How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
- Use annotation test in idea
- 使用Jstack、Jconsole和jvisualvm进行死锁分析
- 网安学习-内网安全1
- 学习数据库的第一个程序
猜你喜欢
随机推荐
ARFoundation入门教程10-平面检测和放置
WDDM学习
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
QML定制TabBar
Huawei ilearning AI mathematics foundation course notes
ARFoundation从零开始3-创建ARFoundation项目
Arfoundation starts from scratch 3- create an arfoundation project
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
基于注解的三层项目的改造及添加包扫描的方式
源码编译pytorch坑
传奇如何一台服务器配置多个版本微端更新
JS (in ES6) sync & await understanding
Network Security Learning - Intranet Security 1
后置通知的流程分析与功能实现有哪些内容你还记得吗?
Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
How to solve the problem of configuring the progress every time Office2010 is opened?
Understand activity workflow
Ros1 dead chicken data is stored in txt and SQLite
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
ODOO开发教程之透视表









