当前位置:网站首页>小白高薪捷径-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
边栏推荐
- How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
- Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
- Apache POI implements excel import, read data, write data and export
- ARFoundation从零开始5-AR图像跟踪
- 【config】配置数组参数
- QT学习:使用JSON/XML等非ts文件实现多语言国际化
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- js(forEach)出现return无法结束函数的解决方法
- The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
- tmux随笔
猜你喜欢

浅谈AspectJ框架

那个准时上下班,从不愿意加班加点的人,在我前面升职了...

6.3 references

Deadlock analysis using jstack, jconsole, and jvisualvm

6.2 function-parameters

How to add traffic statistics codes to the legendary Development Zone website

AUTOSAR from introduction to proficiency 100 lectures (78) -autosar-dem module

SparkSql批量插入或更新,保存数据到Mysql中

传奇服务端如何添加地图

自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量
随机推荐
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Arfoundation starts from scratch 8-geospatial API (geospatial) development
How does WPS take quick screenshots? WPS quick screenshot method
QT系列---安装
基于注解的三层项目的改造及添加包扫描的方式
VirtualBox has expanded the capacity of virtual hard disk (without modifying the original data)
Google gtest事件机制
tmux随笔
Pytorch learning notes
SM integration is as simple as before, and the steps are clear (detailed)
Learn the first program of database
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
三层项目的架构分析及构造方法的参数名称注入
缓存穿透、缓存击穿、缓存雪崩以及解决方法
Big silent event Google browser has no title
How to install Office2010 installation package? How to install Office2010 installation package on computer
TCP three handshakes and four waves
On AspectJ framework
Huawei ilearning AI mathematics foundation course notes
Vivo market API event reporting and docking