C語言一網(wǎng)打盡:拼圖切圖悴晰,推箱子慢睡、坦克游戲界面,批量導(dǎo)入技術(shù)铡溪,資質(zhì)準備
1.二位數(shù)組
2.貼圖技術(shù)
3.sprintf函數(shù)
4.相關(guān)素材
視頻講解:
拼圖切圖
原圖:
切割后:
源碼:
#include
#include
#include
/*
1.定義圖片變量 IMAGE ---int
2.loadimage加載資源 ---把圖片放到變量里面
3.putimage 把圖片粘貼窗口上
//切割技術(shù):隨機切割---拼圖游戲----圖片處理之類
*/
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++)
{
//產(chǎn)生一個隨機下標(biāo)
pos = rand() % length;//值沒有初始化
array2D[i][j] = array1D[pos];
//調(diào)整數(shù)組
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 把圖片粘貼窗口上
//切割技術(shù):隨機切割---拼圖游戲----圖片處理之類
*/
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++)
{
//產(chǎn)生一個隨機下標(biāo)
pos = rand() % length;//值沒有初始化
array2D[i][j] = array1D[pos];
//調(diào)整數(shù)組
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;
}
坦克大戰(zhàn)游戲地圖:
#define _CRT_SECURE_NO_WARNINGS //去掉內(nèi)擴增問題---宏定義 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;
//根據(jù)數(shù)組的值貼圖
//圖片編號和數(shù)組中的值得關(guān)系
putimage(x, y, &map[mapIndex[rowNum][colNum]]);
}
}
}
int main()
{
initgraph(cols * 50, rows * 50);
instanceUI();
getchar();
closegraph();
return 0;
}
批量導(dǎo)入技術(shù)
#define _CRT_SECURE_NO_WARNINGS //去掉內(nèi)擴增問題---宏定義 scanf_s sprintf_s fscanf_s _getch();
#include
#include
#include
#pragma comment(lib,"winmm.lib")
IMAGE map[521];
void loadResource()
{
//學(xué)正確編程思想+走正確學(xué)習(xí)C/C++道路
//編寫不一樣的精彩
for (int i = 0; i <= 520; i++)
{
char filename[20] = ""; //定義一個字符數(shù)組
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;
}
最后附加相關(guān)視頻講解: