fqy131314

推箱子小游戏(c++实现)

项目实现准备工作:

1.安装easyX,vs2010版本。

以下是自己写的代码

源代码:

#include <iostream>
#include <Windows.h>
#include <string>
#include <graphics.h>
#include <conio.h>
#include "box_man.h"

IMAGE img_res[ALL];

void changeMap(struct _POS *next_POS,enum _POR prop)
{
	map[next_POS->x][next_POS->y]=prop;
	putimage(next_POS->y*RATIO+Shift_X,next_POS->x*RATIO+Shift_Y,&img_res[prop]);
}


void gameCotrol(enum _DIRECT direct)
{
	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(isValid(next_pos)&&map[next_pos.x][next_pos.y]==FLOOR)
	{
		changeMap(&next_pos,MAN);
		changeMap(&man,FLOOR);
		man=next_pos;

	}else if(isValid(next_pos)&&map[next_pos.x][next_pos.y]==BOX)
	{
		if(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;
		}else if(map[next_next_pos.x][next_next_pos.y]==FLOOR)
		{
			changeMap(&next_next_pos,BOX);
			changeMap(&next_pos,MAN);
			changeMap(&man,FLOOR);
			man=next_pos;
		}
	}
}

void gameOver(IMAGE *black_ground)
{
	putimage(0,0,black_ground);
	settextcolor(YELLOW);
	RECT rec={0,0,WEIGHT,HEIGHT};
	settextstyle(20,0,L"宋体");
	drawtext(L"恭喜你通关~\n",&rec,DT_CENTER|DT_VCENTER|DT_SINGLELINE);	//&rec:参照这个矩形,DT_CENTER:水平居中,DT_VCENTER:垂直居中,DT_SINGLELINE:文字显示单行

}

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;
}


int main(void)
{
	IMAGE bg_bk;
	initgraph(WEIGHT,HEIGHT);

	loadimage(&bg_bk,L"blackground.bmp",960,768);
	putimage(0,0,&bg_bk);

	loadimage(&img_res[WALL],L"wall.bmp",RATIO,RATIO);
	loadimage(&img_res[FLOOR],L"floor.bmp",RATIO,RATIO);
	loadimage(&img_res[BOX_DES],L"des.bmp",RATIO,RATIO);
	loadimage(&img_res[MAN],L"man.bmp",RATIO,RATIO);
	loadimage(&img_res[BOX],L"box.bmp",RATIO,RATIO);
	loadimage(&img_res[HIT],L"box.bmp",RATIO,RATIO);

	for(int i=0;i<LINE;i++)
	{
		for(int j=0;j<COLUMN;j++)
		{
			if(map[i][j]==MAN)
			{
				man.x=i;
				man.y=j;
			}
		putimage(RATIO*j+Shift_X,RATIO*i+Shift_Y,&img_res[map[i][j]]);
		}
	}

	bool quit=true;

	do
	{
		
		if(_kbhit())
		{
			char ch=_getch();
			cout<<ch<<endl;

			if(ch==KEY_UP)
			{
				gameCotrol(UP);

			}else if(ch==KEY_DOWN)
			{
				gameCotrol(DOWN);

			}else if(ch==KEY_LEFT)
			{
				gameCotrol(LEFT);

			}else if(ch==KEY_RIGHT)
			{
				gameCotrol(RIGHT);

			}else if(ch==KEY_QUIT)
			{
				quit=false;
			}
		}

		if(isGameOver())
		{
			quit=false;
			gameOver(&bg_bk);	//结束场景
		}

		Sleep(100);

	}while(quit==true);

	system("pause");

	closegraph();
	
	return 0;
}

头文件:

#define LINE 9
#define COLUMN 12

#define RATIO 61

#define WEIGHT 960
#define HEIGHT 768

#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'

#define Shift_X	100
#define Shift_Y	150

#define isValid(pos)	pos.x>=0&&pos.x<LINE&&pos.y>=0&&pos.y<COLUMN

using namespace std;

int map[LINE][COLUMN]={ 
		{ 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}};

struct _POS
{
	int x;
	int y;
};


struct _POS man;


enum _DIRECT
{
	UP,
	DOWN,
	LEFT,
	RIGHT
};


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

很简单有趣,初学者可以多试试。

posted on   会飞的鱼-blog  阅读(103)  评论(0编辑  收藏  举报  

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示