• 5688阅读
  • 4回复

[讨论]qt写俄罗斯方块(求救) [复制链接]

上一主题 下一主题
离线neolyao
 
只看楼主 倒序阅读 楼主  发表于: 2012-05-20
block.h
  1. #ifndef BLOCK_
  2. #define BLOCK_
  3. #include<QtGui/QtGui>
  4. #define SIZE    40   //每个方格的单位宽度是20像素
  5. #define HEIGHT  15
  6. #define WIDTH   10  //定义游戏区的高度和宽度
  7. #define X_MIN   0    //最小x坐标
  8. #define X_MAX   10   //最大x坐标
  9. class BlockGame;
  10. class Block :public QObject
  11. {
  12.     Q_OBJECT
  13.        private:
  14.                 int id;  //方块的种类
  15.                 int x,y; //方块的坐标
  16.                 int dir; //方块的方向
  17.        public:
  18.                 bool putdown; //判断方块可否再下降
  19.        public:
  20.                 Block(QObject *parent=0);
  21.                 Block (const Block &obj);
  22.                 void OnRotate();  //翻转方块
  23.                 void OnLeft();    //方块左移
  24.                 void OnDown();    //方块下移
  25.                 void OnSink();    //方块沉底
  26.                 void OnRight();   //方块右移
  27.                 bool ChectBlock();  //检查方块是否可以移动
  28.                 void PutBlock();   //放置方块
  29.        friend class BlockGame;
  30. };
  31. #endif
block.cpp
  1. #include<QtGui/QtGui>
  2. #include"block.h"
  3. int GameArea[WIDTH][HEIGHT]={0};
  4. int g_Blocks[7][4]={
  5.     {0x0f00,0x4444,0x0f00,0x4444},//I
  6.     {0x0660,0x0660,0x0660,0x0660},//田
  7.     {0x4460,0x02e0,0x0622,0x0740},//L
  8.     {0x2260,0x0e20,0x0644,0x0470},//反L
  9.     {0x0c60,0x2640,0x0c60,0x2640},//Z
  10.     {0x0360,0x4620,0x0360,0x4620},//反Z
  11.     {0x4e00,0x4c40,0x0e40,0x4640} //T
  12.     };
  13. Block::Block(QObject *parent):QObject(parent)
  14. {
  15.     putdown=false;
  16. }
  17. Block::Block(const Block &obj)
  18. {
  19.     x=obj.x;
  20.     y=obj.y;
  21. }
  22. bool Block::ChectBlock()
  23. {
  24.     int X,Y,i;
  25.     int b=g_Blocks[id][dir];
  26.     for(i=0;i<16;i++)
  27.     {
  28.         if(b&0x8000)
  29.         {
  30.             X=x+i%4;
  31.             Y=y+i/4;
  32.             if(X<X_MIN||X>=X_MAX||GameArea[X][Y]==1||Y>=HEIGHT)
  33.                 return false;
  34.         }
  35.         b<<=1;
  36.     }
  37.    return true;
  38. }
  39. void Block::OnRotate()
  40. {
  41.     int dx;
  42.     Block temp=*this;
  43.     temp.dir=(temp.dir+1)%4;
  44.                 if(temp.ChectBlock()==true) {dx=0;  goto rotate;}
  45.     temp.x=x-1; if(temp.ChectBlock()==true) {dx=-1; goto rotate;}
  46.     temp.x=x+1; if(temp.ChectBlock()==true) {dx=1;  goto rotate;}
  47.     temp.x=x-2; if(temp.ChectBlock()==true) {dx=-2; goto rotate;}
  48.     temp.x=x+2; if(temp.ChectBlock()==true) {dx=2;  goto rotate;}
  49.     temp.x=x-3; if(temp.ChectBlock()==true) {dx=-3; goto rotate;}
  50.     temp.x=x+3; if(temp.ChectBlock()==true) {dx=3;  goto rotate;}
  51.     return;
  52.     rotate:
  53.            dir=temp.dir;
  54.            x+=dx;
  55. }
  56. void Block::OnLeft()
  57. {
  58.     Block temp=*this;
  59.     temp.x--;
  60.     if(temp.ChectBlock()==true)
  61.     {
  62.         x--;
  63.     }
  64. }
  65. void Block::OnRight()
  66. {
  67.     Block temp=*this;
  68.     temp.x++;
  69.     if(temp.ChectBlock()==true)
  70.     {
  71.         x++;
  72.     }
  73. }
  74. void Block::OnDown()
  75. {
  76.     Block temp=*this;
  77.     temp.y++;
  78.     if(temp.ChectBlock()==true)
  79.     {
  80.         y++;
  81.     }
  82.     else
  83.     {
  84.         PutBlock();
  85.         putdown=true;
  86.     }
  87. }
  88. void Block::PutBlock()
  89. {
  90.     int X,Y,i;
  91.     int b=g_Blocks[id][dir];
  92.     for(i=0;i<16;i++)
  93.     {
  94.         if(b&0x8000)
  95.         {
  96.             X=x+i%4;
  97.             Y=y+i/4;
  98.             GameArea[X][Y]=1;
  99.         }
  100.         b<<=1;
  101.     }
  102. }
  103. void Block::OnSink()
  104. {
  105.     Block temp=*this;
  106.     temp.y++;
  107.     while(temp.ChectBlock()==true)
  108.     {
  109.         y++;
  110.         temp.y++;
  111.     }
  112.     PutBlock();
  113.     putdown=true;
  114. }
blockgame.h
  1. #ifndef BLOCKGAME_
  2. #define BLOCKGAME_
  3. #include <QtGui/QtGui>
  4. #include "block.h"
  5. class BlockGame:public QWidget
  6. {
  7.     Q_OBJECT
  8.     private:
  9.          enum CTRL{ctrl_down,ctrl_up,ctrl_left,ctrl_right,ctrl_default};
  10.          Block g_CurBlock,g_NextBlock;
  11.          CTRL ctrl;
  12.          bool start;
  13.     public:
  14.             BlockGame(QWidget *parent=0);
  15.             void InitGame();   //初始化游戏界面
  16.             void NewGame();     // 新游戏,产生新方块
  17.             void DispatchMessage(); //翻译按键消息
  18.             void Draw_CurBlock(); //绘制当前方块
  19.             void Draw_NextBlock();  //绘制下一个方块
  20.             void NewBlock();     //产生新的方块
  21.             void RenewGameArea(); //更新游戏区,显示已落下的方块
  22.             void EraseBlock();   //擦除要消掉的方块
  23.     protected:
  24.             void paintEvent(QPaintEvent *paintevent);
  25.             void timerEvent(QTimerEvent *timerevent);
  26.             void keyPressEvent(QKeyEvent *keyevent);
  27. };
  28. #endif
blockgame.cpp
  1. #include<QtGui/QtGui>
  2. #include"blockgame.h"
  3. extern int g_Blocks[7][4];
  4. extern int GameArea[10][15];
  5. BlockGame::BlockGame(QWidget *parent):QWidget(parent)
  6. {
  7.     start=true;
  8.     ctrl=ctrl_default;
  9.     resize(630,600);
  10.     startTimer(500);
  11.     NewGame();
  12. }
  13. void BlockGame::NewBlock()
  14. {
  15.     g_CurBlock.id=g_NextBlock.id;
  16.     g_CurBlock.dir=g_NextBlock.dir;
  17.     g_CurBlock.x=X_MIN+2;
  18.     g_CurBlock.y=0;
  19.     g_NextBlock.id=qrand()%7;
  20.     g_NextBlock.dir=qrand()%4;
  21. }
  22. void BlockGame::NewGame()
  23. {
  24.     qsrand(time(NULL));
  25.     g_NextBlock.id=qrand()%7;
  26.     g_NextBlock.dir=qrand()%4;
  27.     g_NextBlock.x=X_MAX+1;
  28.     g_NextBlock.y=1;
  29.     NewBlock();
  30. }
  31. void BlockGame::Draw_CurBlock()
  32. {
  33.     int x,y,i;
  34.     int X=g_CurBlock.x,Y=g_CurBlock.y;
  35.     int block=g_Blocks[g_CurBlock.id][g_CurBlock.dir];
  36.     QPainter painter(this);
  37.     QBrush   brush(QColor(255,10,110));
  38.     painter.setBrush(brush);
  39.     for(i=0;i<16;i++)
  40.     {
  41.         if(block&0x8000)
  42.         {
  43.             x=X+i%4;
  44.             y=Y+i/4;
  45.             painter.drawRect(x*SIZE,y*SIZE,SIZE,SIZE);
  46.         }
  47.         block<<=1;
  48.     }
  49. }
  50. void BlockGame::Draw_NextBlock()
  51. {
  52.     int x,y,i;
  53.     int X=g_NextBlock.x,Y=g_NextBlock.y;
  54.     int block=g_Blocks[g_NextBlock.id][g_NextBlock.dir];
  55.     QPainter painter(this);
  56.     QBrush   brush(QColor(120,200,110));
  57.     painter.setBrush(brush);
  58.     for(i=0;i<16;i++)
  59.     {
  60.         if(block&0x8000)
  61.         {
  62.             x=X+i%4;
  63.             y=Y+i/4;
  64.             painter.drawRect(x*SIZE,y*SIZE,SIZE,SIZE);
  65.         }
  66.         block<<=1;
  67.     }
  68. }
  69. void BlockGame::InitGame()
  70. {
  71.     QPainter painter(this);
  72.     QPen pen1(Qt::blue,3);
  73.     QBrush brush(QColor(110,10,30));
  74.     QRect rect1(0,0,400,600),rect2(410,0,210,220),rect3(410,250,210,340);
  75.     QImage image(tr("bg.jpg"));
  76.     painter.drawImage(rect1,image);
  77.     painter.setPen(pen1);
  78.     painter.drawRect(rect1);
  79.     painter.setBrush(brush);
  80.     painter.drawRect(rect2);
  81.     painter.drawRect(rect3);
  82.     QPen pen2(Qt::black,1);
  83.     painter.setPen(pen2);
  84.     for(int j=0;j<HEIGHT;j++)
  85.         painter.drawLine(0,j*SIZE,WIDTH*SIZE,j*SIZE);
  86.     for(int j=0;j<WIDTH;j++)
  87.         painter.drawLine(j*SIZE,0,j*SIZE,HEIGHT*SIZE);
  88.     painter.drawText(rect3,tr("WELCOME TO PLAYING BLOCK GAME!"));
  89. }
  90. void BlockGame::DispatchMessage()
  91. {
  92.     switch(ctrl)
  93.     {
  94.         case ctrl_up:g_CurBlock.OnRotate();break;
  95.         case ctrl_down:g_CurBlock.OnSink();break;
  96.         case ctrl_left:g_CurBlock.OnLeft();break;
  97.         case ctrl_right:g_CurBlock.OnRight();break;
  98.         default:g_CurBlock.OnDown();break;
  99.     }
  100.     ctrl=ctrl_default;
  101. }
  102. void BlockGame::paintEvent(QPaintEvent *paintevent)
  103. {
  104.   InitGame();
  105.   RenewGameArea();
  106.   Draw_CurBlock();
  107.   Draw_NextBlock();
  108. }
  109. void BlockGame::timerEvent(QTimerEvent *timerevent)
  110. {
  111.     DispatchMessage();
  112.     if(g_CurBlock.putdown==true)
  113.     {
  114.         EraseBlock();
  115.         NewBlock();
  116.         g_CurBlock.putdown=false;
  117.     }
  118.     update();
  119. }
  120. void BlockGame::keyPressEvent(QKeyEvent *keyevent)
  121. {
  122.     switch(keyevent->key())
  123.     {
  124.         case Qt::Key_Up:ctrl=ctrl_up;break;
  125.         case Qt::Key_Down:ctrl=ctrl_down;break;
  126.         case Qt::Key_Left:ctrl=ctrl_left;break;
  127.         case Qt::Key_Right:ctrl=ctrl_right;break;
  128.         default:ctrl=ctrl_default;break;
  129.     }
  130. }
  131. void BlockGame::EraseBlock()
  132. {
  133.     int i,j,k,n,Y;
  134.     int row[4]={0};
  135.     bool bRow=false;
  136.     Y=g_CurBlock.y;
  137.     for(j=Y;j<Y+4;j++)
  138.     {
  139.         n=0;
  140.         for(i=0;i<X_MAX;i++)
  141.         {
  142.             if(GameArea[i][j]==1)
  143.                 n++;
  144.         }
  145.         if(n==WIDTH)
  146.         {
  147.             bRow=true;
  148.             row[j-Y]=1;
  149.         }
  150.     }
  151.     if(bRow==true)      {
  152.     for(k=0;k<4;k++)    {
  153.     if(row[k]==1)       {
  154.     for(j=Y+k;j>0;j--)
  155.     for(i=0;i<X_MAX;i++){
  156.     GameArea[i][j]=GameArea[i][j-1];
  157.     GameArea[i][j-1]=0;
  158.     }
  159.     }
  160.     }
  161.     }
  162. }
  163. void BlockGame::RenewGameArea()
  164. {
  165.      int i,j;
  166.      QPainter painter(this);
  167.      QBrush brush(QColor(100,120,155));
  168.      QRect rect;
  169.      painter.setBrush(brush);
  170.      for(j=0;j<HEIGHT;j++)
  171.          for(i=0;i<X_MAX;i++)
  172.          {
  173.              rect.setRect(i*SIZE,j*SIZE,SIZE,SIZE);
  174.              if(GameArea[i][j]==1) painter.drawRect(rect);
  175.          }
  176. }

main.cpp
  1. #include<QtGui/QtGui>
  2. #include"block.h"
  3. #include"blockgame.h"
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     BlockGame mygame;
  8.     mygame.show();
  9.     return a.exec();
  10. }
这个程序运行几秒钟就会异常结束,不知到怎么回事,请高手帮忙看看!!!



离线neolyao
只看该作者 1楼 发表于: 2012-05-20
是段错误,不知到是哪里的问题,坐等有缘人!
离线roywillow

只看该作者 2楼 发表于: 2012-05-20
回 1楼(neolyao) 的帖子
您一口气发这么多代码谁有这个精力一点一点看啊
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
离线453413516

只看该作者 3楼 发表于: 2012-05-20
看到了一半,看的有点头晕,与你无缘啊
离线neolyao
只看该作者 4楼 发表于: 2012-05-20
自己已经解决了,原来是数组越界了。
快速回复
限100 字节
 
上一个 下一个