C语言一网打尽:拼图切图,推箱子、坦克游戏界面,批量导入技术,资质准备
1.二位数组
2.贴图技术
3.sprintf函数
4.相关素材
视频讲解:
拼图切图
原图:
切割后:
源码:
#include
#include
#include
/*
1.定义图片变量 IMAGE ---int
2.loadimage加载资源 ---把图片放到变量里面
3.putimage 把图片粘贴窗口上
//切割技术:随机切割---拼图游戏----图片处理之类
*/
IMAGE mm;
int array2D[3][3] = { 0 };
int array1D[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int pos;
int length=9; //值没有初始化
void loadResource()
{
loadimage(&mm, "美女.jpg",900, 900);
//随机初始化
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//产生一个随机下标
pos = rand() % length;//值没有初始化
array2D[i][j] = array1D[pos];
//调整数组
for (int k = pos; k < length; k++)
{
array1D[k] = array1D[k + 1];
}
length--;
}
}
}
void userUI()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (array2D[i][j] < 4)
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 1) * 300, 0);
}
else if (array2D[i][j]>6)
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 7) * 300,600);
}
else
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 4) * 300,300);
}
}
}
}
int main()
{
//格式和路径
initgraph(900, 900);
srand((unsigned int)time(NULL));
loadResource();
userUI();
getchar();
closegraph();
return 0;
}
推箱子、坦克游戏界面
推箱子:
#include
#include
#include
/*
1.定义图片变量 IMAGE ---int
2.loadimage加载资源 ---把图片放到变量里面
3.putimage 把图片粘贴窗口上
//切割技术:随机切割---拼图游戏----图片处理之类
*/
IMAGE mm;
int array2D[3][3] = { 0 };
int array1D[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int pos;
int length=9; //值没有初始化
void loadResource()
{
loadimage(&mm, "美女.jpg",900, 900);
//随机初始化
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//产生一个随机下标
pos = rand() % length;//值没有初始化
array2D[i][j] = array1D[pos];
//调整数组
for (int k = pos; k < length; k++)
{
array1D[k] = array1D[k + 1];
}
length--;
}
}
}
void userUI()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (array2D[i][j] < 4)
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 1) * 300, 0);
}
else if (array2D[i][j]>6)
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 7) * 300,600);
}
else
{
putimage(j * 300, i * 300, 300, 300, &mm, (array2D[i][j] - 4) * 300,300);
}
}
}
}
int main()
{
//格式和路径
initgraph(900, 900);
srand((unsigned int)time(NULL));
loadResource();
userUI();
getchar();
closegraph();
return 0;
}
坦克大战游戏地图:
#define _CRT_SECURE_NO_WARNINGS //去掉内扩增问题---宏定义 scanf_s sprintf_s fscanf_s _getch();
#include
#include
const int rows = 12, cols = 13;
void instanceUI()
{
int mapIndex[rows][cols] =
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
2, 3, 2, 3, 2, 3, 1, 3, 2, 3, 2, 3, 2,
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2, 1,
2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2,
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 2,
2, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2,
2, 2, 2, 2, 2, 3, 0, 3, 2, 2, 2, 2, 2 };
//0 表示大鸟 1表示银块 2:黑色空地 3:砖头
//mapIndex[rows][cols] : 0-----3
//IMAGE map[4]; 0-----3
// map[0] map[1] map[2] map[3]
IMAGE map[4];
char filename[20] = "";
for (int i = 0; i < 4; i++)
{
sprintf(filename, "map%d.jpg", i);
loadimage(&map[i], filename);
}
int x, y;
for (int rowNum = 0; rowNum < rows; rowNum++)
{
for (int colNum = 0; colNum < cols; colNum++)
{
x = colNum * 50;
y = rowNum * 50;
//根据数组的值贴图
//图片编号和数组中的值得关系
putimage(x, y, &map[mapIndex[rowNum][colNum]]);
}
}
}
int main()
{
initgraph(cols * 50, rows * 50);
instanceUI();
getchar();
closegraph();
return 0;
}
批量导入技术
#define _CRT_SECURE_NO_WARNINGS //去掉内扩增问题---宏定义 scanf_s sprintf_s fscanf_s _getch();
#include
#include
#include
#pragma comment(lib,"winmm.lib")
IMAGE map[521];
void loadResource()
{
//学正确编程思想+走正确学习C/C++道路
//编写不一样的精彩
for (int i = 0; i <= 520; i++)
{
char filename[20] = ""; //定义一个字符数组
if (i < 10)
sprintf(filename, "Resource\000%d.bmp", i);
else if (i >= 10 && i < 100)
sprintf(filename, "Resource\00%d.bmp", i);
else if (i >= 100)
sprintf(filename, "Resource\0%d.bmp", i);
else
break;
loadimage(&map[i], filename, 650, 360);
}
}
int main()
{
initgraph(650, 360);
loadResource();
int i = 0;
mciSendString("open 1.mp3 alias music", 0, 0, 0);
mciSendString("play music repeat", 0, 0, 0);
while (1)
{
Sleep(50);
if (i == 521)
i = 0;
putimage(0, 0, &map[i]);
i++;
}
getchar();
return 0;
}
最后附加相关视频讲解: