1. 首页
  2. 游戏开发
  3. cocos2D
  4. QT的贪吃蛇小游戏开发

QT的贪吃蛇小游戏开发

上传者: 2019-04-04 19:20:42上传 ZIP文件 695.2KB 热度 73次
GameWidget::GameWidget(QWidget *parent) : QWidget(parent), ui(new Ui::GameWidget) { ui->setupUi(this); //加背景 QPalette palet; palet.setBrush(QPalette::Background, \ QBrush(QPixmap(":/new/prefix1/img/green.jpg").scaled(580, 370))); this->setPalette(palet); //蛇头产生随机的 位置和随机的方向 QTime cur = QTime::currentTime(); qsrand(cur.msec()); snake[0][0] = qrand() % COL; snake[0][1] = qrand() % ROW; direction = qrand() % 4; //食物的随机位置 while (1) { foodx = qrand() % COL; foody = qrand() % ROW; if (foodx != snake[0][0] || foody != snake[0][1]) break; } //创建时钟对象 tm = new QTimer(); tm->setInterval(300); //链接时钟超时信号与槽函数 connect(tm, SIGNAL(timeout()), this, SLOT(timeout_handler())); //连接按键的信号与按钮槽函数 connect(this, SIGNAL(pressKeyASignal()), ui->leftPushButton, SLOT(click())); connect(this, SIGNAL(pressKeySSignal()), ui->downPushButton, SLOT(click())); connect(this, SIGNAL(pressKeyWSignal()), ui->upPushButton, SLOT(click())); connect(this, SIGNAL(pressKeyDSignal()), ui->rightPushButton, SLOT(click())); //初始化 foodcount = 0; score = 0; level = 1; } 位置和随机的方向 QTime cur = QTime::currentTime(); qsrand(cur.msec()); snake[0][0] = qrand() % COL; snake[0][1] = qrand() % ROW; direction = qrand() % 4; //食物的随机位置 while (1) { foodx = qrand() % COL; foody = qrand() % ROW; if (foodx != snake[0][0] || foody != snake[0][1]) break; } //创建时钟对象 tm = new QTimer(); tm->setInterval(300); //链接时钟超时信号与槽函数 connect(tm, SIGNAL(timeout()), this, SLOT(timeout_handler())); //连接按键的信号与按钮槽函数 connect(this, SIGNAL(pressKeyASignal()), ui->leftPushButton, SLOT(click())); connect(this, SIGNAL(pressKeySSignal()), ui->downPushButton, SLOT(click())); connect(this, SIGNAL(pressKeyWSignal()), ui->upPushButton, SLOT(click())); connect(this, SIGNAL(pressKeyDSignal()), ui->rightPushButton, SLOT(click())); //初始化 foodcount = 0; score = 0; level = 1; }
用户评论