• 2022/11/22 [easyx]关于字符和一些背景


    easyx的字符介绍: 

    下面给出例子:

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. //包含图形库头文件
    4. int main()
    5. {
    6. //创建一个窗口
    7. //(show console 显示控制台),640480为窗口大小*
    8. initgraph(640,480,SHOWCONSOLE);
    9. //showconsole可以换成NOCLOSE:程序不能关闭
    10. // NOMINIMIZE不能最小化
    11. // 都想要用按位或|
    12. //设置背景颜色(两个步骤)
    13. setbkcolor(YELLOW);//步骤1
    14. //其中setbkcolor还可以用qq的截图按c键来识别色号来复制进去
    15. cleardevice();//清屏//步骤2
    16. //画一个圆
    17. setlinestyle(PS_SOLID, 5);//设置边缘线条颜色,5是线条粗细
    18. setfillcolor(GREEN);//设置填充颜色
    19. circle(50,50,50);//数字为坐标(x,y,r)
    20. fillcircle(50, 150, 50);
    21. solidcircle(50, 250, 50);
    22. //设置字体颜色
    23. settextcolor(GREEN);
    24. //绘制文字
    25. outtextxy(50, 50,L"easyx绘制文字");
    26. setbkmode(TRANSPARENT);//背景透明
    27. //此原函数只能输出单个字符,若想输出字符串,有以下几种方式
    28. //1、在字符串前加上大写的L
    29. //2、用TEXT()把字符串包起来如:,TEXT("")
    30. //3、进项目-->属性-->配置属性-->高级-->字符集-->改为多字节字符集
    31. getchar();
    32. closegraph();//关闭窗口
    33. return 0;
    34. }

    结果如下:

    文字居中的设置如下:

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. int main()
    4. {
    5. initgraph(640, 480, SHOWCONSOLE);
    6. //先选定一个矩形
    7. //200,50;代表矩形左上角的坐标,500100;代表右下角的坐标
    8. fillrectangle(200, 50, 500, 100);
    9. settextcolor(GREEN);
    10. char arr[] = "我是居中的字符";
    11. //textwidth:求出字符串的长度
    12. //textheight:求出字符串的高度
    13. int width = 300 / 2 - textwidth(arr) / 2;
    14. int height = 50 / 2 - textheight(arr) / 2;
    15. outtextxy(width + 200, height + 50, arr);
    16. getchar();
    17. closegraph();
    18. return 0;
    19. }

    居中的原理如图:

    结果如下:

    easyx导入图片:

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. int main()
    4. {
    5. initgraph(640, 480, SHOWCONSOLE);
    6. //输出图片
    7. IMAGE img;//定义一个变量对象
    8. //加载图片
    9. //相对路径:./表示当前文件夹下,../当前文件夹的上一级目录
    10. //绝对路径:C:\Users\fangzelin\source\repos\easyx\easyx\oo.jpg
    11. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg",640,480);
    12. //后面的数字是图片的长度与宽度
    13. //注意\要用转义字符\\
    14. //若地址有空格要用/""
    15. putimage(0, 0, &img);
    16. getchar();
    17. closegraph;
    18. return 0;
    19. }

    结果如下:

    [旧版]通过鼠标来判定坐标的位置:

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. int main()
    4. {
    5. initgraph(640, 480, SHOWCONSOLE);
    6. //先设置背景颜色,在加图片,不然清屏会清掉
    7. setbkcolor(WHITE);//背景颜色
    8. cleardevice();
    9. //图片
    10. IMAGE img;
    11. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 200, 200);
    12. putimage(0, 0, &img);//图片输出位置
    13. while (1)//一直循环
    14. {
    15. if (MouseHit())
    16. {
    17. MOUSEMSG msg = GetMouseMsg();
    18. //消息分发
    19. switch (msg.uMsg)
    20. {
    21. case WM_LBUTTONDOWN://鼠标左键
    22. if (msg.x <200 && msg.y <200)
    23. {
    24. printf("感谢支持\n");
    25. }
    26. outtextxy(400, 400, "鼠标左键按下");//输入字符的位置
    27. printf("坐标(%d,%d)\n", msg.x, msg.y);
    28. break;
    29. case WM_RBUTTONDOWN://鼠标右键
    30. outtextxy(400, 400, "鼠标右键按下");//输入字符的位置
    31. printf("坐标(%d,%d)\n", msg.x, msg.y);
    32. break;
    33. }
    34. }
    35. }
    36. getchar();
    37. closegraph;
    38. return 0;
    39. }

    结果如下:

    [新版]鼠标指针

    1. #include<stdio.h>
    2. #include<easyx.h>//是easyx.h
    3. void button(int x, int y, int w, int h,const char* text)
    4. //x,y是初始坐标,w,h是宽度和高度
    5. {
    6. setbkmode(TRANSPARENT);//将文字周边的黑框变成与背景相同的色彩
    7. setfillcolor(BROWN);
    8. fillroundrect(x, y, x + w, y + h, 10, 10);
    9. //文字居中
    10. char text_[50] = "button";
    11. strcpy_s(text_, text);
    12. int tx = x + (w - textwidth(text)) / 2;
    13. int ty = y + (h - textheight(text)) / 2;
    14. outtextxy(tx, ty,text);
    15. }
    16. int main()
    17. {
    18. initgraph(640, 480, EX_SHOWCONSOLE);
    19. button(50, 50, 150, 50,"按钮");
    20. ExMessage msg;
    21. while (1)
    22. {
    23. if (peekmessage(&msg, EX_MOUSE));
    24. {
    25. switch (msg.message)
    26. {
    27. case WM_LBUTTONDOWN:
    28. if (msg.x >= 50 && msg.x <= 50 + 150 && msg.y >= 50 && msg.y <= 50 + 50)
    29. {
    30. printf("我被点击了");
    31. }
    32. break;
    33. }
    34. }
    35. }
    36. getchar();
    37. closegraph();
    38. return 0;
    39. }

    键盘操作与物体移动

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. #include<conio.h>//使用_getch()
    4. int main()
    5. {
    6. initgraph(640, 480, SHOWCONSOLE);
    7. int x=0, y = 0;
    8. while (1)
    9. {
    10. cleardevice();//清屏后不会有拖尾
    11. //双缓冲绘图,需要放在绘图代码前和后,这样图片就不会一直闪了
    12. BeginBatchDraw();//批量绘图
    13. IMAGE img;//将图片放在清屏后不会消失
    14. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
    15. putimage(0, 0, &img);
    16. setfillcolor(BROWN);
    17. fillcircle(x,y,20);
    18. EndBatchDraw();//绘图结束
    19. //若用FlushBatchDraw代替,就不用beginbatchdraw
    20. char key = _getch();//键盘移动
    21. switch (key)
    22. {
    23. case 72://上键
    24. case 'w':
    25. case 'W':
    26. y -= 5;//向上y的坐标减少
    27. printf("上键\n");
    28. break;
    29. case 80://下键
    30. case 's':
    31. case 'S':
    32. y += 5;
    33. printf("下键\n");
    34. break;
    35. case 75://左键
    36. case 'a':
    37. case 'A':
    38. x -= 5;
    39. printf("左键\n");
    40. break;
    41. case 77://右键
    42. case 'd':
    43. case 'D':
    44. x += 5;
    45. printf("右键\n");
    46. break;
    47. }
    48. printf("%d,%c",key,key);
    49. }
    50. getchar();
    51. closegraph();
    52. return 0;
    53. }

     这样移动无法做到斜着移动,使用getasynckeystate可以实现

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. #include<conio.h>//使用_getch()
    4. int main()
    5. {
    6. initgraph(640, 480, SHOWCONSOLE);
    7. int x = 0, y = 0;
    8. while (1)
    9. {
    10. cleardevice();//清屏后不会有拖尾
    11. BeginBatchDraw();
    12. IMAGE img;//将图片放在清屏后不会消失
    13. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
    14. putimage(0, 0, &img);
    15. setfillcolor(BROWN);
    16. fillcircle(x, y, 20);
    17. EndBatchDraw();
    18. if (GetAsyncKeyState(VK_UP))//上键
    19. y -= 5;
    20. if (GetAsyncKeyState(VK_DOWN))//下键
    21. y += 5;
    22. if (GetAsyncKeyState(VK_LEFT))//左键{
    23. x -= 5;
    24. if (GetAsyncKeyState(VK_RIGHT))//右键{
    25. x += 5;
    26. }
    27. getchar();
    28. closegraph();
    29. return 0;
    30. }

    播放音乐

    1. #include<stdio.h>
    2. #include<graphics.h>
    3. #include<mmsystem.h>//包含多媒体设备接口头文件
    4. #pragma comment(lib,"winmm.lib")//加载静态库
    5. void BGM()
    6. {
    7. //打开音乐,播放 alias取别名 repeat 重复播放
    8. mciSendString("open ./thedown.mp3 alias bgm", 0, 0, 0);
    9. mciSendString("play bgm repeat", 0, 0, 0);
    10. if (0)//关闭音乐
    11. {
    12. mciSendString("close bgm ", 0, 0, 0);
    13. }
    14. }
    15. int main()
    16. {
    17. initgraph(640, 480, SHOWCONSOLE);
    18. BGM();
    19. int x = 0, y = 0;
    20. while (1)
    21. {
    22. cleardevice();//清屏后不会有拖尾
    23. BeginBatchDraw();
    24. IMAGE img;//将图片放在清屏后不会消失
    25. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
    26. putimage(0, 0, &img);
    27. setfillcolor(BROWN);
    28. fillcircle(x, y, 20);
    29. EndBatchDraw();
    30. if (GetAsyncKeyState(VK_UP))//上键
    31. y -= 5;
    32. if (GetAsyncKeyState(VK_DOWN))//下键
    33. y += 5;
    34. if (GetAsyncKeyState(VK_LEFT))//左键{
    35. x -= 5;
    36. if (GetAsyncKeyState(VK_RIGHT))//右键{
    37. x += 5;
    38. }
    39. getchar();
    40. closegraph();
    41. return 0;
    42. }

    修改窗口标题,弹出对话框:

    1. #include<stdio.h>
    2. #include<graphics.h>//嵌入了windows.h
    3. #include<mmsystem.h>//包含多媒体设备接口头文件
    4. #pragma comment(lib,"winmm.lib")//加载静态库
    5. void BGM()
    6. {
    7. //打开音乐,播放 alias取别名 repeat 重复播放
    8. mciSendString("open ./thedown.mp3 alias bgm", 0, 0, 0);
    9. mciSendString("play bgm repeat", 0, 0, 0);
    10. if (0)//关闭音乐
    11. {
    12. mciSendString("close bgm ", 0, 0, 0);
    13. }
    14. }
    15. void change()
    16. {
    17. //获取窗口句柄 hnd是随便定义的变量
    18. HWND hnd = GetHWnd();
    19. //设置窗口标题
    20. SetWindowText(hnd, "游戏");
    21. //弹出窗口,提示用户操作
    22. //如果不想要黑框框,就不要int isok
    23. int isok=MessageBox(hnd, "充钱给你再玩一次", "提示", MB_OKCANCEL);
    24. if (isok == IDOK)
    25. {
    26. printf("恭喜!");
    27. }
    28. else if (IDCANCEL == isok)
    29. {
    30. printf("无语!");
    31. }
    32. }
    33. int main()
    34. {
    35. initgraph(640, 480, SHOWCONSOLE);
    36. //BGM();
    37. change();
    38. int x = 0, y = 0;
    39. while (1)
    40. {
    41. cleardevice();//清屏后不会有拖尾
    42. BeginBatchDraw();
    43. IMAGE img;//将图片放在清屏后不会消失
    44. loadimage(&img, "C:\\Users\\fangzelin\\source\\repos\\easyx\\easyx\\oo.jpg", 640, 480);
    45. putimage(0, 0, &img);
    46. setfillcolor(BROWN);
    47. fillcircle(x, y, 20);
    48. EndBatchDraw();
    49. if (GetAsyncKeyState(VK_UP))//上键
    50. y -= 5;
    51. if (GetAsyncKeyState(VK_DOWN))//下键
    52. y += 5;
    53. if (GetAsyncKeyState(VK_LEFT))//左键{
    54. x -= 5;
    55. if (GetAsyncKeyState(VK_RIGHT))//右键{
    56. x += 5;
    57. }
    58. getchar();
    59. closegraph();
    60. return 0;
    61. }

     

  • 相关阅读:
    潮玩游戏潮玩宇宙大逃杀游戏
    GitHub-使用 Git工具 创建密钥id_rsa.pub
    【正点原子STM32连载】 第四十四章 触摸屏实验 摘自【正点原子】APM32F407最小系统板使用指南
    typescript:命名空间
    php static延迟静态绑定
    动态规划-矩阵连乘
    Linux部署Docker
    大专专科毕业设计前端网站源码]基于html的美食网站(js)(静态网页)
    Vue2、Vue3知识总结---完整版✨
    认识华为OSN1500光接口板
  • 原文地址:https://blog.csdn.net/fangzelin5/article/details/127989112