代码:
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
#include <QMessageBox>
#include <QAbstractButton>
#include <QVector>
#include "widget.h"
#include "register.h"
#include "userlist.h"
#include "user.h"
namespace Ui {
class Login;
}
class Login : public QDialog
{
Q_OBJECT
public:
explicit Login(QWidget *parent = nullptr);
~Login();
private slots:
void on_btn_login_clicked();
void on_btn_register_clicked();
void register_success_slot(UserList &l);
public:
private:
Ui::Login *ui;
Widget w;
Register r;
//UserList list;
};
#endif // LOGIN_H
register.h
#ifndef REGISTER_H
#define REGISTER_H
#include <QDialog>
#include <QMessageBox>
#include <QAbstractButton>
#include "user.h"
#include "userlist.h"
namespace Ui {
class Register;
}
class Register : public QDialog
{
Q_OBJECT
public:
explicit Register(QWidget *parent = nullptr);
~Register();
signals:
void register_success(UserList &list);
private slots:
void on_btn_register_clicked();
void on_btn_quit_clicked();
private:
Ui::Register *ui;
QMessageBox msgBox;
//UserList list;
};
#endif // REGISTER_H
user.h
#ifndef USER_H
#define USER_H
#include <QObject>
class User
{
public:
User();
~User();
public:
QString getName();
void setName(QString name);
QString getUser_accout();
void setUser_account(QString user_account);
QString getPwd();
void setPwd(QString pwd);
private:
QString name;
QString user_account;
QString pwd;
};
#endif // USER_H
userlist.h
#ifndef USERLIST_H
#define USERLIST_H
#include <QObject>
#include <QVector>
#include "user.h"
class UserList
{
public:
UserList();
~UserList();
public:
void userlist_insert(User &user);
void userlist_delete();
void userlist_locate(QString user_account);
void userlist_update();
bool isExist(QString user_account);
bool account_judge(QString &user, QString &pwd);
private:
QVector<User> userlist;
};
#endif // USERLIST_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimer>
#include <QPen>
#include <QPainter>
#include <QPaintEvent>
#include <QRect>
#include <QPoint>
#include <QBrush>
#include <QLabel>
#include <QMessageBox>
#include <QApplication>
#include <QDir>
#include <QProcess>
static const int RETCODE_RESTART = 773;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public:
void Init();
public:
QTimer *m_Timer;
bool time_flag=false;
int base_time=0,time_s=0;
bool flag_times=false;
private slots:
void dida_time();
void star_time();
void stop_time();
void restar_time();
public:
int x,y,base;
int save[625][2]={{2,0},{1,0},{0,0}};
int length=3;
int mark=0;
int now_head_x,now_head_y;
int head_x=2,head_y=0;
int food_x=6,food_y=6;
bool food_flag=true;
char head_direction=4;
bool head_eat=false;
bool move_flag=false;
void reboot();
protected:
void paintEvent(QPaintEvent *);
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
login.cpp
#include "login.h"
#include "ui_login.h"
UserList list;
Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
connect(&r, SIGNAL(register_success(UserList&)), this, SLOT(register_success_slot(UserList &)));
}
Login::~Login()
{
delete ui;
}
void Login::on_btn_login_clicked()
{
//获取用户输入的用户名和密码
QString user = ui->lineEdit_user->text();
QString pwd = ui->lineEdit_pwd->text();
//判断用户是否存在
if(list.isExist(user) == false)
{
QMessageBox::warning(this, "提示", "用户不存在!");
}
//判断用户名或者密码是否正确
if(list.account_judge(user, pwd) == false)
{
QMessageBox::warning(this, "提示", "用户名或密码错误!");
}
else
{
this->hide();
w.show();
}
}
void Login::on_btn_register_clicked()
{
this->close();
r.show();
}
//注册界面发来注册成功信号,登陆界面显示
void Login::register_success_slot(UserList &l)
{
this->show();
list = l;
}
main.cpp
#include "widget.h"
#include "login.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Widget w;
//w.show();
Login login;
login.show();
return a.exec();
}
register.cpp
#include "register.h"
#include "ui_register.h"
#include <QDebug>
extern UserList list;
Register::Register(QWidget *parent) :
QDialog(parent),
ui(new Ui::Register)
{
ui->setupUi(this);
ui->label_title->setFont(QFont("宋体", 20, QFont::Bold));
ui->lineEdit_user->setPlaceholderText("请输入用户名");
ui->lineEdit_pwd->setPlaceholderText("请输入密码");
ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
ui->lineEdit_pwd_2->setPlaceholderText("请再次确认密码");
ui->lineEdit_pwd_2->setEchoMode(QLineEdit::Password);
}
Register::~Register()
{
delete ui;
}
void Register::on_btn_register_clicked()
{
QString user = ui->lineEdit_user->text();
QString pwd = ui->lineEdit_pwd->text();
QString pwd_2 = ui->lineEdit_pwd_2->text();
//用户名和密码不能为空
if(user.isEmpty() || pwd.isEmpty() || pwd_2.isEmpty())
{
QMessageBox::warning(this, "提示", "用户名或密码不符合要求!");
return ;
}
//判断两次用户名是否相同
if(pwd.compare(pwd_2, Qt::CaseInsensitive) != 0)
{
QMessageBox::warning(this, "提示", "两次键入的密码不同,请重新输入!");
}
//判断用户是否存在,若存在则注册失败,不存在则注册
if(list.isExist(user))
{
QMessageBox::warning(this, "提示", "失败,注册用户已存在!");
}
else
{
User newUser;
newUser.setUser_account(user);
newUser.setPwd(pwd);
list.userlist_insert(newUser);
msgBox.about(this, "提示", "注册成功!");
}
}
//注册成功,跳转到登陆界面的槽函数,发送注册成功信号
void Register::on_btn_quit_clicked()
{
this->hide();
emit register_success(list);
}
user.cpp
#include "user.h"
User::User()
{
}
User::~User()
{
}
QString User::getName()
{
return this->name;
}
void User::setName(QString name)
{
this->name = name;
}
QString User::getUser_accout()
{
return this->user_account;
}
void User::setUser_account(QString user_account)
{
this->user_account = user_account;
}
QString User::getPwd()
{
return this->pwd;
}
void User::setPwd(QString pwd)
{
this->pwd = pwd;
}
userlist.cpp
#include "userlist.h"
#include <QDebug>
UserList::UserList()
{
}
UserList::~UserList()
{
}
void UserList::userlist_insert(User &user)
{
userlist.push_front(user);
}
void UserList::userlist_delete()
{
}
void UserList::userlist_locate(QString user_account)
{
}
//查找用户名是否存在,存在返回true,不存在返回false
bool UserList::isExist(QString user_account)
{
for(int i = 0; i < userlist.length(); i++)
{
User user_temp = userlist.at(i);
if(user_temp.getUser_accout().compare(user_account, Qt::CaseInsensitive) == 0)
{
return true;
}
}
return false;
}
bool UserList::account_judge(QString &user, QString &pwd)
{
for(int i = 0; i < userlist.length(); i++)
{
User user_temp = userlist.at(i);
//用户存在并且账户密码都正确,返回true,否则返回false
if(user_temp.getUser_accout().compare(user, Qt::CaseInsensitive) == 0 && user_temp.getPwd().compare(pwd, Qt::CaseSensitive) == 0)
{
qDebug() << "账户:" << user_temp.getUser_accout();
qDebug() << "密码:" << user_temp.getPwd();
return true;
//break;
}
}
return false;
}
void UserList::userlist_update()
{
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->Init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init()
{
ui->lcd_mark->display(0);
ui->lcd_time->display(0);
connect(ui->btn_star,SIGNAL(clicked()),this,SLOT(star_time()));
connect(ui->btn_stop,SIGNAL(clicked()),this,SLOT(stop_time()));
connect(ui->btn_restar,SIGNAL(clicked()),this,SLOT(restar_time()));
}
void Widget::star_time()
{
m_Timer = new QTimer(this);
m_Timer->start(100);
connect(m_Timer,SIGNAL(timeout()),this,SLOT(dida_time()));
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
time_flag=true;
}
void Widget::stop_time()
{
if(time_flag==true)
{ disconnect(m_Timer,SIGNAL(timeout()),this,SLOT(dida_time())); time_flag=false; }
}
void Widget::restar_time()
{
reboot();
}
void Widget::dida_time()
{
base_time++;
time_s=base_time/10;
ui->lcd_time->display(time_s);
{
if(head_x==food_x && head_y==food_y)
{
head_eat=true;
mark++;
length++;
ui->lcd_mark->display(mark);
}
if(head_eat==false)
{
}
else
{
food_x=qrand()%25;
food_y=qrand()%25;
for(int i=1;i<length; i++)
{
if(food_x==save[i][0] && food_y==save[i][1])
{
food_x=qrand()%25;
food_y=qrand()%25;
i=1;
}
}
head_eat=false;
}
now_head_x=head_x;
now_head_y=head_y;
switch(head_direction)
{
case 1: head_y--; break;
case 2: head_y++; break;
case 3: head_x--; break;
case 4: head_x++; break;
}
for(int i=1;i<length; i++)
{
if(head_x==save[i][0] && head_y==save[i][1])
{
disconnect(m_Timer,SIGNAL(timeout()),this,SLOT(dida_time()));
QMessageBox message(QMessageBox::NoIcon, "挑战失败", "the game is over!");
message.exec();
reboot();
}
if(head_x>24 || head_y>24 ||head_x<0 || head_y<0)
{
disconnect(m_Timer,SIGNAL(timeout()),this,SLOT(dida_time()));
QMessageBox message(QMessageBox::NoIcon, "挑战失败", "the game is over!");
message.exec();
reboot();
}
}
for(int i=1;i<length; i++)
{
save[length-i][0]=save[length-i-1][0];
save[length-i][1]=save[length-i-1][1];
}
save[0][0]=head_x;
save[0][1]=head_y;
update();
}
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QPen(Qt::blue,2));
painter.setBrush(Qt::gray);
base=20;
for(x=0;x<32;x++)for(y=0;y<40;y++)
painter.drawRect(x*base,y*base,base,base);
painter.setPen(QPen(Qt::yellow,2));
painter.setBrush(Qt::green);
for(int num=0;num<length;num++)
{
painter.drawRect(save[num][0]*base,save[num][1]*base,base,base);
}
painter.setPen(QPen(Qt::white,2));
painter.setBrush(Qt::red);
painter.drawRect(food_x*base,food_y*base,base,base);
}
void Widget::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_W:
qDebug("I get up!"); if(head_direction!=2) head_direction=1; break;
case Qt::Key_S:
qDebug("I get down!"); if(head_direction!=1) head_direction=2; break;
case Qt::Key_A:
qDebug("I get left!"); if(head_direction!=4) head_direction=3; break;
case Qt::Key_D:
qDebug("I get right!");if(head_direction!=3) head_direction=4; break;
case Qt::Key_Space:
qDebug("I get Space!"); break;
default: QWidget::keyPressEvent(event); break;
}
update();
}
void Widget::keyReleaseEvent(QKeyEvent *)
{
}
void Widget::reboot()
{
QString program = QApplication::applicationFilePath();
QStringList arguments = QApplication::arguments();
QString workingDirectory = QDir::currentPath();
QProcess::startDetached(program, arguments, workingDirectory);
QApplication::exit();
exit(0);
}
布局文件(.ui结尾)
login.ui
register.ui
文章来源:https://www.toymoban.com/news/detail-548297.html
widget.ui
文章来源地址https://www.toymoban.com/news/detail-548297.html
到了这里,关于使用QT制作贪吃蛇小游戏(含登录注册界面)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!