Qt简单版贪吃?游戏

时间:2023-02-18 19:00:33
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
resize(480,500);
//设置窗体背景色为黑色
setStyleSheet("QWidget{background:black}");
setWindowOpacity(0.8);//设置窗口的透明度

snake.append(QRectF(200,500,snakeNodeWidth,snakeNodeHeight));
addTopRectF();
addTopRectF();
//首先生成一个奖励节点
rewardNode.append(QRectF(100,100,snakeNodeWidth,snakeNodeWidth));

timer = new QTimer;
connect(timer, SIGNAL(timeout()),this,SLOT(timeOut()));
//timer->start(time);

rewardTimer = new QTimer;
connect(rewardTimer,SIGNAL(timeout()),this,SLOT(rewardTimeOut()));

}
//向上移动
void Widget::addTopRectF()
{
if(snake.at(0).y()-snakeNodeHeight < 0){
snake.insert(0,QRectF(QPointF(snake.at(0).x(),this->height()-snakeNodeHeight),
QPointF(snake.at(0).x()+snakeNodeWidth,this->height())));
}else{
snake.insert(0,QRectF(snake.at(0).topLeft()+QPointF(0,-snakeNodeHeight),snake.at(0).topRight()));
}
}
//向下移动
void Widget::addDownRectF()
{
if(snake.at(0).y()+snakeNodeHeight*2 > this->height()){
snake.insert(0,QRectF(QPointF(snake.at(0).x(),snakeNodeHeight),
QPointF(snake.at(0).x()+snakeNodeWidth,0)));
}else{
snake.insert(0,QRectF(snake.at(0).bottomLeft(),snake.at(0).bottomRight()+QPointF(0,snakeNodeHeight)));
}
}
//向左移动
void Widget::addLeftRectF()
{
if(snake.at(0).x()-snakeNodeWidth < 0){
snake.insert(0,QRectF(QPointF(this->width()-snakeNodeWidth,snake.at(0).y()),
QPointF(this->width(),snake.at(0).y()+snakeNodeHeight)));
}else{
snake.insert(0,QRectF(snake.at(0).topLeft()+QPointF(-snakeNodeWidth,0),snake.at(0).bottomLeft()));
}
}
//向右移动
void Widget::addRightRectF()
{
if(snake.at(0).x()+snakeNodeWidth*2 > this->width()){
snake.insert(0,QRectF(QPointF(0,snake.at(0).y()),
QPointF(snakeNodeWidth,snake.at(0).y()+snakeNodeHeight)));
}else{
snake.insert(0,QRectF(snake.at(0).topRight(),snake.at(0).bottomRight()+QPointF(snakeNodeWidth,0)));
}
}
//删除结尾数据
void Widget::deleteLastRectF()
{
snake.removeLast();

}

Qt简单版贪吃?游戏