• 【无标题】


    编译环境

    系 统Windows11

    编译器:Visual Studio 2022

    图形库EasyX


    源代码

    #include
    #include
    const int WIDTH = 400;
    const int HEIGHT = 760;
    const int INTERVAL = 48;
    
    IMAGE img_bk;
    IMAGE img_item[3];
    
    void loadImg()
    {
    	loadimage(&img_bk, "images/bk.jpg", WIDTH, HEIGHT);
    	loadimage(img_item + 0, "images/cao.png",48,48);
    	loadimage(img_item + 1, "images/lingdang.png", 48, 48);
    	loadimage(img_item + 2, "images/shu.png", 48, 48);
    }
    
    int main()
    {
    	initgraph(WIDTH, HEIGHT,EW_SHOWCONSOLE);
    	loadImg();
    
    	int map[3][3] = {0};
    	//初始化地图
    	for (int r = 0; r < 3; r++)
    	{
    		for (int c = 0; c < 3; c++)
    		{
    			map[r][c] = r+1;	//1 2 3
    		}
    	}
    	//打乱数组
    	for (int r = 0; r < 3; r++)
    	{
    		for (int c = 0; c < 3; c++)
    		{
    			int r1 = rand() % 3;
    			int c1 = rand() % 3;
    			int t = map[r1][c1];
    			map[r1][c1] = map[r][c];
    			map[r][c] = t;
    		}
    	}
    
    	int store[7] = { 0 };
    
    	int offsetX = (WIDTH - (2 * (48 + INTERVAL) + 48)) / 2;
    	int offsetY = (HEIGHT - (2 * (48 + INTERVAL) + 48)) / 2;
    	while (true)
    	{
    		BeginBatchDraw();
    		//绘制地图
    		putimage(0, 0, &img_bk);
    		//输出图片
    		for (int r = 0; r < 3; r++)
    		{
    			for (int c = 0; c < 3; c++)
    			{
    				if(map[r][c])
    					putimage(offsetX + c * (48 + INTERVAL), offsetY + r * (48 + INTERVAL), img_item + (map[r][c]-1));
    			}
    		}
    		//绘制下面
    		for (int i = 0; i < 7; i++)
    		{
    			if (store[i])
    				putimage((i * 50)+26, 620, img_item + (store[i]-1));
    		}
    
    
    		EndBatchDraw();
    		ExMessage msg = { 0 };
    		if (peekmessage(&msg) && msg.message == WM_LBUTTONDOWN)
    		{
    			//鼠标坐标判断
    			for (int r = 0; r < 3; r++)
    			{
    				for (int c = 0; c < 3; c++)
    				{
    					int x = offsetX + c * (48 + INTERVAL);
    					int y = offsetY + r * (48 + INTERVAL);
    					//printf("pos(%d,%d)\n", x, y);
    					//判断鼠标是否点击了图片
    					if (msg.x > x && msg.x < x + 48 && msg.y>y && msg.y < y + 48)
    					{
    						//求出点击的图片对应的数组下标
    						int col = (msg.x - offsetX)  /  (48 + INTERVAL);
    						int row = (msg.y - offsetY)  / (48 + INTERVAL);
    						//printf("row:%d col:%d\n", row, col);
    						//把点击的图片添加到下面
    						for (int i = 0; i < 7; i++)
    						{
    							if (store[i] == 0)
    							{
    								store[i] = map[row][col];
    								break;
    							}
    						}
    
    						//如果下面有三个一样的就消除
    						int cnt = 0;
    						for (int i = 0; i < 7; i++)
    						{
    							if (store[i] == map[row][col])
    							{
    								cnt++;
    							}
    						}
    						if (cnt == 3)
    						{
    							for (int i = 0; i < 7; i++)
    							{
    								if (store[i] == map[row][col])
    								{
    									store[i] = 0;
    								}
    							}
    						}
    
    						//把点击的图片从map中删除
    						map[row][col] = 0;					
    					}
    				}
    			}
    		}
    
    	}
    
    	while (1);
    	return 0;
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131

    总结

    代码不难也不多,跟上线的游戏比还有很多可以完善优化的地方

    如果有什么不懂的地方可以私信问我,素材可以自行准备哦,不想找的小伙伴可以访问我主页的粉丝君¥下载。

    总的来说,能够自己摸透的话写出一个类似的游戏还是很容易的。

    最后喜欢蜜糖的可以支持一下点赞关注我哟,今天就到这里啦,我们下次再见!

  • 相关阅读:
    mysql drop table 死锁
    洛谷P3092 状压dp
    苹果V3签名是什么?优势是什么?什么场合需要应用到?该怎么部署?
    CodeForces - 667A Pouring Rain
    2023开放原子全球开源峰会——Intel专题探访
    map和set
    力扣-167号题.两数之和II-输入有序的数组
    【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
    iOS开发之iOS15.6之后拉流LFLiveKit,画面模糊及16.1马赛克问题
    ref实现input自动获取光标并执行多次
  • 原文地址:https://blog.csdn.net/weixin_45713725/article/details/126910559