• 2956阅读
  • 0回复

[提问]Qt贪吃蛇程序几点疑问 [复制链接]

上一主题 下一主题
离线r187111
 
只看楼主 倒序阅读 楼主  发表于: 2012-04-06
  1. #ifndef SNAKE_H
  2. #define SNAKE_H
  3. #include<QDialog>
  4. #include<QLabel>
  5. #include <QList>
  6. #include <QFrame>
  7. #include <QPalette>
  8. #include <QColor>
  9. #include <QApplication>
  10. #include <QMessageBox>
  11. #include <QTime>
  12. enum Direction{d_up,d_down,d_left,d_right};
  13. class Snake:public QDialog{
  14. Q_OBJECT
  15. private:
  16. QLabel *food;
  17. QList<QLabel*> data;
  18. int maxlen;
  19. int speed;
  20. Direction dire;
  21. public :
  22. Snake();
  23. ~Snake();
  24. public slots:
  25. void snakemove();
  26. public:
  27. void keyPressEvent(QKeyEvent *e);
  28. void timerEvent(QTimerEvent *e);
  29. /*生成食物的函数 */
  30. QLabel* getFood();
  31. };
  32. #endif // SNAKE_H

#include "Snake.h"
#include <QKeyEvent>
#include <QTime>
Snake::Snake(){
qsrand(QTime::currentTime().msec());
this->resize(600,500);
data.push_back(getFood());
// data[0]->show();
dire=d_right;
speed=20;
this->startTimer(300);
getFood();
}
Snake::~Snake(){
}
void  Snake::snakemove(){
      int  nhx=data[0]->x();
      int  nhy=data[0]->y();
      if(nhx==food->x()&&nhy==food->y()){
            data.push_back(food);
            food=getFood();
            food->show();
      }
      switch(dire){
              case  d_up:
               nhy=nhy-20;
               break;
              case d_down:
               nhy=nhy+20;
               break;
              case d_left:
               nhx=nhx-20;
               break;
              case d_right:
              nhx=nhx+20;
              break;
      }
      /*从data中的最后一个元素  */
      for(int i=data.size()-1;i>0;i--){//为啥i--?
            data->move(data[i-1]->x(),data[i-1]->y());
      }
      data[0]->move(nhx,nhy);
      if(nhx<=0||nhx>=this->width()||
         nhy<=0||nhy>=this->height()){
          this->close();
      }
}
void  Snake::keyPressEvent(QKeyEvent *e){
       /*判断 到底那个键被按下*/
      if(e->key()==Qt::Key_Up){
        dire=d_up;
      }else if(e->key()==Qt::Key_Down){
        dire=d_down;
     }else if(e->key()==Qt::Key_Left){
        dire=d_left;
     }else if(e->key()==Qt::Key_Right){
          dire=d_right;
      }else{
         ;
      }
}
void  Snake::timerEvent(QTimerEvent *e){
       snakemove();
}
/*生成食物的函数 */
QLabel*  Snake::getFood(){
          food=new QLabel(this);//this代表谁?
          food->resize(20,20);
          food->setAutoFillBackground(true);
          food->setFrameShape(QFrame::Box);
          food->setPalette(QPalette(QColor(255,0,0)));
          int  x=this->width();
          int  y=this->height();
          food->move((qrand()%(x/20))*20,// /20再*20什么意思?
                     (qrand()%(y/20))*20);
          return food;
}
  1. #include "Snake.h"
  2. #include <QApplication>
  3. int main(int argc,char**argv){
  4. QApplication app(argc,argv);
  5. Snake sn;
  6. sn.show();//show();函数的作用是通过sn调用头文件的Snake这个类内所有函数?
  7. return app.exec();
  8. }






快速回复
限100 字节
 
上一个 下一个