vlambda博客
学习文章列表

C语言程序16:图形化中级篇

C语言程序16:图形化中级篇

Merry Christmas

C语言程序16:图形化中级篇

这里也可以是一个标题


提示:图形化文件格式需要设为  .cpp  ;且设为多字节



一、显示图片

#include <graphics.h>

#include <stdio.h>

#include <conio.h>

/*

1.显示图片

2.图片尺寸问题

2.1 缩放加载

loadimage(&mm, "mm.jpg",int w,int h); //loadimage :加载图片


                  (1)loadimage:加载图片

                  (2)IMAGE      :1.起名字 ,即将图片存放在IMAGE(c++里面的类,相当于c语言的结构体)类型变量中

                  (3)putimage  :显示图片

*/

int main()

{

initgraph(800, 600);

IMAGE mm;//1.起名字 ,即将图片存放在IMAGE(c++里面的类,相当于c语言的结构体)类型变量中

//loadimage(&mm,"图片路径")  

//路径: 相对路径

//        绝对路径

loadimage(&mm, "mm.jpg",800,600);   //修改了项目属性不需要加L

//putimage(int x,int y,&mm)

putimage(0, 0, &mm);


_getch();

closegraph();

return 0;

}






二、鼠标操作

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

/*

1.存放在变量:鼠标所有的动作反馈给计算机就只有一个值

MOUSEMSG  m;

2.m.uMsg 代表当前鼠标消息

3. 鼠标的坐标:m.x,m.y

4.鼠标的类型

window:窗口  message:消息 mouse:鼠标 move:移动

WM_MOUSEMOVE:

//L:left  R:right

//BUTTON:按钮

//down:按下 up:弹起

WM_LBUTTONDOWN:

5.获取鼠标消息:GetMouseMsg();

*/

int main()

{

//神雕侠侣: 左右互搏---->左键画圆 右键画方

initgraph(800, 600);

while (1)

{

MOUSEMSG m;

m = GetMouseMsg();

switch (m.uMsg)

{

case WM_LBUTTONDOWN:

circle(m.x, m.y, 5);

break;

case WM_RBUTTONDOWN:

rectangle(m.x - 5, m.y - 5, m.x + 5, m.y + 5);

break;

}

}

closegraph();

//system("pause");

return 0;

}





三、音乐播放和鼠标点击案例

/*

简单的鼠标点击切换图片

1.音乐播放

1.1多媒体库 #include <mmsystem.h>

1.2加载静态库#pragma comment(lib,"winmm.lib")

mciSendString("指令集",0,0,0);

指令: open: 打开

  play:播放

  pause:暂停

  resume:继续

  close:关闭

  stop:停止

wait:等待的播放方式

repeat:重复方式

2.批量加载

2.1 C语言基本知识-->循环+sprintf函数

*/

#define _CRT_SECURE_NO_WARNINGS

#include <graphics.h>

#include <conio.h>

#include <stdio.h>

#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")

IMAGE img[3];//类型 数组名[数组长度]

void loadResource()

{

//加载资源

for (int i = 0; i < 3; i++)

{

char fileName[20] = "";

sprintf(fileName, "%d.jpg", i+1); //图片名字是从1.jpg开始

loadimage(img + i, fileName,300,500);

}

}

//尽量要把加载资源的过程和画的过程分开


int main()

{

initgraph(300, 500);

loadResource();

mciSendString("open 1.mp3", 0, 0, 0);

mciSendString("play 1.mp3", 0, 0, 0);

//自动切换版本

//int count = 0;

//while (1)

//{

//putimage(0, 0, img + count);

//count++;

//if (count == 3)

//count = 0;

//Sleep(1000);

//}

int count = 0;

while (1)

{

putimage(0, 0, img + count);

MOUSEMSG m = GetMouseMsg();

if (m.uMsg == WM_LBUTTONDOWN)

{

count++; 

if (count == 3)

count = 0;

}

else if (m.uMsg == WM_RBUTTONDOWN)

{

break;

}

}

closegraph();

return 0;

}






四、贪吃蛇游戏

#define _CRT_SECURE_NO_WARNINGS

#include <graphics.h>

#include <conio.h>

#include <stdio.h>

#include <time.h>

//枚举出方向

enum 方向{left,right,up,down};

//抽象坐标

struct Point

{

int x;

int y;

};

struct snake

{

struct Point xy[100];//最多100节

int num;//蛇的节数

char position;//移动方向

}bigSnake;

int score = 0;


//1.,初始化蛇--->初始化结构变量-->描述事物的最初状态

void initSnake()

{

bigSnake.num = 50;

bigSnake.position = right;

//[0] [1] [2]

//bigSnake.xy[2].x = 0;

//bigSnake.xy[2].y = 0;  //2

//bigSnake.xy[1].x = 10;

//bigSnake.xy[1].y = 0;

//bigSnake.xy[0].x = 20;

//bigSnake.xy[0].y = 0;

for (int i = bigSnake.num-1; i>=0; i--)

{

bigSnake.xy[i].x = 10*((bigSnake.num-1)-i);

bigSnake.xy[i].y = 0;

}


}

void drawSnake()

{

for (int i = 0; i < bigSnake.num; i++)

{

setlinecolor(BLACK);

setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));

fillrectangle(bigSnake.xy[i].x, bigSnake.xy[i].y,

bigSnake.xy[i].x + 10, bigSnake.xy[i].y + 10);

}

}

void moveSnake()

{

//后面的我们可以直接处理

for (int i = bigSnake.num - 1; i > 0; i--)

{

//结构体不允许直接用基本运算符操作

bigSnake.xy[i].x = bigSnake.xy[i-1].x;

bigSnake.xy[i].y = bigSnake.xy[i - 1].y;

}

//蛇头 xy[0] 根据方向处理

switch (bigSnake.position)

{

case left:

bigSnake.xy[0].x -= 10;

break;

case right:

bigSnake.xy[0].x += 10;

break;

case up:

bigSnake.xy[0].y -= 10;

break;

case down:

bigSnake.xy[0].y += 10;

break;

}

}

//按键操作

void keyDown()

{

char userKey = '\0';

userKey = _getch();

if (userKey == ' ')  //第一次按键空格

{

//暂停:下一次输入不是' ' 条件永远都成立

while (_getch() != ' ');  //满足条件循环 

}

//outextxy();

switch (userKey)

{

case 'w':

case 'W':

case 72:

if (bigSnake.position != down)

{

//往上走原来的方向不能往下

bigSnake.position = up;

}

break;

case 's':

case 'S':

case 80:

if (bigSnake.position != up)

{

bigSnake.position = down;

}

break;

case 'A':

case 'a':

case 75:

if (bigSnake.position != right)

{

bigSnake.position = left;

}

break;

case 'd':

case 'D':

case 77:

if (bigSnake.position != left)

{

bigSnake.position = right;

}

break;

}

}


//食物的属性

struct myFood

{

struct Point xy;//食物的坐标

int flag;//食物

}food;

//随机产生坐标

void initFood()

{

food.xy.x = rand() % 80 * 10;

food.xy.y = rand() % 60 * 10;

//食物不能出现在蛇的身上

int flag = 1;   //标记法

while (flag)

{

flag = 0;

for (int i = 0; i < bigSnake.num; i++)

{

if (bigSnake.xy[i].x == food.xy.x&&bigSnake.xy[i].y == food.xy.y)

{

flag = 1;

food.xy.x = rand() % 80 * 10;

food.xy.y = rand() % 60 * 10;

}

}

}

food.flag = 1;  //产生食物置为1

}

//画处理

void drawFood()

{

setlinecolor(BLACK);

setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));

fillrectangle(food.xy.x, food.xy.y, food.xy.x + 10, food.xy.y + 10);

}

void eatFood()

{

if (food.xy.x == bigSnake.xy[0].x&&food.xy.y == bigSnake.xy[0].y)

{

bigSnake.num++;

food.flag = 0;   //把标记置为0 为下一次食物生成准备

score += 10; //分数增加

}

}

//1.显示分数

void showScore(int x, int y, int score)

{

setbkmode(TRANSPARENT);

settextcolor(RED);

settextstyle(15, 0, "楷体");

char scoreStr[20] = "";

sprintf(scoreStr, "分数:%d", score);

outtextxy(x, y, scoreStr);

}

//2.碰壁处理--->边界处理

int hitBoard()

{

if (bigSnake.xy[0].x<0 || bigSnake.xy[0].y<0 || bigSnake.xy[0].x + 10>800 || bigSnake.xy[0].y + 10>600)

{

return 1;

}

return 0;

}

//3.撞自身

int hitSnake()

{

for (int i = 1; i < bigSnake.num; i++)

{

if (bigSnake.xy[0].x == bigSnake.xy[i].x&&bigSnake.xy[0].y == bigSnake.xy[i].y)

return 1;

}

return 0;

}

int main()

{

srand((unsigned int)time(NULL));

HWND hwnd=initgraph(800, 600); 

setbkcolor(WHITE);

cleardevice();

initSnake();

food.flag = 0;

while (1)

{

BeginBatchDraw();

cleardevice();

if (food.flag == 0)

{

initFood();

}

if (hitBoard())

{

MessageBox(hwnd, "hitBoard!", "hitBoard!", MB_OK);

break;

}

if (hitSnake())

{

MessageBox(hwnd, "hitSnake!", "hitSnake!", MB_OK);

break;

}

drawFood();

showScore(700,10,score);

eatFood();

moveSnake();

if (_kbhit()) //不要忘记这是一个函数   不要写成_kbhit;

{

keyDown();

}

drawSnake();

Sleep(50);

EndBatchDraw();

}

closegraph();

//system("pause");

return 0;

}





五、自绘按钮的封装

//自绘按钮制作

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

#include <string.h>

#include <conio.h>

#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")

//按钮

typedef struct Button

{

//按钮左上角

int x;

int y;

//按钮的右下角

int xx;

int yy;

COLORREF color;

char *testString;  //单纯的指向一个地方

}BTN,*LPBTN;


LPBTN createButton(int x, int y, int xx, int yy, COLORREF color, char *testString)

{

LPBTN button = (LPBTN)malloc(sizeof(BTN));

button->x = x;

button->y = y;

button->xx = xx;

button->yy = yy;

button->color = color;

//初始字符串指针:

button->testString = (char *)malloc(strlen(testString) + 1);

strcpy(button->testString, testString);

return button;

}

void drawButton(LPBTN pButton)

{

setlinecolor(BLACK);

setfillcolor(pButton->color);

fillrectangle(pButton->x, pButton->y, pButton->xx, pButton->yy);

setbkmode(TRANSPARENT);//去掉文字背景

settextcolor(BLACK);

settextstyle(30, 0, "楷体");

outtextxy(pButton->x + 15, pButton->y + 10, pButton->testString);

}

//判断鼠标是否在按钮中

int isInButton(LPBTN pButton, MOUSEMSG m)

{

if (m.x >= pButton->x&&m.x <= pButton->xx&&m.y >= pButton->y&&m.y <= pButton->yy)

{

return 1;

}

return 0;

}

int clickButton(LPBTN pButton, MOUSEMSG m)

{

if (isInButton(pButton, m))

{

if (m.uMsg == WM_LBUTTONDOWN)

{

return 1;

}

}

return 0;

}



int main()

{

IMAGE background;

loadimage(&background, "background.jpg", 800, 600);

initgraph(800, 600);

setbkcolor(WHITE);

cleardevice();

LPBTN play = createButton(300, 200, 450, 250, YELLOW, "播放音乐");

LPBTN pause = createButton(300, 260, 450, 310, YELLOW, "暂停音乐");

LPBTN resume = createButton(300, 320, 450, 370, YELLOW, "继续音乐");

LPBTN close = createButton(300, 380, 450, 430, YELLOW, "关闭音乐");

mciSendString("open 1.mp3", 0, 0, 0);

while (1)

{

BeginBatchDraw();

cleardevice();

MOUSEMSG m = GetMouseMsg();

putimage(0, 0, &background);

drawButton(play);

drawButton(pause);

drawButton(resume);

drawButton(close);

if (isInButton(play, m))

{

play->color = RED;

}

else

{

play->color = YELLOW;

}

if (isInButton(pause, m))

{

pause->color = RED;

}

else

{

pause->color = YELLOW;

}

if (isInButton(resume, m))

{

resume->color = RED;

}

else

{

resume->color = YELLOW;

}

if (isInButton(close, m))

{

close->color = RED;

}

else

{

close->color = YELLOW;

}

if (clickButton(play, m))//表示点击play按钮

{

mciSendString("play 1.mp3", 0, 0, 0);

}

if (clickButton(pause, m))

{

mciSendString("pause 1.mp3", 0, 0, 0);

}

if (clickButton(resume, m))

{

mciSendString("resume 1.mp3", 0, 0, 0);

}

if (clickButton(close, m))

{

mciSendString("close 1.mp3", 0, 0, 0);

}

EndBatchDraw();

}

_getch();

closegraph();

return 0;

}




我知道你在看





新浪微博:@秀米XIUMI