?? dzk.ui.h
字號:
/****************************************************************************** ui.h extension file, included from the uic-generated form implementation.**** If you wish to add, delete or rename functions or slots use** Qt Designer which will update this file, preserving your code. Create an** init() function in place of a constructor, and a destroy() function in** place of a destructor.*****************************************************************************/void Dzk::init(){ gameOver=false; guan=1; scoreNum=0; bloodNum=3; blood->setText(QString::number(bloodNum)); myTimerId=0; timerTime=TIMER_TIME; wav=new WavePlay(); paiz->setGeometry( QRect(width()/2, height()-PAIZ_SIZE_TO_BUTTOM, PAIZ_WIDTH, PAIZ_HEIGHT )); initFall(); initGame(); paiz->installEventFilter(this);}// events**********************************************************************************bool Dzk::eventFilter(QObject *target,QEvent *event){ if(target==paiz){ if(event->type()==QEvent::KeyPress&&gameOver==false){ QKeyEvent *keyEvent=(QKeyEvent *)event; if(keyEvent->key()==Key_Right&&paiz->x()<width()-PAIZ_WIDTH){ paiz->setGeometry(paiz->x()+PAIZ_SPEED,paiz->y(),paiz->width(),PAIZ_HEIGHT); return true; }else if(keyEvent->key()==Key_Left && paiz->x()>0){ paiz->setGeometry(paiz->x()-PAIZ_SPEED,paiz->y(),paiz->width(),PAIZ_HEIGHT); return true; }else if(keyEvent->key()==Key_Up && paiz->y()>height()/2){ paiz->setGeometry(paiz->x(),paiz->y()-PAIZ_SPEED,paiz->width(),PAIZ_HEIGHT); return true; }else if(keyEvent->key()==Key_Down && paiz->y()<height()-PAIZ_SIZE_TO_BUTTOM){ paiz->setGeometry(paiz->x(),paiz->y()+PAIZ_SPEED,paiz->width(),PAIZ_HEIGHT); return true; }else if(keyEvent->key()==Key_Space){ begin=true; //let the ball jump } } } return QWidget::eventFilter(target,event); } void Dzk::showEvent(QShowEvent *){ myTimerId=startTimer(timerTime);}void Dzk::timerEvent(QTimerEvent *event){ if(event->timerId()==myTimerId&&gameOver==false){ mainLogic(); }}void Dzk::paintEvent( QPaintEvent * ){ // QPainter painter(this);}// logics***********************************************************************void Dzk::mainLogic(){ //游戲主邏輯 if(begin){ //i按下空格讓球彈起 ball->setGeometry(ball->x()-ballPoint[0].speed_x,ball->y()-ballPoint[0].speed_y,ball->width(),ball->height()); for(int i=1;i<6;i++){ (*(fallScores.find(i))).second->setGeometry(QRect((*(fallScores.find(i))).second->x(), (*(fallScores.find(i))).second->y()+FALL_SPEED,13,20 ) ); } }else{ //ball keep up with paiz ball->setGeometry(paiz->x()+PAIZ_WIDTH/2,paiz->y()-ball->height(),ball->width(),ball->height()); } for(int i=1;i<6;i++){ if(((*(fallScores.find(i))).second->x()>=paiz->x()-(*(fallScores.find(i))).second->width()/2) &&((*(fallScores.find(i))).second->x()<=paiz->x()+paiz->width()-(*(fallScores.find(i))).second->width()/2) &&(((*(fallScores.find(i))).second->y()>paiz->y()-(*(fallScores.find(i))).second->width()) &&((*(fallScores.find(i))).second->y()<paiz->y()+paiz->height()-(*(fallScores.find(i))).second->width()))){//fall和拍上壁相撞 switch(i) { case 1:// blue 9 scoreNum+=9; score->setText(QString::number(scoreNum)); sound(1);break; case 2://green <-> paiz->setGeometry(paiz->x(),paiz->y(),paiz->width()+50,PAIZ_HEIGHT); sound(2);break; case 3:// high blue 99 scoreNum+=99; score->setText(QString::number(scoreNum)); sound(3);break; case 4://black -><- paiz->setGeometry(paiz->x(),paiz->y(),paiz->width()-50,PAIZ_HEIGHT); sound(4);break; case 5://red +o bloodNum+=1; blood->setText(QString::number(bloodNum)); sound(5);break; } (*(fallScores.find(i))).second->setGeometry( QRect( 800, 800, 13,20 ) ); } } if(ball->x()<=0){//球和左壁相撞 ballPoint[0].speed_x=0-ballPoint[0].speed_x; sound(8); } if(ball->x()>=width()-ball->width()){//球和右壁相撞 ballPoint[0].speed_x=0-ballPoint[0].speed_x; sound(8); } if(ball->y()<=0){//球和上壁相撞 ballPoint[0].speed_y=0-ballPoint[0].speed_y; sound(8); } if((ball->x()>= paiz->x()-ball->width()/2) &&(ball->x()<=paiz->x()+paiz->width()-ball->width()/2) &&(((ball->y()<paiz->y()+paiz->height()) &&(ball->y()>paiz->y())) ||(( ball->y() > paiz->y()-ball->width() ) && ( ball->y() < paiz->y()+paiz->height()-ball->width())))){ ballPoint[0].speed_y=0-ballPoint[0].speed_y;//球和拍上下壁相撞 sound(7); //hitball } if((ball->y()>=paiz->y()-ball->width()/2) &&(ball->y()<=paiz->y()+paiz->height()-ball->width()/2) &&(((ball->x()<paiz->x()+paiz->width()) &&(ball->x()>paiz->x())) ||((ball->x()>paiz->x()-ball->width()) &&(ball->x()<paiz->x()+paiz->width()-ball->width())))){ ballPoint[0].speed_x=0-ballPoint[0].speed_x;//球和拍左右壁相撞 sound(7); //hitball } if (ball->y()>height()){//球跑出下邊的屏幕 begin=false; paiz->setGeometry( QRect(width()/2, height()-PAIZ_SIZE_TO_BUTTOM, paiz->width(), PAIZ_HEIGHT )); sound(9); if(--bloodNum<0){ gameOver=true; gameOverFun(); } blood->setText(QString::number(bloodNum)); } BlockMap::iterator it=blocks->begin(); while(it!=blocks->end()){//球和磚相撞 VarMap::iterator vt=blockVar.find((*it).first); if(ballHitBlocks((*it).second,(*vt).second.first)==true){ int a=(*it).first.first/BLOCK_WIDTH ; a%=9; FallScore::iterator ft=fallScores.find(a); if(ft!=fallScores.end()){ (*ft).second->setGeometry(QRect((*it).second->x(),(*it).second->y()+30,13,20 ) ); } tempScore+=(*vt).second.second.toInt(); scoreNum+=(*vt).second.second.toInt(); // I shall use if text vt.second.second is or not int score->setText(QString::number(scoreNum)); sound(6); //block blocks->erase(it); blockVar.erase(vt); } ++it; }//while over if(gameOver==false&&tempScore&&tempScore==passScore){ //logic of pass this guan過關(guān)羅輯 blockVar.erase(blockVar.begin(),blockVar.end()); BlockMap::iterator bt=blocks->begin(); while(bt!=blocks->end()){ // (*bt).second->setGeometry( QRect(800,800, BLOCK_WIDTH, BLOCK_HEIGHT ) ); (*bt).second->close(); bt++; } blocks->erase(blocks->begin(),blocks->end()); delete blocks; guan++; initGame();// erase(); } }bool Dzk::ballHitBlocks( QPushButton *block,int &hard){ //first is thid block,and second is thid blocks hard if((ball->x()>= block->x()-ball->width()/2) &&(ball->x()<=block->x()+block->width()-ball->width()/2) &&(((ball->y()<block->y()+block->height()) &&(ball->y()>block->y())) ||(( ball->y() > block->y()-ball->width() ) && ( ball->y() < block->y()+block->height()-ball->width())))){ ballPoint[0].speed_y=0-ballPoint[0].speed_y;//球和磚上下壁相撞 if(--hard==0){ //hited and on hard ,then return true // block->destroy(); block->close(); //delete block; return true; } sound(6);//hited but hard >0 ,only play sound } if((ball->y()>=block->y()-ball->width()/2) &&(ball->y()<=block->y()+block->height()-ball->width()/2) &&(((ball->x()<block->x()+block->width()) &&(ball->x()>block->x())) ||((ball->x()>block->x()-ball->width()) &&(ball->x()<block->x()+block->width()-ball->width())))){ ballPoint[0].speed_x=0-ballPoint[0].speed_x;//球和磚左右壁相撞 if(--hard==0){//hited and on hard ,then return true //block->destroy(); block->close(); //delete block; return true; } sound(6); //hited but hard >0 ,only play sound } return false;}//*inits************void Dzk::initGame(){//initgame 初始化 begin=false; //rinit paiz //rinit Ball Point point={ball->x(),ball->y(),X_SPEED,SPEED-X_SPEED,BALL_SIZE}; ballPoint.push_back(point); //init BLocks initBlocks(guan); tempScore=0;}void Dzk::initFall(){ //the falls掉落物體初始化 for(int i=1;i<6;i++){ fallScores[i] = new QLabel( this, "score" ); FallScore::iterator it=fallScores.find(i); (*it).second->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, (*it).second->sizePolicy().hasHeightForWidth() ) ); if(i==1){ (*it).second->setGeometry( QRect( 800, 800, 13,20 ) ); (*it).second->setMinimumSize( QSize( 13, 20 ) ); (*it).second->setMaximumSize( QSize( 13, 20 ) ); (*it).second->setPaletteForegroundColor( QColor( 0, 170, 255 ) ); // blue +9 (*it).second->setText( tr( "9" ) ); }else if(i==2){ (*it).second->setGeometry( QRect( 800, 800, 39,20 ) ); (*it).second->setMinimumSize( QSize( 39, 20 ) ); (*it).second->setMaximumSize( QSize( 39, 20 ) ); (*it).second->setPaletteForegroundColor( QColor( 47, 211, 53 ) ); //green <-> (*it).second->setText( tr( "<->" ) ); }else if(i==3){ (*it).second->setGeometry( QRect( 800, 800, 26,20 ) ); (*it).second->setMinimumSize( QSize(26, 20 ) ); (*it).second->setMaximumSize( QSize( 26, 20 ) ); (*it).second->setPaletteForegroundColor( QColor( 0, 0, 255 ) ); //high blue +99 (*it).second->setText( tr( "99" ) ); }else if(i==4){ (*it).second->setGeometry( QRect( 800, 800, 52,20 ) ); (*it).second->setMinimumSize( QSize( 52, 20 ) ); (*it).second->setMaximumSize( QSize( 52, 20 ) ); (*it).second->setPaletteForegroundColor( QColor( 0, 0, 0 ) ); //black -><- (*it).second->setText( tr( "-><-" ) ); }else if(i==5){ (*it).second->setGeometry( QRect( 800, 800, 26,20 ) ); (*it).second->setMinimumSize( QSize( 26, 20 ) ); (*it).second->setMaximumSize( QSize( 26, 20 ) ); (*it).second->setPaletteForegroundColor( QColor( 255, 0, 0 ) ); //red +o (*it).second->setText( tr( "+" ) ); } QFont score_font((*it).second->font() ); score_font.setFamily( "Aharoni" ); score_font.setPointSize( 20 ); score_font.setBold( TRUE ); (*it).second->setFont( score_font ); }}void Dzk::initBlocks( int guanface ){ //blocks初始化磚 passScore=0; switch(guanface) { case 1: blocks=new BlockMap ; for(int i=1;i<5;i++){ for(int j=1;j<5;j++){ VAR var; (*blocks)[XY(i*BLOCK_WIDTH,j*BLOCK_HEIGHT)]=new QPushButton(this); BlockMap::iterator it=blocks->find(XY(i*BLOCK_WIDTH, j*BLOCK_HEIGHT)); (*it).second->setGeometry( i*BLOCK_WIDTH,j*BLOCK_HEIGHT+BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT ); (*it).second->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 50, 0, (*it).second->sizePolicy().hasHeightForWidth() ) ); (*it).second->setMinimumSize( QSize( BLOCK_WIDTH, BLOCK_HEIGHT ) ); (*it).second->setMaximumSize( QSize( BLOCK_WIDTH, BLOCK_HEIGHT ) ); if(j==14){ (*it).second->setPaletteBackgroundColor( QColor( 248, 248, 0 ) );//gold var=VAR(GOLD_BLOCK_HARD,GOLD_BLOCK_SCORE); passScore+=GOLD_BLOCK_SCORE; }else if(i==1||i==14){ (*it).second->setPaletteBackgroundColor( QColor( 202, 202, 202 ) ); //iron var=VAR(IRON_BLOCK_HARD,IRON_BLOCK_SCORE); passScore+=IRON_BLOCK_SCORE; }else if(i==j){ (*it).second->setPaletteBackgroundColor( QColor( 0, 170, 255 ) ); //blue var=VAR(BLOCK_HARD,BLOCK_SCORE*9); passScore+=BLOCK_SCORE*9; }else{ (*it).second->setPaletteBackgroundColor( QColor( 255, 170, 0 ) ); //blockred var=VAR(BLOCK_HARD,BLOCK_SCORE); passScore+=BLOCK_SCORE; } (*it).second->setFocusPolicy( QPushButton::NoFocus ); //(*it).second->raise(); blockVar[XY(i*BLOCK_WIDTH,j*BLOCK_HEIGHT)]=var; //init varmap } }break; case 2: blocks=new BlockMap ; for(int i=1;i<15;i++){ for(int j=1;j<15;j++){ VAR var; (*blocks)[XY(i*BLOCK_WIDTH,j*BLOCK_HEIGHT)]=new QPushButton(this); BlockMap::iterator it=blocks->find(XY(i*BLOCK_WIDTH, j*BLOCK_HEIGHT)); (*it).second->setGeometry( i*BLOCK_WIDTH,j*BLOCK_HEIGHT+BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT ); (*it).second->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 50, 0, (*it).second->sizePolicy().hasHeightForWidth() ) ); (*it).second->setMinimumSize( QSize( BLOCK_WIDTH, BLOCK_HEIGHT ) ); (*it).second->setMaximumSize( QSize( BLOCK_WIDTH, BLOCK_HEIGHT ) ); if(j==14){ (*it).second->setPaletteBackgroundColor( QColor( 248, 248, 0 ) );//gold var=VAR(GOLD_BLOCK_HARD,GOLD_BLOCK_SCORE); passScore+=GOLD_BLOCK_SCORE; }else if(i==1||i==14){ (*it).second->setPaletteBackgroundColor( QColor( 202, 202, 202 ) ); //iron var=VAR(IRON_BLOCK_HARD,IRON_BLOCK_SCORE); passScore+=IRON_BLOCK_SCORE; }else if(i==j){ (*it).second->setPaletteBackgroundColor( QColor( 0, 170, 255 ) ); //blue var=VAR(BLOCK_HARD,BLOCK_SCORE*9); passScore+=BLOCK_SCORE*9; }else{ (*it).second->setPaletteBackgroundColor( QColor( 255, 170, 0 ) ); //blockred var=VAR(BLOCK_HARD,BLOCK_SCORE); passScore+=BLOCK_SCORE; } (*it).second->setFocusPolicy( QPushButton::NoFocus ); (*it).second->show(); blockVar[XY(i*BLOCK_WIDTH,j*BLOCK_HEIGHT)]=var; //init varmap } }break; // case 3:break;// gameOverFun();break; default : gameOver=true;gameOverFun(); }//switch case over }int Dzk::getScore(){ return scoreNum;}void Dzk::backSoundSlot(){ sound(0);}void Dzk::sound(int choice) { //音效 switch(choice) { case 0: wav->openSound("sound/backmusic.wav"); break;//back music case 1: wav->openSound("sound/bird.wav"); break;//blue 9 case 2: wav->openSound("sound/up.wav"); break;//green <-> case 3: wav->openSound("sound/bird.wav"); break;//high blue 99 case 4: wav->openSound("sound/up2.wav"); break;//black -><- case 5: wav->openSound("sound/walll.wav"); break;//red +o case 6: wav->openSound("sound/block.wav"); break;//block case 7: wav->openSound("sound/hitball.wav"); break;//hitball case 8: wav->openSound("sound/wall.wav"); break; //sound case 9: wav->openSound("sound/died.wav"); break; //sound } wav->play();}void Dzk::gameOverFun(){ DzkDialog *dzkdia=new DzkDialog(); // 輸入性名 dzkdia->setScore(scoreNum+=bloodNum>0?bloodNum*99:0); dzkdia->show(); over=new QLabel( this ); over->setGeometry( QRect( 90, 240, 620, 137 ) ); over->setPaletteForegroundColor( QColor( 85, 0, 0 ) ); QFont textLabel_font( over->font() ); textLabel_font.setPointSize( 100 ); textLabel_font.setBold( TRUE ); over->setFont( textLabel_font ); over->setTextFormat( QLabel::RichText ); resize( QSize(800, 550).expandedTo(minimumSizeHint()) ); clearWState( WState_Polished ); over->setText( tr( "GAME OVER" ) ); over->show(); agin=new QPushButton (this); agin->setGeometry( QRect( 281, 360, 260, 60 ) ); agin->setPaletteForegroundColor( QColor( 0, 170, 255 ) ); QFont pushButton_font(agin->font() ); pushButton_font.setPointSize( 40 ); agin->setFont( pushButton_font ); agin->setText( tr( "quit") ); // b.setText( trUtf8( "鍐嶆潵涓
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -