Qt 贪吃蛇小游戏

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

 

贴代码

 

#include "mainwindow.h"
#include "ui_mainwindow.h"#include 
#include 
#include 
#include 
#include #define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3static 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 eventfilterCOL =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;bodyIdstart(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;ilabel_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

 

 

 

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部