Qt制作贪吃蛇游戏

Qt制作界面很好看,而且它的模块也很多很全面,下面制作一个我们经常玩的贪吃蛇游戏。

#include "mysnake.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    mySnake w;
    w.show();

    return a.exec();
}
#ifndef MYSNAKE_H
#define MYSNAKE_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class mySnake;
}

class mySnake : public QMainWindow
{
    Q_OBJECT

public:
    explicit mySnake(QWidget *parent = 0);
    ~mySnake();
    void paintEvent(QPaintEvent *event);
    void init_Snake(); //初始化
    void speed(); //移动速度
    QRect createRect();//产生一个随机矩形
    void isEat(); //判断是否吃到
    void isHit(); //判断是否撞到自己

private slots:
    void Snake_update();

private:
    void keyPressEvent(QKeyEvent *key);
    Ui::mySnake *ui;
    bool isRun; //是否启动
    bool isOver; //是否结束
    QRect *SnakeRect;//蛇身
    QRect SnakeHeard;//蛇头
    QRect temp_SnakeRect;//随机矩形
    int SnakeLen; //蛇长
    QTimer *timer; //定时器
    int direction; //蛇移动方向
    QString display;//开始、结束提示
    QString scoreLabel;//得分标签
    int score; //得分
};

#endif // MYSNAKE_H
#include "mysnake.h"
#include "ui_mysnake.h"

mySnake::mySnake(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mySnake)
{
    ui->setupUi(this);
}

mySnake::~mySnake()
{
    delete ui;
}

void mySnake::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(!isRun)
{
init_Snake();
}
//画背景
painter.setPen(Qt::black);
painter.setBrush(Qt::blue);
painter.drawRect(15,15,260,260);
painter.setPen(Qt::black);
painter.setBrush(Qt::black);
painter.drawRect(20,20,250,250);
painter.setPen(Qt::blue);
for(int ii = 2;ii <= 27;ii++)
{
painter.drawLine(20,ii*10,270,ii*10);
painter.drawLine(ii*10,20,ii*10,270);
}
QFont font("Courier", 12);
painter.setFont(font);
painter.setPen(Qt::blue);
painter.drawText(30,290,"By chenjt3533 creation !");
//显示开始、结束
QFont font1("Courier", 24);
painter.setFont(font1);
painter.setPen(Qt::red);
painter.setBrush(Qt::red);
painter.drawText(40,150,display);
//计分
QFont font2("Courier",15);
painter.setFont(font2);
painter.setPen(Qt::red);
painter.setBrush(Qt::red);
painter.drawText(290,60,scoreLabel);
painter.drawText(360,60,QString::number(score));
//画蛇
painter.setBrush(Qt::red);
painter.setPen(Qt::black);
painter.drawRect(temp_SnakeRect);
painter.drawRects(SnakeRect,SnakeLen);
if(!isOver)
speed();//运动速度
}
void mySnake::keyPressEvent(QKeyEvent *event)
{
QKeyEvent *key = (QKeyEvent*)event;
switch(key->key())
{
case Qt::Key_Up : direction=1;break;
case Qt::Key_Down : direction=2;break;
case Qt::Key_Left : direction=3;break;
case Qt::Key_Right : direction=4;break;
default: ;
}
}
void mySnake::init_Snake()//初始化 snake
{
SnakeLen =5; //初始化 snake 的长度为 5
direction = 2; //初始化 snake 的运动方向向下
display ="GAME START...";
scoreLabel = "Score:";
score = 0;
isRun = true;
isOver = false;
temp_SnakeRect = createRect();
SnakeRect = new QRect[SnakeLen];
for(int i=0;i100,70+10*i,10,10);
SnakeRect[i] = rect;
}
SnakeHeard = SnakeRect[SnakeLen-1];
}
void mySnake::Snake_update()
{
display = "";
SnakeHeard = SnakeRect[SnakeLen-1];
isHit();
isEat();
for(int j = 0; j< SnakeLen-1;j++ )
{
SnakeRect[j] = SnakeRect[j+1];
}
switch(direction)
{
case 1: SnakeHeard.setHeight(SnakeHeard.height()-10);
SnakeHeard.setTop(SnakeHeard.top()-10);
break;
case 2: SnakeHeard.setHeight(SnakeHeard.height()+10);
SnakeHeard.setTop(SnakeHeard.top()+10);
break;
case 3: SnakeHeard.setLeft(SnakeHeard.left()-10);
SnakeHeard.setRight(SnakeHeard.right()-10);
break;
case 4: SnakeHeard.setLeft(SnakeHeard.left()+10);
SnakeHeard.setRight(SnakeHeard.right()+10);
break;
default :
;
}
SnakeRect[SnakeLen-1] = SnakeHeard;
if(SnakeHeard.left()<20||SnakeHeard.right()>270||SnakeHeard.top()>260||SnakeHeard.bottom()<20)
{
display = "GAME OVER !";
isOver = true;
}
update();//paintEvent 更新
}
void mySnake::speed()//运动速度
{
timer = new QTimer(this);
timer->setSingleShot(true); //将会只启动定时器一次,启动后每经过一次设定的时间就发送一次 timeout()
timer->start(500); //1 秒
connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));
}
QRect mySnake::createRect()//产生一个随机矩形
{
int x,y;
x = qrand()%25;
y = qrand()%25;
QRect rect(20+x*10,20+y*10,10,10);
return rect;
}
void mySnake::isEat()
{
if(SnakeHeard == temp_SnakeRect)
{
SnakeHeard = temp_SnakeRect;
SnakeRect[SnakeLen] = SnakeHeard;
SnakeLen++;
temp_SnakeRect = createRect();
score+=10;
}
}
void mySnake::isHit()
{
for(int i = 0;i< SnakeLen-1 ;i++)
{
if(SnakeHeard == SnakeRect[i])
{
display = "GAME OVER !";
isOver = true;
update();
}
}
}

效果如下:
Qt制作贪吃蛇游戏_第1张图片

吃到红色的食物后游戏就结束啦,所以还需要改良。

你可能感兴趣的:(QT学习笔记)