Qt 贪吃蛇小游戏

简单的实现了走和变大的样子,剩下的还在完善

贴代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPainter>
#include <QRect>
#include <QMessageBox>
#include <QString>
#include <QDebug>

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3




static int rect_width = 10;
static int COL = 0;
static int ROW = 0;


MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow),m_foodCount(0),m_currentGrade(0),m_currentSpeed(500)
{
    ui->setupUi(this);
    this->setWindowTitle("Snake");              //set window title "Snake"
    ui->widget->installEventFilter(this);       //install eventfilter
    COL =ui->widget->width()/rect_width;
    ROW =ui->widget->height()/rect_width;
    init();
}

MainWindow::~MainWindow()
{
    timer->stop();
    delete ui;
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)                   //eventfilter
{
    if(watched == ui->widget && event->type() == QEvent::Paint )
    {
        painter_rects();
//        return true;
    }
    else
    {
        return 0;
    }

    return QWidget::eventFilter(watched,event);
}

void MainWindow::creatFood()
{
    foodPosition[0] = qrand()%COL;
    foodPosition[1] = qrand()%ROW;
    for(int bodyId=0;bodyId<m_foodCount;bodyId++)
    {
        if(foodPosition[0] == snake[bodyId][0]&&foodPosition[1] == snake[bodyId][1])
        {
            foodPosition[0] = qrand()%COL-1;
            foodPosition[1] = qrand()%ROW-1;
        }
    }
}

void MainWindow::drawSnakeHead()
{
    snake[0][0] = qrand()%COL;
    snake[0][1] = qrand()%ROW;

    m_direction = qrand()%4;
}

void MainWindow::init()
{
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slot_Timerout()));
    timer->start(m_currentSpeed);
    creatFood();
    drawSnakeHead();
}

bool MainWindow::dealGuoJie()
{
    if(snake[0][0]>(COL-1)|| snake[0][0]<0 || snake[0][1]>(ROW-1)|| snake[0][1]<0)
    {
        gameOver();
        return true;
    }
    for(int i =1;i<m_foodCount;++i)
    {
        if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
        {
            gameOver();
            return true;
        }
    }

    return false;
}

void MainWindow::gameOver()
{
    QMessageBox::information(this,QString("敬告"),QString("游戏结束"));
}

void MainWindow::flashgrade()
{
    ui->label_grade->setText(QString::number(m_currentGrade,10));
}

void MainWindow::snakeRun()
{
    if(dealGuoJie())
    {
        return;
    }
    checkEatFood();
    update();
}

void MainWindow::snakeHeadRun()
{
    switch (m_direction) {
    case UP:
        --snake[0][1];
        break;
    case DOWN:
        ++snake[0][1];
        break;
    case LEFT:
        --snake[0][0];
        break;
    case RIGHT:
        ++snake[0][0];
        break;
    default:
        break;
    }
}

void MainWindow::checkEatFood()
{
    if(snake[0][0] == foodPosition[0] && snake[0][1] == foodPosition[1])
    {
        ++m_foodCount;
        ungrade();
        flashgrade();
        creatFood();
    }
    for(int snId = m_foodCount;snId>0;--snId)
    {
        snake[snId][0] = snake[snId-1][0];
        snake[snId][1] = snake[snId-1][1];
    }
    snakeHeadRun();
}

void MainWindow::ungrade()
{

}

void MainWindow::painter_rects()
{
    QPainter painer(ui->widget);
    /*
     * draw the rects
     */
    for(int x=0;x<COL;x++)
    {
        for(int y=0;y<ROW;y++)
        {
            painer.drawRect(QRect(x*rect_width,y*rect_width,rect_width,rect_width));
        }
    }
    /*
     * draw food
     */
    painer.fillRect(QRect(foodPosition[0]*rect_width,foodPosition[1]*rect_width,rect_width,rect_width),Qt::green);
    /*
     * draw snake head
     */
    painer.fillRect(QRect(snake[0][0]*rect_width,snake[0][1]*rect_width,rect_width,rect_width),Qt::red);
    /*
     * draw snake body
     */
    for(int snakebody=1;snakebody<m_foodCount;snakebody++)
    {
        painer.fillRect(QRect(snake[snakebody][0]*rect_width,snake[snakebody][1]*rect_width,rect_width,rect_width),Qt::black);
    }

}

void MainWindow::on_pushButton_up_clicked()
{
    m_direction = UP;
    qDebug()<<"up active";
}

void MainWindow::on_pushButton_left_clicked()
{
    m_direction = LEFT;
    qDebug()<<"left active";
}

void MainWindow::on_pushButton_right_clicked()
{
    m_direction = RIGHT;
    qDebug()<<"right active";
}

void MainWindow::on_pushButton_down_clicked()
{
    m_direction = DOWN;
    qDebug()<<"down active";
}

void MainWindow::slot_Timerout()
{
    snakeRun();
    qDebug()<<"timrt is Run";
}



原文地址:https://www.cnblogs.com/DreamDog/p/9160066.html