qt的贪吃蛇实现

通过查阅资料,完成最终样品


snake.pro为配置文件

widget为父窗口相关文件

gamewidget为子窗口相关文件

⒉widget.h中的代码

#ifndef WIDGET_H#define WIDGET_H

#include<QWidget>#include<QIcon>#include<QPalette>#include<QBrush>#include<QPixmap>#include<QPushButton>#include<QMessageBox>

#include"gamewidget.h"

 

class Widget : public QWidget

{

    Q_OBJECT

public:

    Widget(QWidget *parent = 0);

    ~Widget();

    QPushButton *startPush;

    QPushButton *exitPush;

    QMessageBox *exitBox;

    Gamewidget  *gamewindow;//声明一个新的界面

private slots:

    void M_startPush();

    void M_exitPush();

 

};

#endif // WIDGET_H

3.

widget.cpp中的代码

 #include "widget.h"#include"gamewidget.h"#include<QDebug>

 

 

Widget::Widget(QWidget *parent)

    : QWidget(parent)

{

    this->resize(891,510);

    this->setWindowTitle("贪吃蛇");

    this->setWindowIcon(QIcon(":/Picture/img/WindowPicture.jpg"));

    QPalette palette;

    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/Picture/img/Back2.jpg")));

    this->setStyleSheet("background-image:url(qrc:/new/Gamewindow/img/GameBack.jpg)");

    this->setPalette(palette);

 

    startPush=new QPushButton(this);

    exitPush=new QPushButton(this);

    startPush->setIconSize(QSize(920,45));

    startPush->setGeometry(400,360,90,45);

    startPush->setIcon(QIcon(":/Picture/img/Start.png"));

    startPush->setFlat(1);

    exitPush->setIconSize(QSize(90,45));

    exitPush->setGeometry(400,420,90,45);

    exitPush->setIcon(QIcon(":/Picture/img/Exit.png"));

    exitPush->setFlat(1);//设置按钮的透明属性

 

    exitBox=new QMessageBox;

    connect(exitPush,SIGNAL(clicked(bool)),this,SLOT(M_exitPush()));

    connect(startPush,SIGNAL(clicked(bool)),this,SLOT(M_startPush()));

}

void Widget::M_startPush()

{

    gamewindow=new Gamewidget(this);  //创建一个新的界面,任然以原来的界面为父窗口,退出后可以直接回到上一个界面。

    gamewindow->show();

}void Widget::M_exitPush()

{

    if(QMessageBox::Yes==QMessageBox::question(this,"Tips:","Do you want to leave th ox:⒋gamewidget.h中的代码:No))

    {

 

  delet  this;

        exit(0);

    }

}

4.

gamewidget.h中的代码

#ifndef GAMEWIDGET_H#define GAMEWIDGET_H

#include <QWidget>#include<QIcon>#include<QPalette>#include<QBrush>#include<QPixmap>#include<QPushButton>#include<QMessageBox>#include<QPaintEvent>#include<QPainter>#include<QLabel>#include<QTimer>#include<QTime>#include<ctime>#include<QButtonGroup>#include<QKeyEvent>#define longnumber 20//宏定义游戏界面方格的多少#define widthnumber 16

class Gamewidget : public QWidget

{

    Q_OBJECTpublic:

    explicit Gamewidget(QWidget *parent = 0);

    void paintEvent(QPaintEvent*);

    void keyPressEvent(QKeyEvent *e);

    QPushButton *upPush;

    QPushButton *downPush;

    QPushButton *leftPush;

    QPushButton *rightPush;

    QPushButton *StartPush;

    QPushButton *ExitPush;

    QLabel *ScoreLabel;

    QLabel *LevelLabel;

    QLabel *ScoreLabelNumber;

    QLabel *LevelLabelNumber;

    QTimer *Timer;

    int snake[200][2];//创建了一个包含蛇的身体坐标的数组

    int snake1[200][2];//这个数组只是上一个坐标的复制,在最后一步(咬到身体或者撞到边缘)需要将它的身体坐标还原

    static  int m_setDiretion;//对方向的更改的存储

    QButtonGroup *buttonGroup;//上下左右四个键构成一个按钮组,根据它们的返回值改变方向的值

    int foodx;//食物的X坐标

    int foody;//食物的Y坐标

    int bodycount;

    bool Nobite;

    int Score;

    int Difficulty;//难度级别,时间变化private:

    QPalette *palette;

 

signals:

public slots:

    void M_timeout();

    void M_startPush();

    void M_setDiretion(int index);

    void M_exitPush();

    void M_pausePush();

    void M_CheckGameOver();

};

#endif // GAMEWIDGET_H

5.gamewidget.cpp中的代码

#include "gamewidget.h"#include"widget.h"#include<QDebug>

int Gamewidget::m_setDiretion=4;

Gamewidget::Gamewidget(QWidget *parent) : QWidget(parent)

{

    qsrand(time(NULL));//随机数的种子

    Score=0;

    Difficulty=1;

    this->setAutoFillBackground(true);//设置父窗口背景可被覆盖填充

    this->resize(891,510);

    this->setWindowTitle("李子的贪吃蛇");

    this->setWindowIcon(QIcon(":/Picture/img/WindowPicture.jpg"));

 

    QPalette palette;

    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/new/Gamewindow/img/GameBack.jpg").scaled(this->size())));

    this->setPalette(palette);

 

 

    upPush =new QPushButton(

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值