【C++】2048游戏系列---优化模块第一稿【加载图片】

【C++】2048游戏系列---优化模块第一稿【加载图片】

(更新中……)
参考博客:
https://blog.csdn.net/qq_39151563/article/details/104283217
https://blog.csdn.net/qq_39151563/article/details/104342530
由于放在一篇会导致篇幅太长,所以分成了几篇。(可能有个10篇吧=.=)

目前写完的:


本篇介绍如何把数字转化为图片输出
相关图片资源下载:2048图片 提取码:u0h3


一、图片的命名

图片的命名

2的1次方 到 2的17次方,本篇用到背景图、格子图和gameOver图, 共19张图。

二、图片加载

  1. sprintf格式化字符串
#include <iostream>
#include "graphics.h" 
using namespace std;
int main()
{
	char imgAdress[40];
	//PIMAGE BlockImgs[18];//EGE图片形式 
	for(int i=1,num=2; i<18; i++,num*=2)
	{
		sprintf(imgAdress,"image\\block_%d.png",num);	
		cout << imgAdress << endl;
	}
	
	return 0;
} 

运行结果是图片的相对路径

sprintf调试结果

  1. EGE图片操作

    • 获取图像 getimage()

    参考博客里写了很多,这里这用 getimage(BlockImgs[i], imgAdress);

    第一个参数是PIMAGE对象,第二个参数是图片地址(建议用相对路径,这样方便移动文件夹)

    要把图片文件夹放在于main.cpp同一目录下

    imgAddress地址的图片”赋 给“BlockImgs[i]对象,这样我们就可以在程序中进行引用了。

    • 绘制图像 putimage()

    最常用的,putimage(x, y, pimg);

    本篇用的:putimage_withalpha(NULL,pimg,x,y)

    void EGEAPI putimage
    (
    	int dstX,
    	int dstY,				//目标位置(X,Y)
    	const PIMAGE pSrcImg,	//源图像
    	DWORD dwRop = SRCCOPY	//最后一个参数是三元光栅操作码,不用管它,不写就行,
        						//因为它有默认参数SRCCOPY (默认参数就是如果不写,就传入默认的值)。
    ); 
    
    int putimage_withalpha(
    	PIMAGE imgdest,		// 目标图像
    	PIMAGE imgsrc,		// 源图像
        int dstX,    		// 目标图像左上角x坐标 
        int dstY,    		// 目标图像左上角y坐标
    	int srcX = 0,		// 源图像左上角x坐标
        int srcY = 0,		// 源图像左上角y坐标
        int srcWidth = 0,   // 原图像混合区域宽度
        int srcHeight = 0   // 源图像混合区域高度
    );
    

三、图片的尺寸

  1. 背景图片尺寸:500*500背景图片尺寸

  2. Block图片尺寸:106*106Block图片尺寸

  3. 尺寸计算:

尺寸计算
图中红色的点(x,y)就是我们要放照片函数putimage_withalpha(NULL,pimg,x,y)里面的参数

  1. 遍历红点放照片

    const int DEVIDE = 15;
    const int GRID_WIDTH = 106;
    
    void Draw()
    {
    	putimage_withalpha(NULL,BlockImgs[0],0,0);//放背景
    	for(int i=0;i<4;i++)
    	{
    		
    		for(int j=0;j<4;j++)
    		{
    			int x = (j+1)*DEVIDE + j*GRID_WIDTH;
    			int y = (i+1)*DEVIDE + i*GRID_WIDTH;
    			//cout << "(x,y) = " << "(" << x << ","<< y << ")" << endl;
    			if(grid[i][j]!=0)
    			{
    				putimage_withalpha(NULL,BlockImgs[grid[i][j]],x,y);
    			}
    		}
    	}
    	
    }
    
    

    运行结果:
    添加图片调试

四、图片的释放

在图片无用后或游戏结束后,应该释放图片内存空间

对应的函数:delimage(pimg);

void ReleaseImgs()
{
	for(int i=0; i<18; i++)
	{
		delimage(BlockImgs[i]);
	}
	delimage(GameOverImg);
}

五、所有的代码

2048-优化篇一稿-加载图片

#include <iostream>
#include "graphics.h"
using namespace std;
//测试矩阵 
int grid[4][4] = {0
//    {1,2,3,4},
//    {4,5,6,7},
//    {7,8,9,10},
//    {1,1,1,0}
};

int EmptyBlock = 4 ;	//空格数 
int dir = -1;				// 0-左,1-上,2-右,3-下 
//打印函数 
void PrintGrid()
{
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
            cout << grid[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}
//计算空格函数 
int CalculateEmpty()
{
    int cnt = 0;
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            if(grid[i][j]==0)   cnt++;
    return cnt;
}

//显示信息
void ShowInfo()
{
	cout << "dir = " << dir << endl;
    cout<< "EmptyBlock = " << CalculateEmpty() << endl;
	cout << "grid[4][4] = " << endl;
	PrintGrid();
} 

//移动函数 
static int x0[4] = {0, 0, 3, 0};
static int y0[4] = {0, 0, 0, 3};
static int firstOffset[4][2]  = {{1,0},{0,1},{-1,0},{0,-1}};
static int secondOffset[4][2] = {{0,1},{1,0},{0,1} ,{1,0}};
void Move(int dir)
{
	//bool moved = false; 
	if(dir==-1)	return;
    int tx, ty;
    int t1x, t1y;
    int t2x, t2y;
    for(int i=0; i<4; i++)
    {
    	
        tx = x0[dir] + i*secondOffset[dir][0];
        ty = y0[dir] + i*secondOffset[dir][1];
       //cout << "(" << tx << ", " << ty << ")" << endl;
       
        t1x = tx;
        t1y = ty;
        t2x = tx + firstOffset[dir][0]; 
        t2y = ty + firstOffset[dir][1];
        for( ;t2x>=0&&t2x<=3&&t2y>=0&&t2y<=3; t2x+=firstOffset[dir][0],t2y+=firstOffset[dir][1])
        {
            if(grid[t2y][t2x]!=0)
            {
                if(grid[t1y][t1x]==0)
                {
                    grid[t1y][t1x] = grid[t2y][t2x];
                    grid[t2y][t2x] = 0;
                   // moved = true;
                }
                else if(grid[t1y][t1x]==grid[t2y][t2x])
                {
                    grid[t1y][t1x]++;
                    grid[t2y][t2x] = 0;
                    t1x += firstOffset[dir][0];
                    t1y += firstOffset[dir][1];
                   // moved = true;
                }
                else if(t1x+firstOffset[dir][0]!=t2x||t1y+firstOffset[dir][1]!=t2y)
                {
                    grid[t1y+firstOffset[dir][1]][t1x+firstOffset[dir][0]] = grid[t2y][t2x];
                    grid[t2y][t2x] = 0;
                    t1x += firstOffset[dir][0];
                    t1y += firstOffset[dir][1];
                    //cout << "Move Test" << endl;
                   // moved = true;
                }
                else
                {
                    t1x += firstOffset[dir][0];
                    t1y += firstOffset[dir][1];
                }
            }
            
        }
    }
    //return moved;
}

//添加新数
void Addnum(int n)
{	
	while(n--)//添加n个
	{
		EmptyBlock = CalculateEmpty();
		if(EmptyBlock<=0)	
		{
			//cout << "addnum EmptyBlock = " << EmptyBlock << endl; 
			//cout << "addnum Test1" << endl;
			return;	
		}
		int cnt = random(EmptyBlock)+1;//随机得到一个空格数以内的数	
		//cout << "找到第" << cnt << "个空位" << endl; 
		//cout << "cnt = " << cnt << endl;

		int *p = &grid[0][0]-1;//记录矩阵首地址前一个 ,因为后面的 p 在找到时还会 ++ 
		//cout << "n = "<< n <<endl;
		for(int i=0; i<4&&cnt; i++)
			for(int j=0; j<4&&cnt; j++)
			{
				if(grid[i][j]==0 && cnt)//如果有空格并且cnt有效	
				{
					//cout << "cnt = " << cnt << endl;
					cnt--;//找到一个划一个
				}
                p++;//p 指向下一个再进行判断
			}
        //循环结束时 p 指向我们之前随机指定的空格
		*p = (random(10)==0)?2:1;// 0.1 的概率为2,0.9 的概率为1
		//cout << "插入成功" << endl; 
		//*p = (random(10)==0)+1;//这样写也可以
		EmptyBlock--;
	}
}

PIMAGE BlockImgs[18];//EGE图片形式 
PIMAGE GameOverImg;
//加载图片
void LoadImgs()
{
	char imgAdress[40];
	for(int i=1,num=2; i<18; i++,num*=2)
	{
		
		sprintf(imgAdress,"image\\block_%d.png",num);
		BlockImgs[i] = newimage();	
      	getimage(BlockImgs[i], imgAdress);
		//cout << imgAdress << endl;
	}
	BlockImgs[0] = newimage();
	GameOverImg = newimage();
	getimage(BlockImgs[0],"image\\background.png");	
	getimage(GameOverImg,"image\\gameOver.png"); 
	cout<< "读取图片成功" << endl;	
}
//释放图片 
void ReleaseImgs()
{
	for(int i=0; i<18; i++)
	{
		delimage(BlockImgs[i]);
	}
	delimage(GameOverImg);
}

const int DEVIDE = 15;
const int GRID_WIDTH = 106;
void Draw()
{
	cleardevice();
	putimage_withalpha(NULL,BlockImgs[0],0,0);
	for(int i=0;i<4;i++)
	{	
		for(int j=0;j<4;j++)
		{
			int x = (j+1)*DEVIDE + j*GRID_WIDTH;
			int y = (i+1)*DEVIDE + i*GRID_WIDTH;
			//cout << "(x,y) = " << "(" << x << ","<< y << ")" << endl;
			if(grid[i][j]!=0)
			{
				putimage_withalpha(NULL,BlockImgs[grid[i][j]],x,y);
			}
		}
	}
	
}

bool gameOver()
{
	EmptyBlock = CalculateEmpty();
	if(EmptyBlock>0)	return false;
	for(int i=0;i<4;i++)
	{
		int t1=0,t2=1;
		while(t2<=3)
		{
			if(grid[i][t1]==grid[i][t2] || grid[t1][i]==grid[t2][i])//  横 ||纵 
			{
				return false;
			}
			else
			{
				t1++;
				t2++;
			}
		}
	}
	return true;
}
 
int main()
{
	initgraph(500, 500);
	setbkcolor(WHITE);
	
	Addnum(2); //在随机2个位置添加新数 
	ShowInfo();
	LoadImgs();
	Draw();   
    for ( ; is_run(); delay_fps(60) )
	{
		//cleardevice();
		
		// todo: 逻辑更新(数据更新)
		//按键检测	
		while(kbmsg())
		{
		    key_msg keyMsg = getkey();
		    if(keyMsg.msg == key_msg_down)
		    {
		        switch(keyMsg.key)
		        {
		            case 'A':case key_left  : dir = 0; break;//左 
		            case 'W':case key_up  	: dir = 1; break;//上 
		            case 'D':case key_right : dir = 2; break;//右 
		            case 'S':case key_down	: dir = 3; break;//下 
		        }
		    }
		}   
		// todo: 图形更新
		if(dir!=-1)
		{
			system("cls");
			switch(dir)
		        {
		            case  0: cout << "按下了 A/左 键" << endl; break;//左 
		            case  1: cout << "按下了 W/上 键" << endl; break;//上 
		            case  2: cout << "按下了 D/右 键" << endl; break;//右 
		            case  3: cout << "按下了 S/下 键" << endl; break;//下 
		        }
		               
		    bool flag = false;    //移动标志位 
			int tempGrid[4][4];	 //暂存数组 
			int i,j; 
			for(i=0;i<4;i++)
				for(j=0;j<4;j++)
					tempGrid[i][j] = grid[i][j];
			Move(dir);
			//比较 
			for(i=0; i<4; i++)
				for(j=0; j<4; j++)
					if(grid[i][j]!=tempGrid[i][j])	
					{
						flag = true;
						break;
					} 
			if(flag)
			{
				cout << "有效移动" << endl;
				Addnum(1);
			}	
			else	cout << "无效移动" << endl;					 
			ShowInfo();
			cout << "gameover: " << (gameOver()?"是":"否") << endl; 
			Draw();
			dir = -1;//将 dir 置为无效,否则控制台会一直刷新 
		}
		if(gameOver())
		{
			cout << "Game Over!" << endl;
			//putimage(150,150,GameOver);
			putimage_withalpha(NULL,GameOverImg,120,200);
			break;
		}		
	} 
	ReleaseImgs();
	getch();
    closegraph();  
    return 0;
}

六、运行结果

运行结果


本篇就结束了,介绍了加载图片的原理和操作,不过是不是感觉有点单调呢?那是因为我们没有计分,没有“正向反馈”,缺少了游戏的乐趣,下一部分的优化,就是加入计分模块,加油!加油

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值