#include<iostream>
#include<graphics.h>//图形库头文件
#include<mmsystem.h>//多媒体设备接口头文件
#include<conio.h>//控制台输入输出
#pragma comment(lib,"Winmm.lib")//加入音乐需要的头文件
using namespace std;
/*屏幕由像素点组成,坐标为数学坐标翻转*/
//要显示图片,需要把图片资源放到项目文件
//1.定义一个图片变量
IMAGE img,role1,role2;
int num = 0, x = 0, y = 450;
void Music()//插入音乐
{
//1.打开音乐,向多媒体接口发送一个字符串、alias 取别名
mciSendString("open D:\\vs 编译器\\图形练习\\res\\超级玛丽 - 超级玛丽.mp3 alias bk",0, 0,0);
//2.播放音乐
mciSendString("play bk",0, 0, 0);
//
Sleep(3000);
mciSendString("colse bk", 0, 0, 0);
}
void GameIn()//插入图片
{
//2.把图片保存到定义好的变量中去 load加载
loadimage(&img, "D:\\vs 编译器\\图形练习\\res\\background.jpg");
loadimage(&role1, "D:\\vs 编译器\\图形练习\\res\\ro1.jpg");//背景图
loadimage(&role2, "D:\\vs 编译器\\图形练习\\res\\ro2.jpg");//屏蔽图
putimage(0, 0, &img);//1,2位是绘制坐标的起始点
putimage(x, 450, 70, 124, &role2, 70 * num, 124 * 2, SRCAND);//角色初始位置
putimage(x, 450, 70, 124, &role1, 70 * num, 124 * 2, SRCPAINT);
}
void Circle()
{
//1.画圆
setfillcolor(GREEN);//将要绘制圆的颜色
fillcircle(320,100,10);//实心填充无边框
circle(320, 200, 10);//空心圆
solidcircle(320, 150, 10);//实心填充有边框
}
void SetLine()
{
//2.画线段
setlinecolor(RED);//绘制颜色
setlinestyle(PS_DASH,5);//设置线段样式
line(100, 100, 300, 100);//线段起点(10,20),终点(100,200);
line(230, 100, 230, 300);
line(200, 280, 230, 300);
line(120, 150, 200, 150);
line(120, 220, 200, 220);
line(120, 150, 120, 220);
line(200, 150, 200, 220);
}
void Rectangle()
{
//3.画矩形
setlinecolor(BLUE);
setlinestyle(PS_DASH, 5);
rectangle(10, 10, 100, 100);
}
void Text()
{
//4.输出带背景的常量文字
setbkmode(OPAQUE);//设置文字输出时的背景模式为QPAQE
//setbkmode(TRANSPARENT); //TRANSPARENT表示无背景
setbkcolor(BROWN);//字体背景颜色为棕色
settextstyle(25, 0, "楷体");//设置字体的高度,宽度,字体。宽度为0表示自适应
outtextxy(250, 250, "开始游戏");//在指定坐标输出文字
}
void ShuBiao()//接收鼠标
{
FlushMouseMsgBuffer();//清空鼠标消息缓冲区
while (true)
{
ExMessage msg ={0};//定义一个结构体鼠标变量
if (peekmessage(&msg, EM_MOUSE));//检测鼠标信息
{
switch (msg.message)//筛选鼠标信息类型
{
case WM_LBUTTONDOWN://左键信息
if (msg.x >=250 && msg.x <= 350 && msg.y >= 250 && msg.y <= 350) {
cout << "我是左键" << endl;
}
break;
default:
break;
}
}
}
Sleep(100);
}
void TuBiao()//生成图标
{
setbkmode(OPAQUE);
setbkcolor(GREEN);
settextstyle(100, 0, "Wingdings");//高度,宽度,字体
outtextxy(50, 100, 0xC0);//坐标,编码,字符映射表可查。左下角搜索栏
}
void Get_jianpan()
{
while (1)
{
char key;
key= _getch();//获取键盘消息
switch (key)
{
//case 119:
case 'w':
case 'W':
putimage(0, 0, &img);
putimage(x, y, 70, 124, &role2, 70 * num, 124 * 3, SRCAND);
putimage(x, y, 70, 124, &role1, 70 * num, 124 * 3, SRCPAINT);
num++;
if (num == 4)
num = 0;
y -= 7;
if (y<= 0)
{
y = 0;
}
break;
//case 115:
case 's':
case 'S':
putimage(0, 0, &img);
putimage(x, y, 70, 124, &role2, 70 * num, 0, SRCAND);
putimage(x, y, 70, 124, &role1, 70 * num, 0, SRCPAINT);
num++;
if (num == 4)
num = 0;
y += 7;
if (y >=760)
{
y = 0;
}
break;
//case 97:
case 'a':
case 'A':
putimage(0, 0, &img);
putimage(x, y, 70, 124, &role2, 70 * num, 124 , SRCAND);
putimage(x, y, 70, 124, &role1, 70 * num, 124 , SRCPAINT);
num++;
if (num == 4)
num = 0;
x-= 7;
if (x<= 0)
{
x = 0;
}
break;
//case 100:
case 'd':
case 'D':
putimage(0, 0, &img);
putimage(x, y, 70, 124, &role2, 70 * num, 124 * 2, SRCAND);
putimage(x, y, 70, 124, &role1, 70 * num, 124 * 2, SRCPAINT);
num++;
if (num == 4)
num = 0;
x+= 7;
if (x >= 1010)
{
x = 1010;
}
break;
default:
break;
}
}
}
void get_key()
{
int x = 200, y = 200;
while (1)
{
BeginBatchDraw();//双缓冲,批量绘图,放在绘图代码前
setfillcolor(BLUE);
cleardevice();//清屏,删除上一次遗留的画面
fillcircle(x, y, 20);
FlushBatchDraw();//
if (GetAsyncKeyState(VK_UP))//上键
{
y -= 5;
}
if (GetAsyncKeyState(VK_DOWN))//上键
{
y+= 5;
}
if (GetAsyncKeyState(VK_LEFT))//左键
{
x-= 5;
}
if (GetAsyncKeyState(VK_RIGHT))//右键
{
x += 5;
}
}
}
void chuangkou()
{
HWND hn = GetHWnd();//获取窗口句柄函数;
SetWindowText(hn, "游戏界面");//修改窗口名字函数
MessageBox(hn, "警告,3秒后自动关机", "提示",MB_ICONSTOP);//消息框弹出设置函数,内容,名字,按钮参数
//
}
int main()
{
//1,2个参数窗口的宽度和高度,第三个参数,是否显示控制台
initgraph(640,425);/*SHOWCONSOLE:打开窗口。NOCLOSE:没有关闭按钮。NOMINIMIZE:没有最小化按钮*/
get_key();
closegraph();
return 0;
}
easyx图形库学习总结
最新推荐文章于 2024-07-06 21:12:31 发布