08
2014
06

qtday03 简单版的贪吃蛇游戏

//snakegame.h

#ifndef SNAKEGAME_H
#define SNAKEGAME_H
#include<QList>
#include<QtWidgets/QLabel>
#include<QtWidgets/QDialog>
#include<QKeyEvent>
#include<QTimer>
/*枚举,表示方向*/
enum Direction{D_UP,D_DOWN,D_LEFT,D_RIGHT};
class SnakeGame:public QDialog
{
    Q_OBJECT
public:
    SnakeGame() ;
    ~SnakeGame();
public slots:
    void snakemove();/*移动*/
public:
    QLabel* getfood();/*产生新食物*/
    void keyPressEvent(QKeyEvent *key);
private:
    QList<QLabel*> snake;/*蛇*/
    QLabel * food;/*食物*/
    Direction dire;/*方向*/
    int foodlen;/*食物长度*/
    int maxlen;/*最大长度*/
    QTimer * timer;/*定时器*/
};
#endif // SNAKEGAME_H


//snakegame.cpp

#include "snakegame.h"
#include<QTime>
#include<QMessageBox>
SnakeGame::SnakeGame(){
this->resize(760,600);
    foodlen=40;/*食物的高/宽*/
    qsrand(QTime::currentTime().msec());
    timer=new QTimer(this);
    timer->setInterval(300);
    timer->start();
    snake.push_back(getfood());
    food=getfood();
    maxlen=5;
    dire=D_RIGHT;
connect(timer,SIGNAL(timeout()),this,SLOT(snakemove()));
}
SnakeGame::~SnakeGame(){
    delete timer;
    delete food;
    for(int i=0;i<snake.length();i++){
        delete snake[i];
    }
}
void SnakeGame::snakemove(){
    int x=snake[0]->x();
    int y=snake[0]->y();
    bool isover=false;/*自己碰到自己,结束*/
    for(int i=1;i<snake.length();i++){
        if(snake[0]->x()==snake[i]->x() && snake[0]->y()==snake[i]->y()){
            isover=true;
            break;
        }
    }
    if(snake.size()>=maxlen){
        QMessageBox msg(this);
        msg.setText("Continue ?");
        msg.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
        if(msg.exec()==QMessageBox::No)
        {
        this->close();
        }
        else{
            maxlen=maxlen*2+1;
        }
    }
    else  if(x<0|| y<0 || x>this->width()|| y>this->width()|| isover==true){
        QMessageBox msg(this);
        msg.setText("Game over!");
        msg.exec();
        this->close();
    }
    if(x==food->x()&& y==food->y()){
        snake.push_back(food);
        food=getfood();
    }
    /*把后面的移动一步,*/
    for(int i=snake.length()-1;i>=1;i--){
        snake[i]->move(snake[i-1]->x(),snake[i-1]->y());
    }
    switch (dire) {
    case D_UP:
        y=y-foodlen;
        break;
    case D_DOWN:
        y=y+foodlen;
        break;
    case D_LEFT:
        x=x-foodlen;
        break;
    case D_RIGHT:
        x=x+foodlen;
        break;
    default:
        break;
    }
    /*把头移动一步*/
    snake[0]->move(x,y);
}
QLabel* SnakeGame::getfood(){
    food=new QLabel(this);
    food->resize(foodlen,foodlen);
    food->setAutoFillBackground(true);
    /*随机颜色*/
    //qDebug(QString::number(i).toStdString());
    food->setPalette(QPalette(QColor(qrand()%256,qrand()%256,qrand()%256)));
    int w=this->width()-foodlen;
    int h=this->height()-foodlen;
    food->move((qrand()%w)/foodlen*foodlen,
               (qrand()%h/foodlen*foodlen));
    food->show();
    return food;
}
void SnakeGame::keyPressEvent(QKeyEvent *key){
    switch (key->key()) {
    case Qt::Key_Left:
        if(dire!=D_RIGHT)  dire=D_LEFT;
        break;
    case Qt::Key_Right:
        if(dire!=D_LEFT)    dire=D_RIGHT;
        break;
    case Qt::Key_Up:
        if(dire!=D_DOWN)   dire=D_UP;
        break;
    case Qt::Key_Down:
        if(dire!=D_UP)   dire=D_DOWN;
        break;
    default:
        break;
    }
}



//main.cpp

#include<QApplication>
#include "snakegame.h"
int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    SnakeGame sg;
    sg.show();
    return app.exec();
}




版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。