学Qt一个月了,写了超级简陋俄罗斯方块,求鼓励!
代码:
Block.h
#ifndef BLOCK_#define BLOCK_#include<QtGui/QtGui>#define SIZE 40 //每个方格的单位宽度是20像素#define HEIGHT 15#define WIDTH 10 //定义游戏区的高度和宽度#define X_MIN 0 //最小x坐标#define X_MAX 10 //最大x坐标class BlockGame;class Block{ private: int id; //方块的种类 int x,y; //方块的坐标 int dir; //方块的方向 public: void OnRotate(); //翻转方块 void OnLeft(); //方块左移 void OnDown(); //方块下移 void OnSink(); //方块沉底 void OnRight(); //方块右移 bool ChectBlock(); //检查方块是否可以移动 void PutBlock(); //放置方块 friend class BlockGame;};#endif
#include<QtGui/QtGui>#include"Block.h"#include"BlockGame.h"Block g_CurBlock,g_NextBlock;bool PutDown=false;int GameArea[WIDTH][HEIGHT]={0};int g_Blocks[7][4]={ {0x0f00,0x4444,0x0f00,0x4444},//I {0x0660,0x0660,0x0660,0x0660},//田 {0x4460,0x02e0,0x0622,0x0740},//L {0x2260,0x0e20,0x0644,0x0470},//反L {0x0c60,0x2640,0x0c60,0x2640},//Z {0x0360,0x4620,0x0360,0x4620},//反Z {0x4e00,0x4c40,0x0e40,0x4640} //T };bool Block::ChectBlock(){ int X,Y,i; int b=g_Blocks[id][dir]; for(i=0;i<16;i++) { if(b&0x8000) { X=x+i%4; Y=y+i/4; if(X<X_MIN||X>=X_MAX||GameArea[X][Y]==1||Y>=HEIGHT) return false; } b<<=1; } return true;}void Block::OnRotate(){ int dx; Block temp=*this; temp.dir=(temp.dir+1)%4; if(temp.ChectBlock()==true) {dx=0; goto rotate;} temp.x=x-1; if(temp.ChectBlock()==true) {dx=-1; goto rotate;} temp.x=x+1; if(temp.ChectBlock()==true) {dx=1; goto rotate;} temp.x=x-2; if(temp.ChectBlock()==true) {dx=-2; goto rotate;} temp.x=x+2; if(temp.ChectBlock()==true) {dx=2; goto rotate;} temp.x=x-3; if(temp.ChectBlock()==true) {dx=-3; goto rotate;} temp.x=x+3; if(temp.ChectBlock()==true) {dx=3; goto rotate;} return; rotate: dir=temp.dir; x+=dx;}void Block::OnLeft(){ Block temp=*this; temp.x--; if(temp.ChectBlock()==true) { x--; }}void Block::OnRight(){ Block temp=*this; temp.x++; if(temp.ChectBlock()==true) { x++; }}void Block::OnDown(){ Block temp=*this; temp.y++; if(temp.ChectBlock()==true) { y++; } else { PutBlock(); PutDown=true; }}void Block::PutBlock(){ int X,Y,i; int b=g_Blocks[id][dir]; for(i=0;i<16;i++) { if(b&0x8000) { X=x+i%4; Y=y+i/4; GameArea[X][Y]=1; } b<<=1; }}void Block::OnSink(){ Block temp=*this; temp.y++; while(temp.ChectBlock()==true) { y++; temp.y++; } PutBlock(); PutDown=true;}
#ifndef BLOCKGAME_#define BLOCKGAME_#include<QtGui/QtGui>enum CTRL{ctrl_down,ctrl_up,ctrl_left,ctrl_right,ctrl_default};class BlockGame:public QWidget{ Q_OBJECT private: CTRL ctrl; bool start; public: BlockGame(QWidget *parent=0); void InitGame(); //初始化游戏界面 void NewGame(); // 新游戏,产生新方块 void DispatchMessage(); //翻译按键消息 void Draw_CurBlock(); //绘制当前方块 void Draw_NextBlock(); //绘制下一个方块 void NewBlock(); //产生新的方块 void RenewGameArea(); //更新游戏区,显示已落下的方块 void EraseBlock(); //擦除要消掉的方块 protected: void paintEvent(QPaintEvent *paintevent); void timerEvent(QTimerEvent *timerevent); void keyPressEvent(QKeyEvent *keyevent);};#endif
#include<QtGui/QtGui>#include"BlockGame.h"#include"Block.h"extern Block g_CurBlock;extern Block g_NextBlock;extern int g_Blocks[7][4];extern int GameArea[10][15];extern bool PutDown;//判断方块可否再下降BlockGame::BlockGame(QWidget *parent):QWidget(parent){ start=true; ctrl=ctrl_default; resize(630,600); startTimer(500); NewGame();}void BlockGame::NewBlock(){ g_CurBlock.id=g_NextBlock.id; g_CurBlock.dir=g_NextBlock.dir; g_CurBlock.x=X_MIN+2; g_CurBlock.y=0; g_NextBlock.id=qrand()%7; g_NextBlock.dir=qrand()%4;}void BlockGame::NewGame(){ qsrand(2); g_NextBlock.id=qrand()%7; g_NextBlock.dir=qrand()%4; g_NextBlock.x=X_MAX+1; g_NextBlock.y=1; NewBlock();}void BlockGame::Draw_CurBlock(){ int x,y,i; int X=g_CurBlock.x,Y=g_CurBlock.y; int block=g_Blocks[g_CurBlock.id][g_CurBlock.dir]; QPainter painter(this); QBrush brush(QColor(255,10,110)); painter.setBrush(brush); for(i=0;i<16;i++) { if(block&0x8000) { x=X+i%4; y=Y+i/4; painter.drawRect(x*SIZE,y*SIZE,SIZE,SIZE); } block<<=1; }}void BlockGame::Draw_NextBlock(){ int x,y,i; int X=g_NextBlock.x,Y=g_NextBlock.y; int block=g_Blocks[g_NextBlock.id][g_NextBlock.dir]; QPainter painter(this); QBrush brush(QColor(120,200,110)); painter.setBrush(brush); for(i=0;i<16;i++) { if(block&0x8000) { x=X+i%4; y=Y+i/4; painter.drawRect(x*SIZE,y*SIZE,SIZE,SIZE); } block<<=1; }}void BlockGame::InitGame(){ QPainter painter(this); QPen pen1(Qt::blue,3); QBrush brush(QColor(110,10,30)); QRect rect1(0,0,400,600),rect2(410,0,210,220),rect3(410,250,210,340); QImage image(tr("bg.jpg")); painter.drawImage(rect1,image); painter.setPen(pen1); painter.drawRect(rect1); painter.setBrush(brush); painter.drawRect(rect2); painter.drawRect(rect3); QPen pen2(Qt::black,1); painter.setPen(pen2); for(int j=0;j<HEIGHT;j++) painter.drawLine(0,j*SIZE,WIDTH*SIZE,j*SIZE); for(int j=0;j<WIDTH;j++) painter.drawLine(j*SIZE,0,j*SIZE,HEIGHT*SIZE); painter.drawText(rect3,tr("WELCOME TO PLAYING BLOCK GAME!"));}void BlockGame::DispatchMessage(){ switch(ctrl) { case ctrl_up:g_CurBlock.OnRotate();break; case ctrl_down:g_CurBlock.OnSink();break; case ctrl_left:g_CurBlock.OnLeft();break; case ctrl_right:g_CurBlock.OnRight();break; default:g_CurBlock.OnDown();break; } ctrl=ctrl_default;}void BlockGame::paintEvent(QPaintEvent *paintevent){ InitGame(); RenewGameArea(); Draw_CurBlock(); Draw_NextBlock();}void BlockGame::timerEvent(QTimerEvent *timerevent){ DispatchMessage(); if(PutDown==true) { EraseBlock(); NewBlock(); PutDown=false; } update();}void BlockGame::keyPressEvent(QKeyEvent *keyevent){ switch(keyevent->key()) { case Qt::Key_Up:ctrl=ctrl_up;break; case Qt::Key_Down:ctrl=ctrl_down;break; case Qt::Key_Left:ctrl=ctrl_left;break; case Qt::Key_Right:ctrl=ctrl_right;break; default:ctrl=ctrl_default;break; }}void BlockGame::EraseBlock(){ int i,j,k,n,Y; int row[4]={0}; bool bRow=false; Y=g_CurBlock.y; for(j=Y;j<Y+4;j++) { n=0; for(i=0;i<X_MAX;i++) { if(GameArea[i][j]==1) n++; } if(n==WIDTH) { bRow=true; row[j-Y]=1; } } if(bRow==true) { for(k=0;k<4;k++) { if(row[k]==1) { for(j=Y+k;j>0;j--) for(i=0;i<X_MAX;i++) { GameArea[i][j]=GameArea[i][j-1]; GameArea[i][j-1]=0; } } } }}void BlockGame::RenewGameArea() { int i,j; QPainter painter(this); QBrush brush(QColor(100,120,155)); QRect rect; painter.setBrush(brush); for(j=0;j<HEIGHT;j++) for(i=0;i<X_MAX;i++) { rect.setRect(i*SIZE,j*SIZE,SIZE,SIZE); if(GameArea[i][j]==1) painter.drawRect(rect); }}
#include<QtGui/QtGui>#include"Block.h"#include"BlockGame.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); BlockGame mygame; mygame.show(); return a.exec();}