Qt版双人俄罗斯方块游戏

发布时间:2025-12-09 18:18:07 浏览次数:4

Qt版双人俄罗斯方块游戏 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907
    之前写过《VC版双人俄罗斯方块》,将其在Qt下又重写了一遍,核心的算法还是采用VC下曾经用过的C++源码,直接用的是当时封装好的类,写在一个文件中,所以直接拿来用。不同的仍然是与显示有关的东西需要采用Qt下的显示方式。 以下是Qt版运行截图:


先是按键响应槽的实现代码: void Tetris::keyPressEvent(QKeyEvent *event){if(event->key() == Qt::Key_0)onPause();if(event->key() == Qt::Key_Right)activeBrick->shiftRight(bin);else if(event->key() == Qt::Key_Left)activeBrick->shiftLeft(bin);else if(event->key() == Qt::Key_Up)activeBrick->rotateClockwise(bin);else if(event->key() == Qt::Key_Down)activeBrick->shiftDown(bin);if(event->key() == Qt::Key_6)activeBrick2->shiftRight(bin2);else if(event->key() == Qt::Key_4)activeBrick2->shiftLeft(bin2);else if(event->key() == Qt::Key_8)activeBrick2->rotateClockwise(bin2);else if(event->key() == Qt::Key_5)activeBrick2->shiftDown(bin2);}
之后还有就是绘制槽的实现代码: void Tetris::paintEvent(QPaintEvent *event){QPainter painter(this);unsigned int width,i,j;unsigned int height,width1,height1;unsigned int height2,width2,height3,width3;width=bin->getWidth(); //左边主显示面板宽度与高度的获取height=bin->getHeight();width1=bin1->getWidth();//左边预测面板的宽度与高度的获取height1=bin1->getHeight();width2=bin2->getWidth();//右边主显示面板宽度与高度的获取height2=bin2->getHeight();width3=bin3->getWidth();//右边预测面板的宽度与高度的获取height3=bin3->getHeight();int nSize=20;QRect rect(0,0,860,500);QImage imageBack;imageBack.load(":/images/background.png");QBrush brushBack(imageBack);painter.setBrush(brushBack);painter.drawRect(rect);//右边玩家分数的输出QRect re;//pDC->FillSolidRect(re,RGB(210,255,255));char buf[100];// sprintf(buf," %d ",numLines*10);// pDC->TextOut(330,90,buf);ui->label_scorel->setText(QString::number(numLines*10));ui->label_scorel_2->setText(QString::number(numLines1*10));//左边玩家分数的输出QRect re1;// pDC->FillSolidRect(re1,RGB(230,255,100));// char buf1[100];if(GameModal==2){// sprintf(buf1," %d ",numLines1*10);// pDC->TextOut(440,90,buf1);}QRect rc,rc1,rc2,rc3;QString str[] = {":/images/blue.png",":/images/red.png",":/images/green.png",":/images/blue.png",":/images/ya.png",":/images/yello.png"":/images/green.png",":/images/green.png"};int b,b1,b2;for(i=0;i<height1;i++){for(j=0;j<width1;j++){rc1=QRect(j*nSize+300,i*nSize,nSize,nSize);//矩形的区域if(image1[i][j]!=0){b=image1[i][j];// qDebug()<<str[b];QImage img;img.load(":/images/red.png");// img.load(str[b]);QBrush brush(img);painter.setBrush(brush);painter.fillRect(rc1,brush);// painter.setBrush(Qt::green);// painter.drawRect(rc1);}}}for(i=0;i<height;i++){for(j=0;j<width;j++){rc=QRect(j*nSize+80,i*nSize,nSize,nSize);if(image[i][j]!=0){b=image[i][j]; //左边玩家主面板砖块的位图填充// qDebug()<<str[b];QImage img;img.load(":/images/red.png");// img.load(str[b]);QBrush brush(img);painter.setBrush(brush);painter.fillRect(rc,brush);}}}/if(GameModal==2){for(i=0;i<height3;i++){for(j=0;j<width3;j++){rc3=QRect(j*nSize+420,i*nSize,nSize,nSize);if(image3[i][j]!=0){b=image3[i][j];//右边玩家预测砖块的位图填充// qDebug()<<str[b];QImage img;img.load(":/images/green.png");// img.load(str[b]);QBrush brush(img);painter.setBrush(brush);painter.fillRect(rc3,brush);}}}for(i=0;i<height2;i++){for(j=0;j<width2;j++){rc2=QRect(j*nSize+540,i*nSize,nSize,nSize);if(image2[i][j]!=0){b=image2[i][j];//右边玩家主面板砖块的位图填充// qDebug()<<str[b];QImage img;img.load(":/images/green.png");// img.load(str[b]);QBrush brush(img);painter.setBrush(brush);painter.fillRect(rc2,brush);}}}}}其他的都和VC下的一样。 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907



需要做网站?需要网络推广?欢迎咨询客户经理 13272073477