//贴图步骤:
//1.图片路径:bmp是不改变 像素 的图片存储(位图,会比原图还大),jpg(压缩式),png(带透明度压缩),.avi .gif(动图)
//2.定义图片变量
//3.加载图片
//4.贴图
//5.等比例缩放
//6.透明贴图
//7.切图
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define TEST 1 //调试使用,需要调试则为1,true
#define WINDOW_WIDTH 800 //图形库宽
#define WINDOW_HIGHT 600 //图形库高
#define PEOPLE_WIDTH 60 //人物宽
#define PEOPLE_HEIGHT 108 //人物高
#define NUM 8 //图片数目
IMAGE bk, move0, move1, move2, move3;
int x = 100, y = 100; //人物坐标
enum direct{ p_up, p_dowm, p_left, p_right};//人物方向
int main()
{
#if TEST
initgraph(WINDOW_WIDTH, WINDOW_HIGHT);// 需要调试
#else
initgraph(WINDOW_WIDTH, WINDOW_HIGHT);//不需要
#endif
mciSendString("open 1.mp3",NULL, NULL, NULL);
mciSendString("play 1.mp3 repeat", NULL, NULL, NULL);
loadimage(&bk, "bk.bmp", WINDOW_WIDTH, WINDOW_HIGHT,true);
loadimage(&move0, "move0.bmp");
loadimage(&move1, "move1.bmp");
loadimage(&move2, "move2.bmp");
loadimage(&move3, "move3.bmp");
int n = 0;
int dir = p_dowm;//人物方向
int ch;
while (1)
{
BeginBatchDraw();
putimage(0, 0, &bk);//贴背景图
//贴图时贴图与背景贴图间为&运算黑色没有颜色0,0,0,不显示,任何数&0==0,彩色&黑色==黑色,任何数&1==原本数,彩色&白色==原色
//贴图的SRCAND 和 SRCPINAT之间为|运算
switch (dir)
{
case p_up:putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move0, n * PEOPLE_WIDTH, PEOPLE_HEIGHT, SRCAND);
putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move0, n * PEOPLE_WIDTH, 0, SRCPAINT);
break;
case p_dowm:putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move1, n * PEOPLE_WIDTH, PEOPLE_HEIGHT, SRCAND);
putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move1, n * PEOPLE_WIDTH, 0, SRCPAINT);
break;
case p_left:putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move2, n * PEOPLE_WIDTH, PEOPLE_HEIGHT, SRCAND);
putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move2, n * PEOPLE_WIDTH, 0, SRCPAINT);
break;
case p_right:putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move3, n * PEOPLE_WIDTH, PEOPLE_HEIGHT, SRCAND);
putimage(x, y, PEOPLE_WIDTH, PEOPLE_HEIGHT, &move3, n * PEOPLE_WIDTH, 0, SRCPAINT);
break;
default:
break;
}
EndBatchDraw();
n++;
if (n >= 7) n = 0;
//移动
if (_kbhit())//有按键输入为真,返回true
{
ch = _getch();//_getch阻塞函数,不输入则不执行
switch (ch){
case 'W':
case 'w':
dir = p_up; y -= 3; break;
case 'S':
case 's':
dir = p_dowm; y += 3; break;
case 'A':
case 'a':
dir = p_left; x-= 3; break;
case 'D':
case 'd':
dir = p_right;x += 3; break;
}
}
Sleep(100);
}
//项目-->属性——>配置属性-->c\c++-->代码生成-->运行库(MT),.exe和图片音乐等发送出去即可使用(图片音乐等需要放在和.exe同一个路径)
}
|