c++小程序2021/4/3
自己做了一个小程序,希望能给做小游戏的人一些帮助,大佬勿喷,谢谢!
用了一些关于数据库的东西,还没调试好,请多多包含
#include <cassert>
#include <bits/stdc++.h>
#include <fstream>
#include <windows.h>
#include <cctype>
#include <cerrno>
#include <cstring>
#include <ctime>
#include<time.h>
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <WinSock.h>
#include <thread>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#define WP 3;
#define GJ 3;
#define WQ 5;
#define WNJ 3;
using namespace std;
string banben = "1.2", cdbb, zh, mm, nc, dm, m_mysql[6];//游戏版本号,存档版本号,账号
const char* host = "localhost";
int port = 3306;
char yq[64], xz_[64];//系统时间
bool h_p = 0;//时间
bool wq[5], gj[2];//wq=武器,gj=工具,wq[0]=长矛,wq[1]=撬棍,wq[2]=土著人巨剑,wq[3]=弩,wq[4]=侦察头盔
int h;
double h_sj = 0;//游戏时间
int js, js1, js2, js3, js4, sm;//sm=记录僵尸类型
int n, xw;//询问
string xw_;
int qianbi, xl = 100, sh = 6, bd = 0, nj = 0, bh = 0, xz = 0;//变量,qianbi=钱币,xl=血量,sh=伤害,bd=绷带,nj=弩箭,xz=箱子,bh=保护
int xlsx = 100;//xlsx=血量上限
int wnj[3];//护甲耐久
int wp[3];//wp=物品
int jb = 0;//级别
int nl = 50;//能量
int M_bbck()
{
MYSQL_ROW M_mysql;
MYSQL mysql;
mysql_init(&mysql); //初始化MYSQL变量
MYSQL_RES* result;
if (mysql_real_connect(&mysql, host, "root", "12345", "neice", port, NULL, 0) == NULL) //连接到mysql
{
cout << "\n\t-----MySQL连接失败----" << endl;
return 3;
}
if (mysql_query(&mysql, "select * from banben") != 0) //若查询成功返回0,失败返回随机数,执行查询语句,这里是查询所有,banben是表名
{
cout << "\n\t ----查询失败----" << endl;
return 3;
}
result = mysql_store_result(&mysql);//将查询到的结果集储存到result中
M_mysql = mysql_fetch_row(result);//将地址转换成数组
m_mysql[0] = M_mysql[0];
if (banben == M_mysql[0]) return 1;
else return 0;
if (result != NULL) mysql_free_result(result);//释放结果资源
do
{
mysql_free_result(result);
} while (!mysql_next_result(&mysql));
mysql_close(&mysql);//关闭MySQL连接
mysql_library_end();//关闭MySQL库
}
bool M_dlzh()
{
printf("欢迎游玩我的小游戏\n");
printf("您可以选择游客登录或者绑定邮箱\n");
printf("1.游客登陆\n2.绑定邮箱\n");
cl:
cin >> xw;
if (xw == 1)
{
zh = "游客";
return 1;
}
else
{
printf("请输入邮箱账号\n");
cin >> zh;
printf("请输入密码\n");
cin >> mm;
MYSQL_ROW M_mysql;
MYSQL mysql;
printf("请输入游戏昵称\n");
cin >> nc;
mysql_init(&mysql);
MYSQL_RES* result;
mysql_real_connect(&mysql, host, "root", "12345", "neice", port, NULL, 0);
if (mysql_query(&mysql, "set names gbk"))mysql_errno(&mysql);
if (mysql_query(&mysql, "select * from rs") != 0)
{
cout << "\n\t ----查询失败----" << endl;
return 0;
}
result = mysql_store_result(&mysql);//将查询到的结果集储存到result中
M_mysql = mysql_fetch_row(result);//将地址转换成数组
mysql_query(&mysql, "set names gbk");
string dm = M_mysql[0];
string dm2;
stringstream M_stre;
int M_int;
M_stre << dm;
M_stre >> M_int;
M_int = M_int + 1;
stringstream M_stre2;
M_stre2 << M_int;
M_stre2 >> dm2;
string M_str = "insert into r(yx,mm,name,dm) values ('";
M_str.append(zh);
M_str.append("','");
M_str.append(mm);
M_str.append("','");
M_str.append(nc);
M_str.append("','");
M_str.append(dm);
M_str.append("')");
if (mysql_query(&mysql, M_str.c_str()) != 0)
{
cout << "MySQL存储失败" << mysql_error(&mysql);
return 0;
}
M_str = "update rs set rs='";
M_str.append(dm2);
M_str.append("'where rs='");
M_str.append(dm);
M_str.append("'");
if (mysql_query(&mysql, M_str.c_str()) != 0)
{
cout << "MySQL存储失败";
return 0;
}
mysql_close(&mysql);//关闭MySQL连接
mysql_library_end();//关闭MySQL库
return 1;
}
}
char* U2G(const char* utf8)//UTF8转化为 从数据库得到的结果显示中文正常
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if (wstr)
{
delete[] wstr;
wstr = NULL;
}
return str;
}
void liaotians()
{
MYSQL_ROW M_mysql;
MYSQL_RES* result;
MYSQL mysql2;
while (xw_ != "-1")
{
Sleep(500);
system("cls");
mysql_init(&mysql2);
mysql_real_connect(&mysql2, host, "root", "12345", "neice", port, NULL, 0);
mysql_query(&mysql2, "select * from liaotian");
result = mysql_store_result(&mysql2);//将查询到的结果集储存到result中
M_mysql = mysql_fetch_row(result);//将地址转换成数组
do
{
result = mysql_store_result(&mysql2);
mysql_free_result(result);
}while (!mysql_next_result(&mysql2));
for (int i = 0; i < 5; i++)
{
cout << U2G(M_mysql[i]) << "\n";
m_mysql[i] = U2G(M_mysql[i]);
}
Sleep(500);
}
mysql_close(&mysql2);
mysql_library_end();
}
int liaotian()
{
MYSQL mysql;
MYSQL_RES* result;
mysql_init(&mysql);
if (mysql_real_connect(&mysql, host, "root", "12345", "neice", port, NULL, 0) == NULL) //连接到mysql
{
cout << "\n\t-----MySQL连接失败----" << endl;
return 2;
}
if (mysql_query(&mysql, "set names gbk"))
{
cout << "\n\t-----MySQL更改字符失败----" << endl << mysql_error(&mysql) << endl;
return 2;
}
if (mysql_query(&mysql, "select * from liaotian") != 0) //连接到mysql
{
cout << "\n\t-----MySQL查询失败----" << endl;
return 2;
}
do
{
result = mysql_store_result(&mysql);
mysql_free_result(result);
} while (!mysql_next_result(&mysql));
std::thread liaotians_(liaotians);
while (1)
{
cin >> xw_;
if (xw_ == "-1")
break;
string M_str = "DELETE FROM liaotian WHERE yi='"+m_mysql[0]+"'";
mysql_query(&mysql, M_str.c_str());
M_str = "insert into liaotian values ('"+ m_mysql[1] + "','" + m_mysql[2] + "','" + m_mysql[3] + "','" + m_mysql[4] + "','" + nc + ":" + xw_ + "')";
mysql_query(&mysql, M_str.c_str());
}
liaotians_.join();
mysql_close(&mysql);//关闭MySQL连接
mysql_library_end();//关闭MySQL库
system("cls");
return 0;
}
void cshijian()
{
struct tm* info;
time_t curtime;
time(&curtime);
info = localtime(&curtime);
strftime(yq, sizeof(yq), "%M:%S", localtime(&curtime));
}
void zjnl()
{
struct tm* info;
time_t curtime;
time(&curtime);
info = localtime(&curtime);
strftime(xz_, sizeof(xz_), "%M:%S", localtime(&curtime));
int xz__ = 0, yq_ = 0;
for (int i = 0; i < 2; i++)
xz__ = xz__ * 10 + (xz_[i] - '0');
for (int i = 0; i < 2; i++)
yq_ = yq_ * 10 + (yq[i] - '0');
int m = xz__ * 60;
int ym = yq_ * 60;
xz__ = 0;
yq_ = 0;
for (int i = 3; i <= 4; i++)
xz__ = xz__ * 10 + (xz_[i] - '0');
for (int i = 3; i <= 4; i++)
yq_ = yq_ * 10 + (yq[i] - '0');
m = m + xz__;
ym = ym + yq_;
nl = nl + (m - ym) / 10;
}
void wrighttxt()//退出
{
ofstream OutFile("内测服存档.txt");
OutFile << banben;
OutFile << "\n" << nl;
OutFile << "\n" << qianbi;
OutFile << "\n" << sh;
OutFile << "\n" << jb;
OutFile << "\n" << xl;
OutFile << "\n" << bd;
OutFile << "\n" << nj;
OutFile << "\n" << xz;
OutFile << "\n" << bh;
OutFile << "\n" << h;
for (int i = 0; i < WQ i++)
{
OutFile << "\n" << wq[i];
}
for (int i = 0; i < GJ i++)
{
OutFile << "\n" << wp[i];
}
for (int i = 0; i < WP i++)
{
OutFile << "\n" << gj[i];
}
for (int i = 0; i < WNJ i++)
{
OutFile << "\n" << wnj[i];
}
ofstream OutFile1("账号.txt");
OutFile1 << zh;
OutFile1 << "\n" << nc;
}
int cbd()//用绷带
{
if (bd > 0)
{
bd = bd - 1;
xl = xl + 40;
if (xl > xlsx)
{
xl = xlsx;
}
}
}
bool sl()//判断是否死了
{
if (xl <= 0)
{
xl = 100;
cout << "你死了!";
Sleep(600);
system("cls");//清空屏幕
cout << " 目前血量" << xl << "\n";
if (bh != 1)
qianbi = 0;
if (bh != 2)
bd = 0;
if (bh != 3)
for (int i = 0; i < WQ i++)
{
wq[i] = 0;
sh = 6;
nj = 0;
}
for (int i = 0; i < WP i++)
{
wp[i] = 0;
}
wrighttxt();
Sleep(250);
return 1;
}
return 0;
}
void sj()//升级
{
int q = 0, t = 0, b = 0, k = 0;//q=升级需要花费的钱
cx:
cout << "目前钱币" << qianbi;
cout << "\n目前铁矿" << wp[0];
cout << "\n目前铁板" << wp[1];
cout << "\n目前空气过滤器" << wp[2] << "\n";
if (jb == 0)
{
cout << "升级" << jb + 1 << "需要10元钱\n";