基于QT5.4的C++贪吃蛇小游戏

版权声明:转载请注明出处 https://blog.csdn.net/qq_35294564/article/details/82924811

游戏核心代码:

void myWidget::timerEvent(QTimerEvent *e){//定时器
    if(e->timerId() == timerId && gamePause){
        if(direction == s_right){
            goRight();
        }else if(direction == s_up){
            goUp();
        }else if(direction == s_down){
            goDown();
        }else if(direction == s_left){
            goLeft();
        }
        //qDebug()<<score;
        if(isEated){
            strScore = QString::number(score);
            ui->label->setText(strScore);
        }
        update();

        if(crashed){
            player->stop();
            player_gg->play();
            Sleep(1000);
            MessageBox(0, TEXT("不要灰心!继续游戏??"), TEXT("Sorry,defeat!!"), 0);//游戏结束时
            gamePause = !gamePause;
            init();
            strScore = QString::number(score);
            ui->label->setText(strScore);
        }
    }
}

void myWidget::keyPressEvent(QKeyEvent *e){//键盘事件监听
   // qDebug()<<(char)e->key();
    if(gameStart){//游戏开始时才可以手动
        if(e->key() == Qt::Key_W){
            if(direction == s_down) return;//不做处理
            direction = s_up;
            //if(e->isAutoRepeat())//重复按下再快跑会有迟钝效果。。
            goUp();
            if(isEated)
                createApple();//随机产生苹果位置
        }else if(e->key() == Qt::Key_S){
            if(direction == s_up) return;//不做处理
            direction = s_down;
            goDown();
            if(isEated)
                createApple();//随机产生苹果位置
        }else if(e->key() == Qt::Key_A){
            if(direction == s_right) return;//不做处理
            direction = s_left;
            goLeft();
            if(isEated)
                createApple();//随机产生苹果位置
        }else if(e->key() == Qt::Key_D){
            if(direction == s_left) return;//不做处理
            direction = s_right;
            goRight();
            if(isEated)
                createApple();//随机产生苹果位置
        }

        strScore = QString::number(score);
        ui->label->setText(strScore);

        update();
    }

}


void myWidget::paintEvent(QPaintEvent *)
{
    //QPainter p(this);
    QPainter p;//创建画家对象
    p.begin(this);//指定当前窗口为绘图设备
    //绘图操作
    //p.drawPixmap(0,0,width(),height(),QPixmap("../image/bg.jpg"));//背景图
    if(bg)
        p.drawPixmap(rect(),QPixmap("../image/bg.jpg"));//背景图
    else{
        p.drawPixmap(rect(),QPixmap("../image/bg.png"));//背景图
    }

    //定义画笔
    QPen pen;
    pen.setWidth(6);
    pen.setColor(RGB(60,60,60));
    p.setPen(pen);//把画笔交给画家

   // p.drawRect(startX,startY,Cwidth,Cheight);//画出游戏边界

    for(int i=-1;i<=W;i++){//画出游戏边界
        p.drawPixmap(i*Bsize+startX,-1*Bsize+startY,Bsize,Bsize,QPixmap("../image/shitou.png"));
        p.drawPixmap(i*Bsize+startX,21*Bsize+startY,Bsize,Bsize,QPixmap("../image/shitou.png"));
    }
    for(int j=0;j<H;j++){//画出游戏边界
        p.drawPixmap(-1*Bsize+startX,j*Bsize+startY,Bsize,Bsize,QPixmap("../image/shitou.png"));
        p.drawPixmap(25*Bsize+startX,j*Bsize+startY,Bsize,Bsize,QPixmap("../image/shitou.png"));
    }

    if(Sround == 1){//画出第一关障碍物
        for(int i=4;i<=8;i++){
            p.drawPixmap(6*Bsize+startX,i*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
            p.drawPixmap(18*Bsize+startX,i*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
        }
        for(int i=11;i<=15;i++){
            p.drawPixmap(6*Bsize+startX,i*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
            p.drawPixmap(18*Bsize+startX,i*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
        }
        for(int i=7;i<=10;i++){
            p.drawPixmap(i*Bsize+startX,4*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
            p.drawPixmap(i*Bsize+startX,15*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
        }
        for(int i=14;i<=17;i++){
            p.drawPixmap(i*Bsize+startX,4*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
            p.drawPixmap(i*Bsize+startX,15*Bsize+startY,Bsize,Bsize,QPixmap("../image/wall.png"));
        }
    }

    /*pen.setWidth(1);//重新设置画笔宽度
    p.setPen(pen);//把画笔交给画家
    for(int i=startX,j=startY;j<startY+Cheight;j+=Bsize){//画格子辅助线
       p.drawLine(i,j,i+Cwidth,j);
    }
    for(int i=startX,j=startY;i<startX+Cwidth;i+=Bsize){
        p.drawLine(i,j,i,j+Cheight);
    }*/

    strScore = QString::number(score);//刷新分数
    ui->label->setText(strScore);

    if(appleX%2==0)//画苹果
        p.drawPixmap(posAppleX,posAppleY-2,appleW,appleH,QPixmap("../image/apple1.png"));
    else//画月饼
        p.drawPixmap(posAppleX,posAppleY,Bsize,Bsize,QPixmap("../image/shead.png"));
    if(isEated){
        createApple();//随机产生苹果位置
        update();
    }

    for(std::vector<Point>::iterator it=s.snakeBody.begin();it!=s.snakeBody.end();it++){//遍历蛇身,画蛇
        Smap[(*it).x()][(*it).y()] = 1;
        if(it == s.snakeBody.end()-1){//vector数组尾作为蛇头,红点
            headX = (*it).x();//蛇头坐标
            headY = (*it).y();//蛇头坐标
            int posHeadX = headX*Bsize+startX;//蛇头像素位置
            int posHeadY = headY*Bsize+startY;//蛇头像素位置
            if(direction == s_up){
                p.drawPixmap(posHeadX-10,posHeadY-15,2*Bsize,2*Bsize,QPixmap("../image/shead_up.png"));
            }else if(direction == s_down){
                p.drawPixmap(posHeadX-10,posHeadY-5,2*Bsize,2*Bsize,QPixmap("../image/shead_down.png"));
            }else if(direction == s_left){
                p.drawPixmap(posHeadX-15,posHeadY-10,2*Bsize,2*Bsize,QPixmap("../image/shead_left.png"));
            }else if(direction == s_right){
                p.drawPixmap(posHeadX-5,posHeadY-10,2*Bsize,2*Bsize,QPixmap("../image/shead_right.png"));
            }
        }else{//蛇身黑点
            int xx=(*it).x()*Bsize+startX;
            int yy=(*it).y()*Bsize+startY;
            p.drawPixmap(xx,yy,Bsize,Bsize,QPixmap("../image/snake1.png"));
        }
        if (headX == appleX && headY == appleY){//吃到了!
            isEated = true;
        }
    }


    p.end();
}


void init() {//每次游戏开始的数据初始化
    for (int i = 0; i < W; i++)
        for (int j = 0; j < H; j++)
                Smap[i][j] = 0;
    s.snakeBody.clear();
    direction = s_right;
    crashed = false;
    gameStart = false;
    score = 0;
    gameStartClickCount = 0;
    snakeInit();//初始化蛇身
    setRound();//设置第几关
    createApple();//随机产生苹果位置
}
void createApple() {//随机产生苹果位置
    if (isEated){
        player_eat->play();//吃到音效
        score += 10;
    }
    while (true) {
        srand((int)time(NULL));
        int row = rand() % 21;//产生0~24的随机数
        int col = rand() % 25;
        if (Smap[col][row] == 0) {
            //Smap[row][col] = 1;
            appleX = col;
            appleY = row;
            posAppleX = appleX*Bsize+startX;//像素位置与坐标转换
            posAppleY = appleY*Bsize+startY;
            break;
        }
    }
    isEated = false;//刚产生的苹果吃不到!
}

 向上走为例:

void goUp() {    //五种情况①撞墙;②撞苹果;③撞自己;④都不撞;⑤撞障碍物
    direction = s_up;
    if (!((headX == appleX) && (headY - 1 == appleY)) && (headY - 1 >= 0) && (Smap[headX][headY - 1] == 0)) {//都不撞(头插一点,尾删一点)
        s.snakeBody.push_back(Point(headX, headY - 1));//头插一点
        //Smap[headX][headY - 1] = 1;
        std::vector<Point>::iterator it = s.snakeBody.begin();//找到第一个点(尾部)
        Smap[(*it).x()][(*it).y()] = 0;
        s.snakeBody.erase(it);//尾删一点
    }
    else if ((headX == appleX) && (headY - 1 == appleY)) {//撞苹果,吃到(头插一点,尾不删)
        s.snakeBody.push_back(Point(headX, headY - 1));//头插一点
        //Smap[headX][headY - 1] = 1;
        isEated = true;
    }else if(Smap[headX][headY - 1]==1){
        crashed = true;
        return;
    }
    else if (headY - 1 < 0) {//不是撞苹果->撞墙:GameOver
        crashed = true;
        return;
    }
    else{//撞自己
        crashed = true;
        return;
    }
}

运行效果图: 

 完整代码及图片音频下载地址:https://download.csdn.net/download/qq_35294564/10699690

猜你喜欢

转载自blog.csdn.net/qq_35294564/article/details/82924811
今日推荐