图片素材及源码
下载链接
数据库SQL语句
create database box_man if not exists;
use box_man;
create table users (
id int not null auto_increment,
username varchar(64) not null,
password varchar(32) not null,
level_id int default 1,
primary key(id),
unique key username(username)
);
create table levels(
id int not null auto_increment,
name varchar(64) not null,
map_row int not null,
map_column int not null,
map_data varchar(4096) not null,
next_level_id int default 0 comment '下一关的id,0代表通关',
primary key(id)
);
insert into users
values(1,'Jack',md5(123456),1);
insert into levels
values(1,'小试牛刀→第一关',8,10,'-1,0,0,0,0,0,0,0,-1,-1|-1,0,2,2,2,2,1,0,-1,-1|0,0,0,2,2,2,4,0,0,0|0,1,1,4,0,4,1,4,1,0|0,1,4,4,1,1,0,4,1,0|0,1,1,1,3,0,1,1,1,0|0,0,0,0,1,1,1,0,0,0,|-1,-1,-1,0,0,0,0,0,-1,-1',2),
(2,'绝地强者→第二关',6,6,'0,0,0,0,0,0|0,2,2,1,1,0|0,4,4,1,3,0|0,2,1,4,0,0|0,1,1,1,0,-1|0,0,0,0,0,-1',3),
(3,'天外强人→第三关',8,10,'-1,-1,0,0,0,0,0,0,-1,-1|0,0,0,1,1,1,0,0,0,0|0,1,1,1,4,1,4,1,1,0|0,1,4,1,1,1,4,1,3,0|0,0,0,4,4,0,0,0,0,0,|-1,-1,0,1,1,2,2,0,-1,-1|-1,-1,0,2,2,2,2,0,-1,-1|-1,-1,0,0,0,0,0,0,-1,-1',4),
(4,'死而复生→BOSS关',8,8,'-1,0,0,0,0,0,0,-1|-1,0,2,1,2,2,0,-1|-1,0,2,1,4,2,0,-1|0,0,0,1,1,4,0,0|0,1,4,1,1,4,1,0|0,1,0,4,0,0,1,0|0,1,1,1,3,1,1,0|0,0,0,0,0,0,0,0',0);
操作数据库C++代码
database.h
#pragma once
#include<string>
using namespace std;
#define LINE 48
#define COLUMN 48
typedef struct _userinfo {
//这里将字符串初始为空
_userinfo() :id(0), username(""), password(""), level_id(0) {};
int id; // 用户id
string username;
string password; // 密码
int level_id;
}userinfo;
typedef struct _levelinfo {
_levelinfo() :id(0), name(""), map_row(0), map_column(0), map_data(""), next_level(0) {};
int id; // 关卡id
string name; // 关卡名字
int map_row; // 地图行数
int map_column; //地图列数
string map_data; // 二维地图数据
int next_level; //下一关卡id
}levelinfo;
bool fetch_user_info(userinfo& user);
int fetch_level_info(levelinfo& level, int level_id);
bool transform_map_db2array(levelinfo& level, int map[][COLUMN]);
bool update_user_level(userinfo& user, int next_level_id);
bool resiting_data(userinfo& user);
database.cpp
记得更改自己的用户名和密码
#include"database.h"
#include<mysql.h>
#include<stdio.h> // 会使用C的接口
#define DB_NAME "box_man" //数据库名
#define DB_HOST "127.0.0.1" //IP地址
#define DB_PORT 3306 //端口号
#define DB_USER "root" //用户名
#define DB_USER_PASSWORD "123456" //密码
bool connect_db(MYSQL& mysql) {
//1.初始数据库句柄
mysql_init(&mysql);
//2.设置字符编码(为句柄设置)
//windows支持中文,在windows上一般是gbk字符集,gbk包含中文简体和繁体,简体是gbk2312
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
//3.连接数据库
//句柄,主机,用户名,密码,数据库,端口号,——,——
if (mysql_real_connect(&mysql, DB_HOST, DB_USER, DB_USER_PASSWORD, DB_NAME, DB_PORT, NULL, 0) == NULL) {
printf("数据库连接失败,错误原因:%s\n", mysql_error(&mysql));
return false;
}
return true;
}
bool fetch_user_info(userinfo& user) {
MYSQL mysql; //定义一个句柄,对mysql的访问都是通过这个句柄(就是一个集合)
MYSQL_RES* res; // 查询的结果集
MYSQL_ROW row; // 如果结果集有多条,可以使用row获取
char sql[256];
bool ret = false;
//1.连接数据库
if (connect_db(mysql) == false) {
return false;
}
//2.设置SQL查询语句
//snprintf函数,设将可变参数(...)按照 format 格式化成字符串,并将字符串复制到 str 中,size 为要写入的字符的最大数目,超过 size 会被截断。
snprintf(sql, 256, "select id,level_id from users where username='%s' and password = md5('%s');", user.username.c_str(), user.password.c_str());
ret = mysql_query(&mysql, sql); //mysql查询,成功返回0
if (ret) {
printf("数据库查询出错,%s错误原因:%s\n", sql, mysql_error(&mysql));
// 关闭数据库
mysql_close(&mysql);
return false;
}
//3.获取查到的结果
res = mysql_store_result(&mysql);
//row获得结果集里的一行记录,再通过下标访问记录里的数据,如果结果集有多行时可以通过 while(row = mysql_fetch_row(res)){ }循环获得
row = mysql_fetch_row(res);
if (row == NULL) { //没有查到记录
//1.释放结果集
mysql_free_result(res);
//1.关闭数据库
mysql_close(&mysql);
return false;
}
user.id = atoi(row[0]); // 字符串转整数
user.level_id = atoi(row[1]);
//4.返回结果
mysql_free_result(res);
mysql_close(&mysql);
return true;
}
int fetch_level_info(levelinfo& level, int level_id) {
MYSQL mysql; //定义一个句柄,对mysql的访问都是通过这个句柄(就是一个集合)
MYSQL_RES* res; // 查询的结果集
MYSQL_ROW row; // 如果结果集有多条,可以使用row获取
char sql[256];
bool ret = false;
//1.连接数据库
if (connect_db(mysql) == false) {
return -1;
}
//2.编写SQL语句
snprintf(sql, 256, "select name,map_row, map_column,map_data,next_level_id from levels where id=%d;", level_id);
ret = mysql_query(&mysql, sql); //mysql查询,成功返回0
if (ret) {
printf("数据库查询出错,%s错误原因:%s\n", sql, mysql_error(&mysql));
// 关闭数据库
mysql_close(&mysql);
return -1;
}
//3.获取查到的结果
res = mysql_store_result(&mysql);
//row获得结果集里的一行记录,再通过下标访问记录里的数据,如果结果集有多行时可以通过 while(row = mysql_fetch_row(res)){ }循环获得
row = mysql_fetch_row(res);
if (row == NULL) { //没有查到记录
//1.释放结果集
mysql_free_result(res);
//1.关闭数据库
mysql_close(&mysql);
return 0;
}
level.id = level_id;
level.name = row[0];
level.map_row = atoi(row[1]);
level.map_column = atoi(row[2]);
level.map_data = row[3];
level.next_level = atoi(row[4]);
//string用printf输出,使用.c_str()可以返回一个const char* 的指针
//printf("level id: %d name: %s map row: %d map column: %d map data: %s next level: %d\n", level.id, level.name.c_str(), level.map_row, level.map_column, level.map_data.c_str(), level.next_level);
//4.返回结果
mysql_free_result(res);
mysql_close(&mysql);
return 1;
}
bool transform_map_db2array(levelinfo& level, int map[][COLUMN]) {
if (level.map_row > LINE || level.map_column > COLUMN) {
printf("地图过大,请重新设置\n");
return false;
}
if (level.map_data.length() < 1) {
printf("地图数据有误,请重新设置!\n");
return false;
}
long long start = 0, end = 0;
int row = 0, column = 0;
do {
end = level.map_data.find('|', start);
if (end == -1) {
end = level.map_data.length();
}
//合法性检查,结束时end=level.map_data.length,start=end+1
if (start >= end)
break;
string line = level.map_data.substr(start, end - start);
//printf("get-line:%s\n", line.c_str());
// 对行数据进行解析:0,1,0,1,1,1,1,1,1,1,0,0
char* next_token = NULL;
//line转为const char*(C语言中的字符串),strtok_s会修改line里的值,按照某个特定字符,所以需要将line.c_str()转为char *
// strtok_s第一个参数是要劈开的字符串(要求是一个char *类型的);第二个参数是按某个字符劈开(会把“,”逗号变成‘\0'结束符,也就是说会修改原来字符串,所以要把原来的const char*转为char*);第三个参数是这个接口
// 需要的,用来做定位功能,
//printf("%p\n", &line[2]);
char* item = strtok_s((char*)line.c_str(), ",", &next_token);
column = 0;
//printf("%p", next_token);
//::system("pause");
//如果某行数据个数多余这个地图列数,控制只读取level.map_column-1个
while (item&&column<level.map_column) {
map[row][column] = atoi(item);
column++;
//再使用strtok_s接口第一个参数可以写NULL,应该是next_token参数上一次做定位功能时已经记录了要劈开字符串给的某个位置
item = strtok_s(NULL, ",", &next_token);
}
//合法性检查
if (column < level.map_column) { // 某行的数据个数小于这个地图的列数
printf("地图数据解析出错,终止!\n");
return false;
}
row++;
//列数多余的话直接舍弃
if (row >= level.map_row) {
break;
}
start = end + 1;
} while (1 == 1);
//列数多余则直接报错
if (row < level.map_row) {
printf("地图行数少于设定,终止!");
return false;
}
return true;
}
bool update_user_level(userinfo& user, int next_level_id) {
MYSQL mysql; //定义一个句柄,对mysql的访问都是通过这个句柄(就是一个集合)
char sql[256];
bool ret = false;
//1.连接数据库
if (connect_db(mysql) == false) {
return false;
}
//2.编写SQL语句
snprintf(sql, 256, "update users set level_id=%d where id=%d", next_level_id, user.id);
ret = mysql_query(&mysql, sql);
if (ret) {
printf("数据库更新出错,%s错误原因:%s\n", sql, mysql_error(&mysql));
// 关闭数据库
mysql_close(&mysql);
return false;
}
user.level_id = next_level_id;
mysql_close(&mysql);
return true;
}
bool resiting_data(userinfo& user) {
MYSQL mysql; //定义一个句柄,对mysql的访问都是通过这个句柄(就是一个集合)
char sql[256];
bool ret = false;
//1.连接数据库
if (connect_db(mysql) == false) {
return false;
}
//2.编写SQL语句
snprintf(sql, 256, "update users set level_id=1 where id=%d", user.id);
ret = mysql_query(&mysql, sql);
//3.判断查询结果
if (ret) {
printf("数据库更新出错,%s错误原因:%s\n", sql, mysql_error(&mysql));
// 关闭数据库
mysql_close(&mysql);
return false;
}
user.level_id = 1;
//4.关闭数据库
mysql_close(&mysql);
return true;
}
推箱子游戏代码
box_man.h
#pragma once
#define RATIO 40
#define SCREEN_WIDTH 740
#define SCREEN_HEIGHT 500
#define START_X 50
#define START_Y 75
#define KEY_UP 'W'
#define KEY_LEFT 'A'
#define KEY_RIGHT 'D'
#define KEY_DOWN 'S'
#define KEY_OUT 'Q'
#define GAME_AGAIN 'R'
#define MAX_RETRY_TIMES 4
#define BG_IMAGE -1
#define isValid(next_pos) next_pos.x > 0 && next_pos.x < LINE && next_pos.y>0 && next_pos.y < COLUMN
typedef enum _PROPS PROPS;
typedef enum _DIRECTION DIRECTION;
typedef struct _POS POS;
enum _PROPS {
WALL, //墙
FLOOR, //地板
BOX_DES,//箱子目的地
MAN, //小人
BOX, //箱子
HIT, //箱子命中目标
MAN_DES, //人站在目标上
VECTOR //通关图片
};
enum _DIRECTION {
UP,
DOWN,
LEFT,
RIGHT
};
struct _POS {
int x; //小人所在二维数组的行数
int y; //小人所在二维数组的列数
};
box_man.cpp
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<conio.h> //???
#include"box_man.h"
#include"database.h"
using namespace std;
int map[LINE][COLUMN] = { 0 };
POS man;
IMAGE images[9];
bool isGameOver() {
for (int i = 0; i < LINE; ++i) {
for (int j = 0; j < COLUMN; ++j) {
if (map[i][j] == BOX_DES)
return false;
}
}
return true;
}
void show_over() {
cleardevice();
IMAGE game_over;
loadimage(&game_over, _T("gameover.png"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
putimage(0, 0, &game_over);
}
void changeMap(POS* next_pos, PROPS prop) {
map[next_pos->x][next_pos->y] = prop;
putimage(START_X + next_pos->y * RATIO, START_Y + next_pos->x * RATIO, &images[prop]);
}
void changeMap2(POS* next_pos, PROPS prop) {
putimage(START_X + next_pos->y * RATIO, START_Y + next_pos->x * RATIO, &images[prop]);
}
void gameControl(DIRECTION direct) {
POS next_pos = man;
POS next_next_pos = man;
switch (direct) {
case UP:
next_pos.x--;
next_next_pos.x -= 2;
break;
case DOWN:
next_pos.x++;
next_next_pos.x += 2;
break;
case LEFT :
next_pos.y--;
next_next_pos.y -= 2;
break;
case RIGHT:
next_pos.y++;
next_next_pos.y += 2;
break;
}
//宏展开next_pos.x > 0 && next_pos.x < LINE && next_pos.y>0 && next_pos.y < COLUMN
// 人的前方是地板
if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR) {
//人的脚下是目的地
if (map[man.x][man.y] == BOX_DES) {
changeMap(&next_pos, MAN);
changeMap2(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
changeMap(&next_pos, MAN);
}
man = next_pos;
}
// 人的前方是箱子
else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] == BOX) {
if (map[next_next_pos.x][next_next_pos.y] == FLOOR) { // 箱子的前方是地板
changeMap(&next_next_pos, BOX);
changeMap(&next_pos, MAN);
if (map[man.x][man.y] == BOX_DES) {
changeMap(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
}
man = next_pos;
}
else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) { // 箱子前面是目的地
changeMap(&next_next_pos, HIT);
changeMap(&next_pos, MAN);
if (map[man.x][man.y] == BOX_DES) {
changeMap(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
}
man = next_pos;
}
}
//前方是目的地
else if (isValid(next_pos) && map[next_pos.x][next_pos.y] == BOX_DES) {
//小人脚下也是目的地
if (map[man.x][man.y] == BOX_DES) {
changeMap2(&next_pos, MAN_DES);
changeMap(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
changeMap2(&next_pos, MAN_DES);
}
man = next_pos;
}
//前方是箱子命中点
else if (isValid(next_pos) && map[next_pos.x][next_pos.y] == HIT) {
if (map[next_next_pos.x][next_next_pos.y] == FLOOR) { // 箱子的前方是地板
changeMap(&next_next_pos, BOX);
changeMap(&next_pos, BOX_DES);
changeMap2(&next_pos, MAN_DES);
//人的脚下是目的地
if (map[man.x][man.y] == BOX_DES) {
changeMap(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
}
man = next_pos;
}
else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) { // 箱子前面是目的地
changeMap(&next_next_pos, HIT);
changeMap(&next_pos, BOX_DES);
changeMap2(&next_pos, MAN_DES);
//人的脚下是目的地
if (map[man.x][man.y] == BOX_DES) {
changeMap(&man, BOX_DES);
}
else {
changeMap(&man, FLOOR);
}
man = next_pos;
}
}
}
bool login(userinfo& user) {
int times = 0;
bool ret = false;
do {
cout << "请输入用户名:";
cin >> user.username;
cout << "请输入密码:";
cin >> user.password;
ret = fetch_user_info(user);
times++;
if (times >= MAX_RETRY_TIMES)
break;
if (ret == false) {
cout << "登录失败,请重新输入!" << endl;
}
} while (!ret);
return ret;
}
void init_graph() {
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
loadimage(&images[WALL], _T("wall_right.bmp"), RATIO, RATIO, true);
loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX], _T("box.jpg"), RATIO, RATIO, true);
loadimage(&images[HIT], _T("box_des.jpg"), RATIO, RATIO, true);
loadimage(&images[MAN_DES], _T("man_des.bmp"), RATIO, RATIO, true);
loadimage(&images[VECTOR], _T("vector.png"), 6*RATIO, 6*RATIO, true);
}
void show_images(levelinfo& level) {
cleardevice(); // 每次贴图清屏一下
IMAGE bg_img;
//图片的宽,高最后一个参数“是否拉伸”
loadimage(&bg_img, _T("bgimage.png"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
putimage(0, 0, &bg_img);
for (int i = 0; i < level.map_row; ++i) {
for (int j = 0; j < level.map_column; ++j) {
if (map[i][j] == MAN) {
man.x = i;
man.y = j;
}
if (map[i][j] == BG_IMAGE)
continue;
putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
}
}
const char* str = level.name.c_str();
setbkmode(TRANSPARENT);
setfont(RATIO, 0, _T("华文楷体"));
setcolor(WHITE);
outtextxy(200, 25, str);
}
void drawAlpha(IMAGE* picture, int picture_x, int picture_y) //x为载入图片的X坐标,y为Y坐标
{
// 变量初始化
DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
DWORD* draw = GetImageBuffer();
DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
int graphWidth = getwidth(); //获取绘图区的宽度,EASYX自带
int graphHeight = getheight(); //获取绘图区的高度,EASYX自带
int dstX = 0; //在显存里像素的角标
// 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
for (int iy = 0; iy < picture_height; iy++)
{
for (int ix = 0; ix < picture_width; ix++)
{
int srcX = ix + iy * picture_width; //在显存里像素的角标
int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
int sg = ((src[srcX] & 0xff00) >> 8); //G
int sb = src[srcX] & 0xff; //B
if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
{
dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
int dr = ((dst[dstX] & 0xff0000) >> 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16) //公式: Cp=αp*FP+(1-αp)*BP ; αp=sa/255 , FP=sr , BP=dr
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8) //αp=sa/255 , FP=sg , BP=dg
| (sb * sa / 255 + db * (255 - sa) / 255); //αp=sa/255 , FP=sb , BP=db
}
}
}
}
void promot_over() {
setbkmode(TRANSPARENT);
drawAlpha(&images[VECTOR], 250, 130); // 载入PNG图并去透明部分
Sleep(1100);
}
void wait(int interval) {
int count = interval / 10;
for (int i = 0; i < count; ++i) {
Sleep(10);
if (_kbhit())
return;
}
}
void game_operation(levelinfo& level, userinfo& user) {
bool quit = false;
do {
//判断是否有按键按下
if (_kbhit()) {
//无缓冲读取
char ch = _getch();
if (ch == KEY_UP) {
gameControl(UP);
}
else if (ch == KEY_DOWN) {
gameControl(DOWN);
}
else if (ch == KEY_LEFT) {
gameControl(LEFT);
}
else if (ch == KEY_RIGHT) {
gameControl(RIGHT);
}
else if (ch == GAME_AGAIN) {
fetch_level_info(level, user.level_id);
transform_map_db2array(level, map);
show_images(level);
}
else if (ch == KEY_OUT) {
closegraph();
exit(0);
}
if (isGameOver()) {
//更新用户下一关关卡信息(用户通关后直接跳转下一关)
update_user_level(user, level.next_level);
quit=true;
}
}
wait(100);
} while (quit == false);
}
void judge_by_result(int result, levelinfo& level, userinfo& user) {
if (result == 1) {
return;
}
else if (result == -1) {
closegraph();
cout << "获取关卡数据失败,请重试!" << endl;
std::system("pause");
exit(-1);
}
else if (result == 0) {
show_over();
do {
//判断是否有按键按下
if (_kbhit()) {
//无缓冲读取
char ch = _getch();
if (ch == KEY_OUT) {
closegraph();
exit(0);
}
else if (ch == GAME_AGAIN) {
if (!resiting_data(user)) {
std::system("pause");
closegraph();
exit(0);
}
break;
}
}
Sleep(50);
} while (1);
fetch_level_info(level, user.level_id);
}
}
int main() {
//用户身份验证
userinfo user;
levelinfo level;
if (!login(user)) {
cout << "登录失败,请重新登录!" << endl;
::system("pause");
exit(-1);
}
init_graph();
//循环(读取关卡→用户操作)
do {
//根据用户信息加载关卡数据
int result = fetch_level_info(level, user.level_id);
judge_by_result(result, level, user);
//将关卡数据数据转换到map游戏地图中
transform_map_db2array(level, map);
//加载游戏图片
show_images(level);
//小人移动
game_operation(level, user);
//恭喜通关
promot_over();
} while (1);
std::system("pause");
closegraph();
return 0;
}
到此这篇关于C语言+MySQL实现推箱子游戏的文章就介绍到这了,更多相关C语言 MySQL推箱子内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!