1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
| #include "widget.h" #include "ui_widget.h" #include<QVBoxLayout> #include<QPushButton> #include<QLabel> #include<QLineEdit> #include<QPainter> #include<QTime> #include<QDebug> #include<QMessageBox> #define ROW 20 #define COLUMN 20 #define Direction 4 void Widget::keyPressEvent(QKeyEvent *event) { if(event->key()=='W'&&m_direction!=Down){ m_direction=Up; } if(event->key()=='S'&&m_direction!=Up){ m_direction=Down; } if(event->key()=='A'&&m_direction!=Right){ m_direction=Left; } if(event->key()=='D'&&m_direction!=Left){ m_direction=Right; } } void Widget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPen pen; pen.setColor(QColor(Qt::darkGray)); QFont font; font.setPointSize(6); QPainter p(this); p.drawPixmap(0,0,width(),height(),QPixmap(":/img/imgs/bg.jpg")); p.setPen(pen); p.setFont(font); for (int row = 0; row < ROW; ++row) { for (int column =0; column < COLUMN; ++column) { p.fillRect(row*20,column*20,20,20,QBrush(QColor(0,0,0,10))); p.drawRect(row*20,column*20,20,20); } } p.fillRect(m_foodPos.x()*20,m_foodPos.y()*20,20,20, QBrush(QPixmap(":/img/imgs/apple.png").scaled(20,20))); if(linkedlist->first()){ Linkedlist:: SnakeNode *pt=linkedlist->first(); do{ if(linkedlist->first()==pt){ QPixmap pixmap; switch (m_direction) { case Up: pixmap=QPixmap(":/img/imgs/up.png").scaled(20,20); break; case Down: pixmap=QPixmap(":/img/imgs/down.png").scaled(20,20); break; case Left: pixmap=QPixmap(":/img/imgs/left.png").scaled(20,20); break; case Right: pixmap=QPixmap(":/img/imgs/right.png").scaled(20,20); break; default: break; } p.fillRect(pt->pos.x()*20, pt->pos.y()*20, 20,20, QBrush(pixmap)); }else if(linkedlist->last()==pt){ QPixmap pixmap; pixmap=QPixmap(":/img/imgs/tail.png").scaled(20,20); p.fillRect(pt->pos.x()*20, pt->pos.y()*20, 20,20, QBrush(pixmap)); }else{ QPixmap pixmap; pixmap=QPixmap(":/img/imgs/body.png").scaled(20,20); p.fillRect(pt->pos.x()*20, pt->pos.y()*20, 20,20, QBrush(pixmap)); } pt=pt->next; }while(pt!=NULL); } } Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setFixedSize(800,450); this->setWindowTitle("贪吃蛇"); initializedGUI(); } Widget::~Widget() { delete ui; }
void Widget::initializedGUI() { m_score=0; QPushButton *btnStart=new QPushButton(this); QPushButton *btnStop=new QPushButton(this); QLabel *lbScore=new QLabel(this); leScore=new QLineEdit(this); btnStart->setText(QObject::tr("开始游戏")); btnStop->setText(QObject::tr("暂停游戏")); btnStart->setFlat(true); btnStop->setFlat(true); lbScore->setText(tr("得分:")); leScore->setText(QString::number(m_score)); leScore->setFocusPolicy(Qt::NoFocus); leScore->setEnabled(false); lbScore->move(500,50); leScore->move(560,50); btnStart->move(500,100); btnStop->move(500,150); connect(btnStart,SIGNAL(clicked(bool)),this,SLOT(startGame())); connect(btnStop,SIGNAL(clicked(bool)),this,SLOT(stopGame())); QTime t= QTime::currentTime(); int s= t.secsTo(QTime(0,0,0,0)); qsrand(s); m_direction=qrand()%Direction; switch (m_direction) { case Up:qDebug()<<"蛇头方向:上"; break; case Down:qDebug()<<"蛇头方向:下"; break; case Left:qDebug()<<"蛇头方向:左"; break; case Right:qDebug()<<"蛇头方向:右"; break; default: break; } m_foodPos.setX(qrand()%COLUMN); m_foodPos.setY(qrand()%ROW); qDebug()<<"食物坐标:"<<m_foodPos; m_speed=150; m_timerControl=new QTimer(this); m_timerControl->setInterval(m_speed); connect(m_timerControl,SIGNAL(timeout()),this,SLOT(timeOut())); QPoint pos; pos.setX(qrand()%COLUMN); pos.setY(qrand()%ROW); linkedlist=new Linkedlist(this,pos); }
void Widget::resetFoodPos() { m_foodPos.setX(qrand()%COLUMN); m_foodPos.setY(qrand()%ROW); checkFoodPosisEqualHeadPos(); }
void Widget::checkisEatSelf() { if(linkedlist->first()){ Linkedlist::SnakeNode *p0=linkedlist->first()->next; if(p0){ do { if(linkedlist->first()->pos==p0->pos){ qDebug()<<"吃到自己!游戏结束"; m_timerControl->stop(); QMessageBox::critical(this,"game over","game over"); this->close(); break; } p0=p0->next; } while (p0); } } }
void Widget::checkisEatFood() { if(linkedlist->first()->pos.x()==m_foodPos.x()&& linkedlist->first()->pos.y()==m_foodPos.y()){ qDebug()<<"吃到食物!"; m_eatfoodCount++; m_score+=20; leScore->setText(QString::number(m_score)+" 分"); if(m_score>=100){ m_speed=80; m_timerControl->setInterval(m_speed); } QPoint pos=QPoint(); linkedlist->append(pos); resetFoodPos(); } }
void Widget::checkisHitWall() { if(linkedlist->first()->pos.x()<0||linkedlist->first()->pos.y()<0|| linkedlist->first()->pos.x()>COLUMN-1||linkedlist->first()->pos.y()>ROW-1){ m_timerControl->stop(); qDebug()<<"撞到墙壁!游戏结束!"; QMessageBox::critical(this,"game over","game over!"); this->close(); } } void Widget::timeOut() { checkisEatFood(); linkedlist->printLinkedlist(); if(linkedlist->first()){ Linkedlist::SnakeNode *p0=linkedlist->last(); if(linkedlist->getLength()!=1&&p0){ do { p0->pos=p0->prev->pos; p0=p0->prev; } while (p0!=linkedlist->first()); } } snakeRunning(); checkisHitWall(); checkisEatSelf(); this->update(); }
void Widget::startGame() { m_timerControl->start(); }
void Widget::stopGame() { m_timerControl->stop(); }
void Widget::snakeRunning() { int x=linkedlist->first()->pos.x(); int y=linkedlist->first()->pos.y(); switch (m_direction) { case Up: linkedlist->first()->pos.setY(y-=1); break; case Down: linkedlist->first()->pos.setY(y+=1); break; case Left: linkedlist->first()->pos.setX(x-=1); break; case Right: linkedlist->first()->pos.setX(x+=1); break; default: break; } }
void Widget::checkFoodPosisEqualHeadPos() { if(linkedlist->first()){ Linkedlist::SnakeNode *p0=linkedlist->first(); do { if(p0->pos==m_foodPos){ qDebug()<<"检测到食物处于蛇身上..."; resetFoodPos(); } p0=p0->next; } while (p0); } }
|