屏幕快照 2020-06-02 19.56.57.png (107.57 KB, 下載次數: 72)
下載附件
2020-6-2 19:57 上傳
部分代碼
#include "snake.h"#include "ui_snake.h"#include <unistd.h>
Snake::Snake(QWidget *parent) : QWidget(parent), ui(new Ui::Snake){ ui->setupUi(this);
initSnakeLogic(); initSnakeUi(); sleep(5);
}
//得到食物點void Snake::upDateFood(){ qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); food = QPoint(qrand() % RECTNUM, qrand() % RECTNUM);
//食物不能在蛇身上 for (int i = 0; i < m_snakeLenth; i++) if (food == snake[i]) food = QPoint(qrand() % RECTNUM, qrand() % RECTNUM); //食物不能在障礙物上 for(int i = 0; i < 20; i++) if (food == snake[i]) food = QPoint(qrand() % RECTNUM, qrand() % RECTNUM);}
//游戲結束boxvoid Snake::overBox(){ timer->stop(); QMessageBox msgBox(this); msgBox.setText("Sorry, the game failed."); msgBox.exec();
initSnakeLogic();}
void Snake::initSnakeLogic(){ m_snakeLenth = 5; //開始蛇長度為10 //隨機數種子 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
//蛇頭食物初始化 QPainter paint(this); snake[0] = QPoint(qrand() % RECTNUM, qrand() % RECTNUM); upDateFood(); //蛇身初始化 for(int i=1;i<m_snakeLenth;i++){ snake[i].rx()=snake[i-1].rx()-1; snake[i].ry()=snake[i-1].ry(); } //障礙物初始化 for(int i=0;i<board_length;i++){ board[i]=QPoint(qrand() % RECTNUM, qrand() % RECTNUM); for(int j=0;j<m_snakeLenth;j++) if(snake[j]==board[i]) board[i]=QPoint(qrand() % RECTNUM, qrand() % RECTNUM); } direc = Direction_Right; m_flagStart = false; whatfood=1;}
void Snake::initSnakeUi(){ this->resize(625, 625); this->setWindowIcon(QIcon(":/img/snake.jpg"));
QPalette palette; palette.setBrush(QPalette::Background, QBrush(QPixmap(":/img/back.jpg").scaled(this->size()))); this->setPalette(palette);
// pb_start = new QPushButton(this); // pb_start->setText("開始游戲"); // pb_start->setStyleSheet("color: rgb(255, 172, 117); font: 75 14pt '蘋方-簡';"); // pb_start->setGeometry(QRect(500, 50, 100, 50)); // pb_start->setFlat(true); //透明
// pb_exit = new QPushButton(this); // pb_exit->setStyleSheet("color: rgb(255, 172, 117); font: 75 14pt '蘋方-簡';"); // pb_exit->setText("重新開始"); // pb_exit->setGeometry(QRect(500, 120, 100, 50)); // pb_exit->setFlat(true);
lb_socre = new QLabel(this); lb_socre->setStyleSheet("color:green;font: 75 20pt '華文琥珀';"); lb_socre->setText("分數:"); lb_socre->setGeometry(525, 10, 50, 50);
lb_scoreShow = new QLabel(this); lb_scoreShow->setStyleSheet("color:red;font: 75 20pt 'Braggadocio';"); lb_scoreShow->setText("0"); lb_scoreShow->setGeometry(575, 10, 50, 50);
// lb_prompt = new QLabel(this); // lb_prompt->setStyleSheet("color: rgb(226, 192, 255); font: 75 14pt '蘋方-簡';"); // lb_prompt->setText("w,a,s,d控制上下左右,k開始游戲,j重新開始\n祝您玩得愉快!"); // lb_prompt->setGeometry(500, 240, 100, 200); // lb_prompt->setWordWrap(true); //自動換行
// connect(pb_start, SIGNAL(clicked(bool)), this, SLOT(start_pressed())); // connect(pb_exit, SIGNAL(clicked(bool)), this, SLOT(exit_pressed()));
timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(slotSnakeAuto()));}
Snake::~Snake(){ delete ui;}
void Snake::paintEvent(QPaintEvent *event){ //畫表格 // QPainter painter(this); // for(int y = 0; y < RECTWIDTH * RECTNUM; y += RECTWIDTH) // { // for(int x = 0; x < RECTWIDTH * RECTNUM; x += RECTWIDTH) // { // painter.drawRect(QRect(x, y, RECTWIDTH, RECTWIDTH)); // } // }
//畫蛇頭 QPainter paint(this); QImage img_head; QImage img_board; switch(direc) { case Direction_Up: img_head = QImage(":/img/headup.png"); break; case Direction_Left: img_head = QImage(":/img/headleft.png"); break; case Direction_Down: img_head = QImage(":/img/headdown.png"); break; case Direction_Right: img_head = QImage(":/img/headright.png"); break; default: break; } paint.drawImage( snake[0].x() * RECTWIDTH, snake[0].y() * RECTWIDTH, img_head.scaled(RECTWIDTH, RECTWIDTH)); //畫蛇身 paint.setBrush(QColor(255, 128, 106)); paint.setPen(Qt::NoPen); paint.setRenderHint(QPainter::Antialiasing); for (int i = 1; i < m_snakeLenth; i++) { paint.drawRoundedRect(QRect( snake[i].x() * RECTWIDTH, snake[i].y() * RECTWIDTH, RECTWIDTH, RECTWIDTH),5,5); }
//畫食物 if(whatfood%5==0)img_head = QImage(":/img/food1.png"); else img_head = QImage(":/img/food.png"); paint.drawImage(food.x() * RECTWIDTH, food.y() * RECTWIDTH,img_head.scaled(RECTWIDTH, RECTWIDTH)); //paint.drawRect(QRect(15 + food.x() * RECTWIDTH, 15 + food.y() * RECTWIDTH, RECTWIDTH, RECTWIDTH));
//畫障礙物 img_board = QImage(":/img/board.png"); for(int i=0;i<board_length;i++){ paint.drawImage(board[i].x() * RECTWIDTH, board[i].y() * RECTWIDTH,img_board.scaled(RECTWIDTH, RECTWIDTH)); }
}
bool Snake::touchSlef(QPoint point){ for (int i = 0; i < m_snakeLenth; i++) { if (point == snake[i]) { return true; } } return false;}
bool Snake::touchBoard(QPoint point){ for (int i = 0; i < board_length; i++) { if (point == board[i]) { return true; } } return false;}
void Snake::slotSnakeAuto(){ if (m_flagStart == true) { switch (direc) { case Direction_Up: up_pressed(); break; case Direction_Left: left_pressed(); break; case Direction_Down: down_pressed(); break; case Direction_Right: right_pressed(); break; default: break; } lb_scoreShow->setText(QString::number(m_snakeLenth - 5));
if (m_snakeLenth >=100) { timer->stop(); QMessageBox msgBox(this); msgBox.setText("Sorry, the snake is too long."); msgBox.exec();
initSnakeLogic(); } }}
void Snake::keyPressEvent(QKeyEvent *event){ //qDebug() << event->key();
switch (event->key()) { case 75: start_pressed(); break; //'k' case 76: exit_pressed(); break; //'j' default: break; }
if (m_flagStart == true) { switch (event->key()) { case 87: up_pressed(); break; //'w' case 65: left_pressed(); break; //'a' case 83: down_pressed(); break; //'s' case 68: right_pressed(); break; //'d' default: break; } lb_scoreShow->setText(QString::number(m_snakeLenth - 5)); if(m_snakeLenth>10) timer->setInterval(500); if(m_snakeLenth>15) timer->setInterval(300); if(m_snakeLenth>20) timer->setInterval(200);
全部資料51hei下載地址:
Sk.zip
(1.29 MB, 下載次數: 31)
2020-6-2 19:59 上傳
點擊文件名下載附件
QT5貪吃蛇小游戲代碼 下載積分: 黑幣 -5
|