c++ 练习项目1推箱子

知识准备

1 命名空间的理解。。。。。

用法1

#include <iostream>
#include <string>
namespace China {
float population = 14.1; //单位: 亿
std::string capital = "北京";
}
namespace Japan {
float population = 1.27; //单位: 亿
std::string capital = "东京";
}
using namespace Japan;
int main(void) {
std::cout << "首都:" << capital << std::endl;
std::cout << "人口:" << population << std::endl;
std::cout << "首都:" << China::capital << std::endl;
std::cout << "人口:" << China::population << std::endl;
system("pause");
return 0;
}

用法2

#include <iostream>
#include <string>
namespace China {
float population = 14.1; //单位: 亿
std::string capital = "北京";
}
namespace Japan {
float population = 1.27; //单位: 亿
std::string capital = "东京";
}
//注意:没有 namespace
//直接指定命名空间中的标识符,而不是整个域名
using China::capital;
using Japan::population;
int main(void) {
std::cout << "首都:" << capital << std::en89d7l;943840118979438401111
std::cout << "人口:" << population << std::endl;
system("pause");
return 0;
}

用法2

#include <iostream>
#include <string>
namespace China {
float population = 14.1; //单位: 亿
std::string capital = "北京";
}
namespace Japan {
float population = 1.27; //单位: 亿
std::string capital = "东京";
}
using namespace China;
using Japan::population;
int main(void) {
std::cout << "首都:" << capital << std::endl;
std::cout << "人口:" << population << std::endl; //出错!
system("pause");
return 0;
}

2 结构体

#include <iostream>
#include<string.h>
#include<graphics.h>

struct programer
{
	char name[32];
	int age;
	int salary;
};

void add_salary(struct programer p, int num)
{
	p.salary += num;
}

//使用指针
void add_salary1(struct programer *p, int num)
{
	if (!p) return; //判断指针是不是空指针
	p->salary += num;
}

//使用引用
void add_salary2(struct programer &p, int num)
{
	p.salary += num;
}

int main(void)
{
	struct programer xiaoniu;
	strcpy_s(xiaoniu.name, "小牛");
	xiaoniu.age = 28;
	xiaoniu.salary = 20000;

	// 打印的结果任然是20000,
	//结构体变量作为参数传值是值传递,和int等基本类型一致,值传递,是不会修改在函数外面的变量的
	add_salary(xiaoniu, 5000);
	printf("姓名:%s, age: %d, salary: %d", xiaoniu.name, xiaoniu.age, xiaoniu.salary);
	printf("\n");

	add_salary1(&xiaoniu, 5000);
	printf("姓名:%s, age: %d, salary: %d", xiaoniu.name, xiaoniu.age, xiaoniu.salary);
	printf("\n");

	add_salary2(xiaoniu, 10000);
	printf("姓名:%s, age: %d, salary: %d", xiaoniu.name, xiaoniu.age, xiaoniu.salary);
	printf("\n");
	system("pause");
	return 0;
}

在这里插入图片描述

(1) 结构体数组

#include <iostream>
#include<string.h>
#include<graphics.h>

struct student
{
	char name[32];
	int age;
};


int main(void)
{
	struct student s[2];

	printf("请输入第一个学生的姓名:");
	scanf_s("%s", s[0].name,sizeof(s[0].name));
	printf("请输入第一个学生的age:");
	scanf_s("%d", &s[0].age);

	printf("第一个学生姓名:%s, age: %d\n", s[0].name, s[0].age);


	printf("请输入第2个学生的姓名:");
	scanf_s("%s", s[1].name, sizeof(s[1].name));
	printf("请输入第2个学生的age:");
	scanf_s("%d", &s[1].age);

	printf("第2个学生姓名:%s, age: %d\n", s[1].name, s[1].age);

	system("pause");
	return 0;
}

(2) 结构体指针

#include <iostream>
#include<string.h>
#include<graphics.h>

struct _friend
{
	char name[32];
	char sex; //m:man, f:woman
	int age;
};


int main(void)
{
	struct _friend girl = {"小龙女",'f',18};

	struct _friend *my_girl = &girl;


	printf("小龙女姓名:%s, 性别: %s, age: %d\n", girl.name, girl.sex == 'm'?"男":"女", girl.age);


	//指针访问结构体变量的成员,有两种方式
	//直接解引用
	printf("小龙女姓名:%s, 性别: %s, age: %d\n", (*my_girl).name, (*my_girl).sex == 'm' ? "男" : "女", (*my_girl).age);

	//使用->访问
	printf("小龙女姓名:%s, 性别: %s, age: %d\n", my_girl->name, my_girl->sex == 'm' ? "男" : "女", my_girl->age);

	system("pause");
	return 0;
}

3 引用

在这里插入图片描述
引用,只能给变量,不能给常量

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
下面为什么不用初始化,因为参数传递的过程就是初始化
在这里插入图片描述

4 枚举

在这里插入图片描述
在这里插入图片描述
如果不指定元素值,就是默认按照顺序,0,1,2,3.。。如果指定了前一个,没有指定后一个,后一个就是在前一个数据的基础上+1
在这里插入图片描述

#include <iostream>
#include<string.h>
#include<graphics.h>

//第一种定义方式,常用
enum Season
{
	Spring,  //0
    Summer,  //1
	Autumn,  //2
	Winter   //3
}; 

//第二种定义方式,定义枚举的时候直接定义变量
enum Season
{
	Spring,  //0
	Summer,  //1
	Autumn,  //2
	Winter   //3
}s,s1;

//第3种定义方式,
enum 
{
	Spring,  //0
	Summer,  //1
	Autumn,  //2
	Winter   //3
}s,s1;

int main(void)
{
	enum Season s;
	s = Spring;

	enum Season s1;
	s1 = Winter;

	printf("s:%d, s1: %d \n", s,s1);

	if (s1 == Winter)
	{
		printf("冬天 \n");
	}

	//s = 0; 不允许,即使枚举类型是整数,也不能将数值复制给枚举
	//s = (enum Season)0;  强制类型转换是允许的

	system("pause");
	return 0;
}

5 类型定义

简化写法;提高程序的可移植性
在这里插入图片描述
在这里插入图片描述
宏定义个类型定义的区别;
宏定义只是一个简单的替换,
下面这个案例中,使用类型定义,定义的变量都是char *变量
但是使用宏定义,,只有第一个变量s3被定义char *, 而后者只是char, char *s3, s4
在这里插入图片描述

编程练习地址。。。。。

核心思想:创建一个二维数组,墙:0,地板:1,箱子目的地:2,人:3,箱子4,箱子命中之后,箱子目的地重新编号为5
在这里插入图片描述

在这里插入图片描述

#include <iostream>
#include<Windows.h>
#include<graphics.h>
#include<MMSystem.h>  //播放
#pragma comment(lib,"winmm.lib") //告诉编译器,加载winmm.lib库文件
#include<stdlib.h>
#include<string>
#include<conio.h>

using namespace std;

#define RATIO 70 //宏定义
#define LINE 9
#define COLLUMN 12
#define START_X 50
#define START_Y 100
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
#define isValid(pos) pos.x >= 0 && pos.x < LINE&& pos.y >= 0 && pos.y <= COLLUMN  //宏定义

//控制上下左右
#define KEY_UP     'w'
#define KEY_LEFT   'a'
#define KEY_RIGHT  'd'
#define KEY_DOWN   's'
#define KEY_QUIT   'q'

//全局变量
//int man_x = 0;
//int man_y = 0;
//int box_x[4] = { 0 };
//int box_y[4] = { 0 };

//控制游戏上下左右
enum _DIRECTION {
	UP,
	DOWN,
	LEFT,
	RIGHT
};

enum _PROPS {
	WALL,
	FLOOR,
	BOX_DES,
	MAN,
	BOX,
	HIT,
	ALL
};

IMAGE images[ALL];

//小人结构体
struct _POS {
	int x;
	int y;
};

//定义一个全局结构体变量
struct _POS man;



//游戏地图
// 二维数组建立地图:0-墙、1-地板、目的地-2、小人-3、箱子-4、箱子命中-5
int map[LINE][COLLUMN] = {
	{0,0,0,0,0,0,0,0,0,0,0,0},
	{0,1,0,1,1,1,1,1,1,1,0,0},
	{0,1,4,1,0,2,1,0,2,1,0,0},
	{0,1,0,1,0,1,0,0,1,1,1,0},
	{0,1,0,2,0,1,1,4,1,1,1,0},
	{0,1,1,1,0,3,1,1,1,4,1,0},
	{0,1,2,1,1,4,1,1,1,1,1,0},
	{0,1,0,0,1,0,1,1,0,0,1,0},
	{0,0,0,0,0,0,0,0,0,0,0,0}
};

bool isGameOver()
{
	for (int i = 0; i < LINE; i++)
	{
		for (int j = 0; j < COLLUMN; j++)
		{
			if (map[i][j] == BOX_DES)
			{
				return false;
			}
		}
	}
	return true;
}

void gameOverScene(IMAGE *bg)
{
	putimage(0, 0, bg);
	settextcolor(WHITE);
	RECT rec = { 0,0,SCREEN_WIDTH ,SCREEN_HEIGHT };
	settextstyle(20, 0, _T("宋体"));
	drawtext(_T("恭喜您~\n"),&rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

/**************************************************
*改变游戏地图有的的道具,并重新显示,相当于移动了道具
* 输入:
*    line,collum----要该u便的道具在地图中的位置
*    prop: 要在新位置显示的道具
**************************************************/

//void changeMap(int line, int column, enum _PROPS prop)
//{
//	map[line][column] = prop;
//	putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
//	//prop存的是枚举值,刚好对应的图像编号
//}
void changeMap(struct _POS *pos, enum _PROPS prop)
{
	map[pos->x][pos->y] = prop;
	putimage(START_X + pos->y * RATIO, START_Y + pos->x * RATIO, &images[prop]);
	//prop存的是枚举值,刚好对应的图像编号
}

void gameControl(enum _DIRECTION direct)
{
	//int x = man.x;
	//int y = man.y;

	struct _POS next_pos = man;
	struct _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;
	}

	//四周都是墙围起来的,不需要判断越界
	//if (next_pos.x >= 0 && next_pos.x  < LINE && next_pos.y >= 0 && next_pos.y <= COLLUMN && map[next_pos.x][next_pos.y] == FLOOR)
	//将next_pos传入isValid,就会替换宏定义里面的pos
	if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR)  //人前方是地板
	{
		//changeMap(next_pos.x, next_pos.y, MAN);
		//changeMap(man.x, man.y, FLOOR);
		changeMap(&next_pos, MAN);
		changeMap(&man, FLOOR);
		man = next_pos;  //结构体可以直接复制,相互之间
	}
	else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] == BOX)//人前方是箱子
	{
		//两种情况,箱子前面是地板或者箱子前面是箱子的目的地
		if (isValid(next_pos) && map[next_next_pos.x][next_next_pos.y] == FLOOR) {
			changeMap(&next_next_pos, BOX);
			changeMap(&next_pos, MAN);
			changeMap(&man, FLOOR);
			//changeMap(next_next_pos.x, next_next_pos.y, BOX);
			//changeMap(next_pos.x, next_pos.y, MAN);
			//changeMap(man.x, man.y, FLOOR);
			man = next_pos;
		}
		else if (isValid(next_pos) && map[next_next_pos.x][next_next_pos.y] == BOX_DES)
		{
			changeMap(&next_next_pos, HIT);
			changeMap(&next_pos, MAN);
			changeMap(&man, FLOOR);
			man = next_pos;
		}

	}
	//if (direct == UP)
	//{
	//	if (next_pos.x > 0 && map[next_pos.x][next_pos.y] == FLOOR)
	//	{
	//		changeMap(next_pos.x, next_pos.y, MAN);
	//		changeMap(man.x, man.y, FLOOR);
	//		man = next_pos;  //结构体可以直接复制,相互之间
	//	}
	//}
	//else if (direct == DOWN)
	//{
	//	if (next_pos.x < LINE && map[next_pos.x][next_pos.y] == FLOOR)
	//	{
	//		//changeMap(x + 1, y, MAN);
	//		//changeMap(x, y, FLOOR);
	//		changeMap(next_pos.x, next_pos.y, MAN);
	//		changeMap(man.x, man.y, FLOOR);
	//		man = next_pos;
	//	}
	//}
	//else if (direct == LEFT)
	//{
	//	if (next_pos.y >=0 && map[next_pos.x][next_pos.y] == FLOOR)
	//	{
	//		//changeMap(x, y-1, MAN);
	//		//changeMap(x, y, FLOOR);
	//		changeMap(next_pos.x, next_pos.y, MAN);
	//		changeMap(man.x, man.y, FLOOR);
	//		man = next_pos;
	//	}
	//}
	//else if (direct == RIGHT)
	//{
	//	if (next_pos.y < COLLUMN  && map[next_pos.x][next_pos.y] == FLOOR)
	//	{
	//		//changeMap(x, y + 1, MAN);
	//		//changeMap(x, y, FLOOR);
	//		changeMap(next_pos.x, next_pos.y, MAN);
	//		changeMap(man.x, man.y, FLOOR);
	//		man = next_pos;
	//	}
	//}
}

int main(void)
{

	initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);

	IMAGE bg_img;
	loadimage(&bg_img, _T("blackground.bmp"), 960, 768, true);
	putimage(0, 0, &bg_img);

	//加载道具目标
	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.bmp"), RATIO, RATIO, true);
	loadimage(&images[HIT], _T("box.bmp"), RATIO, RATIO, true);

	for (int i = 0; i < LINE; i++)
	{
		for (int j = 0; j < COLLUMN; j++)
		{
			putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
			if (map[i][j] == MAN)
			{
				man.x = i;
				man.y = j;
			}
		}
	}

	//游戏环节
	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 == KEY_QUIT)
			{
				quit = true;
			}
			if (isGameOver())
			{
				gameOverScene(&bg_img);
				quit = true;
			}
		}
		Sleep(100);
	}while(quit == false);

	system("pause");
	closegraph();
	return 0;
}

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值