C/C++推箱子游戏

推箱子游戏

  • 项目名称:推箱子
  • 所用知识点:C/C++ 基础知识
  • 所用工具:vs2013&Easyx

程序思路分析
在这里插入图片描述
(效果图)
在这里插入图片描述

程序思路:
第一步:创建窗口

#include<graphics.h>
#include<stdio..h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
system ("pause");
return 0;
}

第二步:贴图(贴图三部曲:定义图片,加载图片,显示图片)

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
IMAGE  b;//定义图片
loadimage(&b, "b.bmp", 50, 50);//加载图片
putimage(j * 50, i * 50, &b);//显示图片
	
system ("pause");
return 0;
}`


第三步:箱子的移动(图片的移动)

{
	int x, y;//表示人物所在位置的下标
	for (int j = 0; j < 8; j++)
	{
		for (int i = 0; i < 8; i++)
		{
			if (Map[j][i] == 5 || Map[j][i] == 8)
			{

				
					x = j;
					y = i;

				
					
			}
		}
	}

	char key = _getch();
	//Sleep(30);
	switch (key)//获取按键
	{

	
		//向上
	case 72:
	case 'w':
	case 'W':
		if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
		{
			Map[x - 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
			if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
			{
				Map[x - 2][y] += 4;
				Map[x - 1][y] += 1;
				Map[x][y] -= 5;
			}

		break;
	case 80:
	case 's':
	case 'S':
		if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
		{
			Map[x + 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
			if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
			{
				Map[x + 2][y] += 4;
				Map[x + 1][y] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 77:
	case 'd':
	case 'D':
		if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
		{
			Map[x][y + 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
			if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
			{
				Map[x][y + 2] += 4;
				Map[x][y + 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 75:
	case 'a':
	case 'A':
		if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
		{
			Map[x][y - 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
			if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
			{
				Map[x][y - 2] += 4;
				Map[x][y - 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	}

程序的源代码


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
IMAGE  a, aby, b, c, d, dby, e, f;
void InitImg()
{

	//1.加载背景
	loadimage(&f, "f.jpg", 400, 400);
	//加载小鸟
	loadimage(&a, "a.bmp", 50, 50);
	loadimage(&aby, "aby.bmp", 50, 50);

	loadimage(&b, "b.bmp", 50, 50);
	loadimage(&c, "c.bmp", 50, 50);
	loadimage(&d, "d.bmp", 50, 50);
	loadimage(&dby, "dby.bmp", 50, 50);
	loadimage(&e, "e.bmp", 50, 50);
}
int Map[8][8] = {
	{ 0, 0, 1, 1, 1, 0, 0, 0 },
	{ 0, 0, 1, 3, 1, 0, 0, 0 },
	{ 0, 0, 1, 0, 1, 1, 1, 1 },
	{ 1, 1, 1, 4, 0, 4, 3, 1 },
	{ 1, 3, 0, 4, 5, 1, 1, 1 },
	{ 1, 1, 1, 1, 4, 1, 0, 0 },
	{ 0, 0, 0, 1, 3, 1, 0, 0 },
	{ 0, 0, 0, 1, 1, 1, 0, 0 },

};
void DrawMap()
{

	putimage(0, 0, &f);
	int i, j;
	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 8; j++) {
			switch (Map[i][j]) {
			case 0:printf("  ");

				break;
			case 1:printf("@");
				putimage(j * 50, i * 50, &b);

				break;
			case 3:printf("#");
				putimage(j * 50, i * 50, &d, SRCAND);
				putimage(j * 50, i * 50, &dby, SRCPAINT);

				break;
			case 4:printf("$");
				putimage(j * 50, i * 50, &c);
				break;
			case 5:printf("%");
				putimage(j * 50, i * 50, &a, SRCAND);
				putimage(j * 50, i * 50, &aby, SRCPAINT);

				break;
				case 7:
					printf("^");
					putimage(j * 50, i * 50, &e);
					break;

			}

		}
		printf("\n");



	}
}
void key_Down()
{
	int x, y;//表示人物所在位置的下标
	for (int j = 0; j < 8; j++)
	{
		for (int i = 0; i < 8; i++)
		{
			if (Map[j][i] == 5 || Map[j][i] == 8)
			{

				
					x = j;
					y = i;

				
					
			}
		}
	}

	char key = _getch();
	//Sleep(30);
	switch (key)//获取按键
	{

	
		//向上
	case 72:
	case 'w':
	case 'W':
		if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
		{
			Map[x - 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
			if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
			{
				Map[x - 2][y] += 4;
				Map[x - 1][y] += 1;
				Map[x][y] -= 5;
			}

		break;
	case 80:
	case 's':
	case 'S':
		if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
		{
			Map[x + 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
			if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
			{
				Map[x + 2][y] += 4;
				Map[x + 1][y] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 77:
	case 'd':
	case 'D':
		if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
		{
			Map[x][y + 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
			if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
			{
				Map[x][y + 2] += 4;
				Map[x][y + 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 75:
	case 'a':
	case 'A':
		if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
		{
			Map[x][y - 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
			if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
			{
				Map[x][y - 2] += 4;
				Map[x][y - 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	}



	}
	int main()
	{


		initgraph(400, 400);
		InitImg();
		DrawMap();
		while (1)
		{


			
			DrawMap();
			key_Down();
			system("cls");
		}
		getchar();
		system("pause");

	}



  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
简易推箱子 #include "Map.h" #include <conio.h> int XDest = 4; int YDest = 4; void CMap::Init() { int i,j; CPoint cPoint; CSprite *pSphte; CPlayer cPlay; CBox cBox; CDest cDest; int XPlayer = 4; int YPlayer = 6; int XBox = 4; int YBox = 5; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (i == 0 || j == 0 || i == 9 || j == 9) { cPoint.SetPoint(i, j); pSphte = new CWall(cPoint); m_nMap[i][j] = pSphte; } else if (i == XPlayer && j == YPlayer) { cPoint.SetPoint(i,j); pSphte = new CPlayer(cPoint); m_nMap[i][j] = pSphte; } else if (i == XBox && j == YBox) { cPoint.SetPoint(i,j); pSphte = new CBox(cPoint); m_nMap[i][j] = pSphte; } else if (i == XDest && j == YDest) { cPoint.SetPoint(i,j); pSphte = new CDest(cPoint); m_nMap[i][j] = pSphte; } else { cPoint.SetPoint(i, j); pSphte = new CSpace(cPoint); m_nMap[i][j] = pSphte; } } cout<<endl; } } void CMap::Draw() { int i,j; for (i=0; i<10; i++) { for (j=0; j<10; j++) { cout<<m_nMap[i][j]->GetSprite(); } cout<<endl; } } void CMap::Logic() { int n = getch(); //获取输入的数字 char ch = char(n);//将获取到的数字进行强制类型转换 CPlayer *pPlayer = GetPlayer(); int XPlayer = pPlayer->GetX(); int YPlayer = pPlayer->GetY(); CBox *pBox = GetBox(); int XBox = pBox->GetX(); int YBox = pBox->GetY(); CDest *pDest = GetDest(); switch(ch) { case 'a': { int yPlayer = pPlayer->GetY(); YPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer++; YBox++; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'd': { int yPlayer = pPlayer->GetY(); YPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer--; YBox--; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'w': { int xPlayer = pPlayer->GetX(); XPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer++; XBox++; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; case 's': { int xPlayer = pPlayer->GetX(); XPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer--; XBox--; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; default: break; } if ((pPlayer->GetX() == XDest && pPlayer->GetY() == YDest) || (pBox->GetX() == XDest && pBox->GetY() == YDest)) { int i,j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { m_nMap[i][j]->SetSprite(" "); } } } } else if (m_nMap[XDest][YDest]->GetSprite() == " ") { m_nMap[XDest][YDest]->SetSprite("◎"); } if (m_nMap[XDest][YDest]->GetSprite() == "■") { m_nMap[XDest][YDest]->SetSprite("●"); // cout<<"游戏结束"<<endl; } if (pBox->GetX() != XDest || pBox->GetY() != YDest) { pBox->SetSprite("■"); } } CPlayer *CMap::GetPlayer(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "♀") { return (CPlayer *)m_nMap[i][j]; } } } } CBox *CMap::GetBox(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "■" || m_nMap[i][j]->GetSprite() == "●") { return (CBox *)m_nMap[i][j]; } } } } CDest *CMap::GetDest(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { return (CDest *)m_nMap[i][j]; } } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code.xinxixue.top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值