文章目录
前言
本文章主要讲解我和朋友一起用Qt开发的小游戏《愤怒的小鸟》,游戏素材来源于游戏截图和网上查找。这是一款曾经在手机上风靡一时的横屏小游戏,游戏具体怎么玩想必我就不用跟大家介绍了吧。主要说说里面最不好实现的一个东西:“物理引擎”,就是小鸟的飞行轨迹和小鸟和障碍物的撞击效果。
一、游戏效果
二、代码功能模块
1.第一个模块(主模块) 登录界面
核心代码如下(示例):
void Widget::regiser(bool)
{
//查询用户是否已经存在
QString str = QString("select * from usertable where account='%0';")\
. arg(le_account->text());
QSqlQuery query;
query.exec(str);
while(query.next())
{
QMessageBox msgBox;
msgBox.setText("该用户已存在!请重新注册!");
msgBox.exec();
return ;
}
//不存在就注册
bool ok;
QString username = QInputDialog::getText(this, "Angry Birds",
"User name:",QLineEdit::Normal,QDir::home().dirName(),
&ok);
if (ok && !username.isEmpty())
{
//向入据库中加入一条用户信息
QString str = QString("insert into usertable values('%0', '%1', '%2', '0');")\
.arg(le_account->text()).arg(le_password->text()).arg(username);//插入用户 账号,密码,用名
QSqlQuery query;
query.exec(str);
QMessageBox msgBox;
msgBox.setText("恭喜你!注册成功!");
msgBox.exec();
}
}
void Widget::login(bool)
{
//账号√ 密码√
QString str = QString("select * from usertable where account='%0' and password='%1';")\
.arg(le_account->text()).arg(le_password->text());
QSqlQuery query;
query.exec(str);
while(query.next())
{
qDebug() << query.value(0).toString();
qDebug() << query.value(1).toString();
qDebug() << query.value(2).toString();
qDebug() << query.value(3).toString();
emit login_successed();//发射登录成功的信号
hide();//隐藏当前界面
return ;
}
//账号√密码×
str = QString("select * from usertable where account='%0';")\
.arg(le_account->text());
query.exec(str);
while(query.next())
{
QMessageBox msgBox;
msgBox.setText("密码错误!请重新输入!");
msgBox.exec();
return ;
}
//账号x
QMessageBox msgBox;
msgBox.setText("该用户不存在!请先注册!");
msgBox.exec();
}
2.第二个模块 游戏开始界面
核心代码如下(示例):
Start::Start(QWidget *parent) : QWidget(parent)
{
setFixedSize(1800,967);
setGeometry(70,50,1800,967);
this->setWindowIcon(QIcon(":/img/icon.png"));
this