C语言小游戏——4*8华容道拼图游戏(有源码)

C语言小游戏——4*8拼图游戏(有源码)

视频介绍

用入门C语言编写4*8拼图游戏

图片展示

在这里插入图片描述

功能介绍

  • 原图反向随机打乱,百分之百有解
  • 鼠标右键查看原图
  • 随机加载背景音乐、移动音效等
  • 随机加载图片
  • 源码如下,所用到的资源包及成品exe见上面视频链接

源码(编译环境:vs2013)

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>//easyX图形库安装(浏览器搜索easyX进入官网即可下载)
#include<time.h>
#include<windows.h>
//多媒体播放接口
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define M 100

/*
可视化窗口,结合图形编程:easyX图形库安装
*/

//定义图片变量
IMAGE ph, blank, ph1;

//定义一维数组和二维数组
int arr[31] = { 0 };
int map[4][8] = { 0 };

//初始化数组
void initMap()
{
	int i, j, m = 0;
	int pos;
	//初始化一维数组
	for (i = 0; i<32; i++)
	{
		arr[i] = i;//元素初始为0-31
	}
	//初始化二维数组 (for循环双重循环)
	for (i = 0; i<4; i++)
	{
		for (j = 0; j<8; j++)
		{
			//把一维数组的元素给二维数组
			pos = m;
			map[i][j] = arr[pos];
			m++;
		}
	}
}


//打印地图
void drawMap()
{
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<8; j++)
		{
			int x = j * M;
			int y = i * M;
			//根据二维数组元素贴图
			switch (map[i][j])
			{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
				putimage(x, y, M, M, &ph, map[i][j] * M, 0);
				break;
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
				putimage(x, y, M, M, &ph, (map[i][j] - 8) * M, M);
				break;
			case 16:
			case 17:
			case 18:
			case 19:
			case 20:
			case 21:
			case 22:
			case 23:
				putimage(x, y, M, M, &ph, (map[i][j] - 16) * M, 2*M);
				break;
			case 24:
			case 25:
			case 26:
			case 27:
			case 28:
			case 29:
			case 30:
				putimage(x, y, M, M, &ph, (map[i][j] - 24) * M, 3*M);
				break;
			case 31:     //只能贴白块
				putimage(x, y, &blank);
				break;
			}
		}

	}
}


//定位白块的位置
int searchPos_i()
{
	//遍历找
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<8; j++)
		{
			if (map[i][j] == 31)
				return i;//找到了,返回i
		}
	}
	return -1;
}

int searchPos_j()
{
	//遍历找
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<8; j++)
		{
			if (map[i][j] == 31)
				return j;//找到了,返回i
		}
	}
	return -1;
}


//随机打乱
void machine()
{
	//srand((unsigned)time (NULL));//随机函数种子
	//鼠标操作,鼠标交互
	//定义鼠标消息变量
	int m, n;
	//drawMap();
	//定位白块的位置:找数组下标
	int pos_i = searchPos_i();
	int pos_j = searchPos_j();
	//定义鼠标点击的下标
	int mouse_i = 0;
	int mouse_j = 0;
	//获取随机数
	m = rand() % 4*M;
	n = rand() % 8*M;
	//将随机数转换成模拟鼠标位置
	mouse_i = m / M;
	mouse_j = n / M;
	//上
	if (mouse_i == pos_i - 1 && mouse_j == pos_j)
	{
		map[pos_i][pos_j] = map[mouse_i][mouse_j];
		map[mouse_i][mouse_j] = 31;
	}
	//下
	if (mouse_i == pos_i + 1 && mouse_j == pos_j)
	{
		map[pos_i][pos_j] = map[mouse_i][mouse_j];
		map[mouse_i][mouse_j] = 31;
	}
	//左
	if (mouse_i == pos_i && mouse_j == pos_j - 1)
	{
		map[pos_i][pos_j] = map[mouse_i][mouse_j];
		map[mouse_i][mouse_j] = 31;
	}
	//右
	if (mouse_i == pos_i && mouse_j == pos_j + 1)
	{
		map[pos_i][pos_j] = map[mouse_i][mouse_j];
		map[mouse_i][mouse_j] = 31;
	}
}


//通过鼠标移动
void palyGame()
{
	//鼠标操作,鼠标交互
	//定义鼠标消息变量
	ExMessage m;
	while (map[0][0] != 0 || map[0][1] != 1 || map[0][2] != 2 || map[0][3] != 3 || map[0][4] != 4 || map[0][5] != 5 || map[0][6] != 6 || map[0][7] != 7 || map[1][0] != 8 || map[1][1] != 9 || map[1][2] != 10 || map[1][3] != 11 || map[1][4] != 12 || map[1][5] != 13 || map[1][6] != 14 || map[1][7] != 15 || map[2][0] != 16 || map[2][1] != 17 || map[2][2] != 18 || map[2][3] != 19 || map[2][4] != 20 || map[2][5] != 21 || map[2][6] != 22 || map[2][7] != 23 || map[3][0] != 24 || map[3][1] != 25 || map[3][2] != 26 || map[3][3] != 27 || map[3][4] != 28 || map[3][5] != 29 || map[3][6] != 30 || map[3][7] != 31)
	{
		//定位白块的位置:找数组下标
		int pos_i = searchPos_i();
		int pos_j = searchPos_j();
		//定义鼠标点击的下标
		int mouse_i = 0;
		int mouse_j = 0;
		//获取鼠标消息
		m = getmessage();

		//右键查看全图功能
		if (m.message==WM_RBUTTONDOWN)
		{
			//mciSendString("close BGM1",NULL,0,NULL);
			putimage(0, 0, &ph);
		}
		//松开右键取消查看
		if (m.message==WM_RBUTTONUP)
			drawMap();

		//判断当前鼠标消息是否是鼠标左键按下
		if (m.message==WM_LBUTTONDOWN)
		{
			//播放移动音效
		//	mciSendString("close BGM2", NULL, 0, NULL);
		//	mciSendString("open ./res/daji.mp3 alias BGM2", 0, 0, 0);
		//	mciSendString("play BGM2", 0, 0, 0);

			//交换处理
			mouse_i = m.y / M;
			mouse_j = m.x / M;

			//上
			if (mouse_i == pos_i - 1 && mouse_j == pos_j)
			{
				map[pos_i][pos_j] = map[mouse_i][mouse_j];
				map[mouse_i][mouse_j] = 31;
				drawMap();
			}
			//下
			if (mouse_i == pos_i + 1 && mouse_j == pos_j)
			{
				map[pos_i][pos_j] = map[mouse_i][mouse_j];
				map[mouse_i][mouse_j] = 31;
				drawMap();
			}
			//左
			if (mouse_i == pos_i && mouse_j == pos_j - 1)
			{
				map[pos_i][pos_j] = map[mouse_i][mouse_j];
				map[mouse_i][mouse_j] = 31;
				drawMap();
			}
			//右
			if (mouse_i == pos_i && mouse_j == pos_j + 1)
			{
				map[pos_i][pos_j] = map[mouse_i][mouse_j];
				map[mouse_i][mouse_j] = 31;
				drawMap();
			}
		}
	}
	putimage(0, 0, &ph);//游戏胜利显示图
}


//随机播放背景音乐
void playBgm()
{
	int y;
	y = rand() % 8;
	//printf("音乐%d\n",y);
	switch (y)
	{
	case 0:mciSendString("open ./res/beijingyinyue0.mp3 alias BGM1", 0, 0, 0);
		break;
	case 1:mciSendString("open ./res/beijingyinyue1.mp3 alias BGM1", 0, 0, 0);
		break;
	case 2:mciSendString("open ./res/beijingyinyue2.mp3 alias BGM1", 0, 0, 0);
		break;
	case 3:mciSendString("open ./res/beijingyinyue3.mp3 alias BGM1", 0, 0, 0);
		break;
	case 4:mciSendString("open ./res/beijingyinyue4.mp3 alias BGM1", 0, 0, 0);
		break;
	case 5:mciSendString("open ./res/beijingyinyue5.mp3 alias BGM1", 0, 0, 0);
		break;
	case 6:mciSendString("open ./res/beijingyinyue6.mp3 alias BGM1", 0, 0, 0);
		break;
	case 7:mciSendString("open ./res/beijingyinyue7.mp3 alias BGM1", 0, 0, 0);
		break;
	}
	mciSendString("play BGM1", 0, 0, 0);
	
}


//随机加载图片
void playPicture()
{
	int x;
	x = rand() % 8;
	//printf("图片%d\n",x);
	switch (x)
	{
	case 0:loadimage(&ph, "res\\0.png", 8*M, 4*M);
		break;
	case 1:loadimage(&ph, "res\\1.png", 8*M, 4*M);
		break;
	case 2:loadimage(&ph, "res\\2.png", 8*M, 4*M);
		break;
	case 3:loadimage(&ph, "res\\3.png", 8*M, 4*M);
		break;
	case 4:loadimage(&ph, "res\\4.png", 8*M, 4*M);
		break;
	case 5:loadimage(&ph, "res\\5.png", 8*M, 4*M);
		break;
	case 6:loadimage(&ph, "res\\6.png", 8*M, 4*M);
		break;
	case 7:loadimage(&ph, "res\\7.jpg", 8*M, 4*M);
		break;
	/*case 12:loadimage(&ph, "res\\12.jpeg", 8 * M, 4 * M);
		break;*/
	}
	loadimage(&blank, "res\\blank.png", M, M);
}


int main()
{
	srand((unsigned)time(NULL));
	initgraph(8*M, 4*M);//SHOWCONSOLE显示控制台
	//开场提示音
	mciSendString("open ./res/timi.mp3 alias BGM", 0, 0, 0);
	mciSendString("play BGM", 0, 0, 0);

	playPicture();
	putimage(0, 0, &ph);
	Sleep(3000);//延时3S
	playBgm();
	initMap();

	//循环调用随机打乱函数
	for (int i = 0; i<10000; i++)
	{
		machine();
	}
	drawMap();

	palyGame();
	while (1);//卡屏
	closegraph();//关闭图形环境
	return 0;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一份用C语言实现4x4数字华容道的代码,希望能够帮到您。 ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 4 #define COL 4 void init(int board[ROW][COL]) { int i, j, k, tmp; srand(time(NULL)); for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { board[i][j] = i * COL + j + 1; } } board[ROW-1][COL-1] = 0; //最后一个位置是空格 for (k = 0; k < 100; k++) { //随机打乱 i = rand() % ROW; j = rand() % COL; tmp = board[i][j]; board[i][j] = board[ROW-1][COL-1]; board[ROW-1][COL-1] = tmp; } } void show(int board[ROW][COL]) { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == 0) { printf(" "); } else { printf("%2d ", board[i][j]); } } printf("\n"); } } int check(int board[ROW][COL]) { int i, j, num = 1; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] != num % (ROW*COL)) { return 0; } num++; } } return 1; } int move(int board[ROW][COL], char dir) { int i, j, x, y, tmp; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == 0) { x = i; y = j; } } } switch (dir) { case 'w': //上移 if (x == 0) { return 0; } tmp = board[x-1][y]; board[x-1][y] = board[x][y]; board[x][y] = tmp; break; case 's': //下移 if (x == ROW-1) { return 0; } tmp = board[x+1][y]; board[x+1][y] = board[x][y]; board[x][y] = tmp; break; case 'a': //左移 if (y == 0) { return 0; } tmp = board[x][y-1]; board[x][y-1] = board[x][y]; board[x][y] = tmp; break; case 'd': //右移 if (y == COL-1) { return 0; } tmp = board[x][y+1]; board[x][y+1] = board[x][y]; board[x][y] = tmp; break; default: return 0; } return 1; } int main() { int board[ROW][COL]; char dir; init(board); while (!check(board)) { show(board); printf("请输入移动方向(w上 s下 a左 d右):"); scanf("%c", &dir); if (move(board, dir)) { system("clear"); //清屏 } else { printf("移动无效!\n"); } getchar(); //吃掉缓冲区的回车符 } show(board); printf("恭喜您完成华容道!\n"); return 0; } ``` 这份代码主要实现了4x4数字华容道的初始化、打乱、展示、移动以及检查是否完成的功能。其中移动操作使用了简单的交换法,检查是否完成则是对每个位置上的数字进行比较。在主函数中,使用while循环不断展示当前状态并读取用户输入的移动方向,直到完成华容道为止。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值