C语言程序15:图形化基础篇
Merry Christmas
图形化编程基础
导读
//一、创建图形窗口
/*
1.如何创建
1.1 注意的点问题: 所有C语言中基本输入和输出在图形窗口中都不可以使用!
//它不是cmd窗口,而是自己创建的窗口。
1.2 做按键处理:使用的是_getch(); 加下划线是因为vs的内扩增
1.3 创建窗口:
init:初始化 graph:图形
initgraph(int width,int height); //窗口的宽:w,高:h
close:关闭
closegraph(); //关闭窗口
2.窗口坐标 几何问题
横向 x
纵向 y
原点在左上角
*/
#include <graphics.h> //1.第一步先包含头文件
#include <conio.h> //getch();
int main()
{
initgraph(400, 400);
_getch(); //防止闪屏。 头文件 :conio.h ;不能用system函数
closegraph();
return 0;
}
//二、文字的基本输出
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
/*
1.outtextxy:输出字符串和字符,但是不能直接输出整数或者小数
//C语言中:如何把整数转换为字符串?
2.窗口格式上
2.1 颜色
2.1.1 颜色表示方式
颜色宏: 颜色英文单词大写 RED: 红色
RGB配置颜色:RGB(0,255,0);
2.1.2 颜色设置 set:设置 color:颜色
2.1.3 设置颜色函数
setbkcolor: 设置背景(background)颜色 刷新显示cleardevice()
settextcolor: 设置文字颜色
setlinecolor: 设置线的颜色 line:线
setfillcolor: 设置填充颜色 fill:填充
2.2 文字格式
settextstyle(int w,int h,char *style): style:表示调整字体 style:样式
No.1 w: 宽 h:高 0:自适应 如果长和宽都是0,代表用默认的大小
No.2 字体在的电脑上位置: C盘//windows//fonts ,用的字体是属性里面的名字
setlinestyle:
*/
//自己弄一个函数将数字以字符串的形式打印出来
void outtextxy_int(int x, int y, int num)
{
char str[20] = "";
sprintf(str, "分数:%d", num); //字符串格式化打印
outtextxy(x, y, str);
}
int main()
{
initgraph(800, 600);
setbkcolor(LIGHTGREEN);
cleardevice();//设置窗口颜色后,刷新后才有效果
//out:输出 text:文字 xy:坐标
settextcolor(RGB(255, 0, 0));
settextstyle(35, 0, "字魂71号-御守锦书");
outtextxy(100, 100, "我自横刀向天笑!");
outtextxy_int(200, 200, 300);
_getch();
closegraph();
return 0;
}
//三、文字的特性输出
#include <graphics.h>
#include <conio.h>
#include <time.h>
/*
1.颜色闪烁,-刀999级?
2.竖立的文字输出?
3.文字动画如何制作?
*/
int main()
{
srand((unsigned int)time(NULL));
char str[] = { "-刀999级,是兄嘚就来砍我!" };
initgraph(800,600);
int textSize = 25; //表示开始的时候文字大小是25,随着++文字的大小跟着改变。
while (1)
{
settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));//随机产生颜色
settextstyle(textSize++, 0, "字魂71号-御守锦书");
outtextxy(100, 100, str);
Sleep(200); //休眠 /延迟 :200毫秒
//按键退出
//判断按键是否存在:kbhit(): 存在按键返回非零值
if (_kbhit())
{
char userKey = _getch();
if (userKey == '\r') //\n 不用\n换行,它是控制台的换行方式。窗口用\r
break;
}
}
cleardevice(); //将上面的东西都清除掉!
//二维数组经常用来操作多个字符串:一行存放一个字符串
//汉字: 一个汉字两个字节存储加上‘\0’ 所以列数是3
char strText[8][3] = { "欲", "练", "神", "功", "必", "先", "自", "宫" }; //一个汉字两个字节,后面还包含一个\0,所以需要将它设为3列,打印出来的才是两列字。
//str[0]--欲
int x = 300, y = 100;
settextstyle(35, 0, "字魂71号-御守锦书");
int count = 0;
while (1)
{
if (count<4) //控制行(第一列)
{
y = 100 + count * 50;
}
else //第二行
{
y = 100 + (count - 4) * 50;//从第一行开始
}
if (count == 4)
{
x -= 150;
}
outtextxy(x, y, strText[count]);
count++;
//重制效果
if (count == 9)
{
count = 0;
x = 300;
y = 100;
cleardevice();//清除
}
Sleep(1000); //毫秒: 1000ms= 1s
}
_getch(); //防止闪屏
closegraph(); //关闭窗口
return 0;
}
//四、基本绘图函数
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
/*
基本绘图函数:
画点:putpixel (int x,int y,COLORREF color);
画线:line(int x,int y,int xx,int yy);//从一个地方画到另一个地方(从x,y位置画到xx,yy位置。)
setlinestyle(style,size) //默认样式:PS_SOLID :SOLID实线 ;DASH:虚线
画圆
circle(int x,int y,int R);
画矩形
rectangle(int x,int y,int xx,int yy);//从左上角画到右下角
画椭圆
ellipse(int x,int y,int xx,int yy)
//填充系列: fill
1.1 设置填充颜色
setfillcolor(YELLOW);
1.2 填充:
solid:无边界线
fill:右边边界线
*/
int main()
{
srand((unsigned int)time(NULL));
initgraph(800, 600);
for (int i = 0; i < 10000; i++)
{
//put:输出
//pixel:点
putpixel(rand() % 800, rand() % 600, RGB(rand() % 256, rand() % 256, rand() % 256));//宽、高、颜色、点的个数
//Sleep(10); //让点与点之间有间隔,就会闪烁
//cleardevice( );//清空点,让它继续闪烁
}
setlinecolor(RED);
setlinestyle(PS_DASH,5);//DASH :虚线
line(0, 0, 800, 600);
rectangle(100, 100, 300, 300);//矩形
ellipse(100, 100, 300, 300);//椭圆
setfillcolor(YELLOW);
fillcircle(400, 400, 50); // 有边界线
solidcircle(200, 200, 50);//无边界线圆
setlinestyle(PS_SOLID, 2);
fillrectangle(400, 100, 500, 125);
_getch();
closegraph();
return 0;
}
//五、二维数组与基本绘图
//打砖块游戏
//#include<stdio.h>
//#include<stdlib.h>
//#include<graphics.h>
//#include<time.h>
//#include<conio.h>
//void initMap(int map[][5], int row, int cols)
//{
//for (int i = 0; i < row; i++)
//{
//for (int j = 0; j < cols; j++)
//{
//map[i][j] = rand() % 4;//0-1-2-3 随机产生4种砖块
//}
//}
//}
//
//void drawMap(int map[][5], int row, int cols)//画砖块
//{
//for (int i = 0; i < row; i++)
//{
//for (int j = 0; j < cols; j++)
//{
//int x = j * 100;
//int y = i * 25;
//switch (map[i][j])
//{
//case 0:
//setfillcolor(GREEN);
//fillrectangle(x, y, x + 100, y + 25);
//break;
//case 1:
//setfillcolor(YELLOW);
//fillrectangle(x, y, x + 100, y + 25);
//break;
//case 2:
//setfillcolor(BLUE);
//fillrectangle(x, y, x + 100, y + 25);
//break;
//case 3:
//setfillcolor(RGB(255, 188, 255));
//fillrectangle(x, y, x + 100, y + 25);
//break;
//case -1:
//break;
//
//}
//}
//}
//
//}
//void keyDown(int *boardx)
//{
//char userKey = _getch();
//switch (userKey)
//{
//case 'a':
//case 'A':
//case 75:
////边界处理
//*boardx -= 20;
//if (*boardx <= 0)
//{
//*boardx = 0;
//}
//break;
//case 'd':
//case 'D':
//case 77:
//*boardx += 20;
////边界处理
//if (*boardx + 200 >= 500)//判断木板是否到达边界
//{
//*boardx = 500 - 200;
//}
//break;
//}
//}
//
//int ballHitBoard(int ballx, int bally, int boardx, int boardy)//球与木板碰撞
//{
//if (bally == boardy - 15)
//{
//if (ballx >= boardx&&ballx <= boardx + 200)
//{
//return 1;
//}
//}
//return 0;
//}
//
//
//int ballHitBrick(int map[][5], int i,int j,int ballx,int bally)//画砖块
//{
//
////砖块的左上角坐标!
//int brickx = 250 - 50;
//int bricky = 100 - 25;
//
////判断球的行列
//j = ballx / 100;
//i = (bally - 15) / 25;
//if (j > brickx&&j<brickx + 100 && i>bricky&&i < bricky + 25)
//{
//if (i - 1 >= 0 && i - 1 <= 3)
//{
//if (j - 1 >= 0 && j - 1 <= 4)
//{
//
//map[i][j] = -1;
//}
//}
//}
//return 0;
//}
//int moveBall(int *ballx, int *bally, int *dx, int *dy, int boardx, int boardy)//移动球
//{
////没有考虑的木板情况的碰撞
////边界
//if (*bally <= 15 || ballHitBoard(*ballx, *bally, boardx, boardy))
//{
//*dy = -*dy;
//}
//if (*ballx <= 15 || *ballx >= 500 - 15)
//{
//*dx = -*dx;
//}
////板子的碰撞??
//if (*bally >= boardy)
//{
//return 1;
//}
//
////移动
//*ballx += *dx;
//*bally += *dy;
//
///*int j = *ballx / 100;
//int i = (*bally - 15) / 25;
//int map[][5] = { 0 };
//if (i - 1 >= 0 && i - 1 <= 3)
//{
//if (j - 1 >= 0 && j - 1 <= 4)
//{
//
//map[i][j] = -1;
//drawMap(map, i, j);
//
//}
//}
//*/
//
//return 0;
//}
//int main()
//{
//srand((unsigned int)time(NULL));
//int map[4][5] = { 0 };
////木板的左上角坐标 ,将木板放在中间
//int boardx = 250 - 100;//木板长度200
//int boardy = 700 - 25;//木板高25
//
////球的坐标
//int ballx = 250;
//int bally = 350;
//
////让球10像素移动
//int dx = 10;
//int dy = -10;
//
//initMap(map, 4, 5);
//HWND hwnd = initgraph(100 * 5, 700 ); //SHOWCONSOLE 控制台
//setbkcolor(RGB(0,87,87));
//cleardevice();
//while (1)
//{
//BeginBatchDraw(); //防止闪屏
//cleardevice();
//drawMap(map, 4, 5);
//ballHitBrick(map, 4, 5, ballx, bally);
//if (moveBall(&ballx, &bally, &dx, &dy, boardx, boardy))
//{
////windows中知识 MB_OK: ok键 确定
//MessageBox(hwnd, "游戏结束", "GameOver", MB_OK);//弹出游戏结束窗口
//
//break;
//}
////画球
//setfillcolor(WHITE);
//solidcircle(ballx,bally,15);//球半径15
////画木板
//setfillcolor(RED);
//solidrectangle(boardx, boardy, boardx + 200,700);
//if (_kbhit())
//{
//keyDown(&boardx);
//}
//Sleep(100);
//EndBatchDraw();//防止闪屏
//
//}
//cleardevice();
//settextcolor(RGB(255, 0, 0));
//settextstyle(35, 0, "楷体");
//outtextxy(200, 300, "游戏结束!");
//
//_getch();
//closegraph();
//return 0;
//}
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
#include<conio.h>
void initMap(int map[][5], int row, int cols)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
map[i][j] = rand() % 4;//0-1-2-3 随机产生4种砖块
}
}
}
void drawMap(int map[][5], int row, int cols)//画砖块
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
int x = j * 100;
int y = i * 25;
switch (map[i][j])
{
case 0:
setfillcolor(GREEN);
fillrectangle(x, y, x + 100, y + 25);
break;
case 1:
setfillcolor(YELLOW);
fillrectangle(x, y, x + 100, y + 25);
break;
case 2:
setfillcolor(BLUE);
fillrectangle(x, y, x + 100, y + 25);
break;
case 3:
setfillcolor(RGB(255, 188, 255));
fillrectangle(x, y, x + 100, y + 25);
break;
case -1:
break;
}
}
}
}
void keyDown(int* boardx)
{
char userKey = _getch();
switch (userKey)
{
case 'a':
case 'A':
case 75:
//边界处理
*boardx -= 20;
if (*boardx <= 0)
{
*boardx = 0;
}
break;
case 'd':
case 'D':
case 77:
*boardx += 20;
//边界处理
if (*boardx + 200 >= 500)//判断木板是否到达边界
{
*boardx = 500 - 200;
}
break;
}
}
int ballHitBoard(int ballx, int bally, int boardx, int boardy)//球与木板碰撞
{
if (bally == boardy - 15)
{
if (ballx >= boardx && ballx <= boardx + 200)
{
return 1;
}
}
return 0;
}
int moveBall(int* ballx, int* bally, int* dx, int* dy, int boardx, int boardy)//移动球
{
//没有考虑的木板情况的碰撞
//边界
if (*bally <= 15 || ballHitBoard(*ballx, *bally, boardx, boardy))
{
*dy = -*dy;
}
if (*ballx <= 15 || *ballx >= 500 - 15)
{
*dx = -*dx;
}
//板子的碰撞??
if (*bally >= boardy)
{
return 1;
}
//移动
*ballx += *dx;
*bally += *dy;
return 0;
}
//球和木板碰撞 drawMap(map, 4, 5);
/*
int x = j * 100;
int y = i * 25;
*/
void ballHitbrick(int(*map)[5], int ballx, int bally,int *dx,int *dy)
{
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 5; k++)
{
if (map[i][k] != -1 && ballx > k * 100 && ballx<k * 100 + 100 && bally> i * 25 && bally < i * 25 + 25)
{
map[i][k] = -1;
*dx = -*dx;
*dy = -*dy;
if (bally <= 15||bally >= i * 25 - 15)
{
if (ballx <= 15||ballx >= k * 100 - 15)
{
*dx = -*dx;
}
}
}
}
}
}
int successEnd(int map[][5], int row, int cols)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
if (map[i][j])
{
return 0;
}
}
}
return 1;
}
int main()
{
srand((unsigned int)time(NULL));
int result = 0;
int map[4][5] = { 0 };
//木板的左上角坐标 ,将木板放在中间
int boardx = 250 - 100;//木板长度200
int boardy = 700 - 25;//木板高25
//球的坐标
int ballx = 250;
int bally = 350;
//让球10像素移动
int dx = 10;
int dy = -10;
initMap(map, 4, 5);
HWND hwnd = initgraph(100 * 5, 700); //SHOWCONSOLE 控制台
setbkcolor(RGB(0, 87, 87));
cleardevice();
while (1)
{
ballHitbrick(map, ballx, bally,&dx,&dy);
BeginBatchDraw(); //防止闪屏
cleardevice();
drawMap(map, 4, 5);
if (result)
{
MessageBox(hwnd, "Gameover", "GameOver", MB_OK);
break;
}
if (successEnd(map, 4, 5))
{
MessageBox(hwnd, "Success!", "Success", MB_OK);
break;
}
/*ballHitBrick(map, 4, 5, ballx, bally);*/
result = moveBall(&ballx, &bally, &dx, &dy, boardx, boardy);
//画球
setfillcolor(WHITE);
solidcircle(ballx, bally, 15);//球半径15
//画木板
setfillcolor(RED);
solidrectangle(boardx, boardy, boardx + 200, 700);
if (_kbhit())
{
keyDown(&boardx);
}
Sleep(20);
EndBatchDraw();//防止闪屏
}
cleardevice();
settextcolor(RGB(255, 0, 0));
settextstyle(35, 0, "楷体");
outtextxy(200, 300, "游戏结束!");
_getch();
closegraph();
return 0;
}
//六、基本绘图与小球碰撞
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int main()
{
initgraph(800, 600);
int x = 400, y = 300;
int dx =-10;
int dy = 10;
while (1)
{
cleardevice();
setfillcolor(RGB(255, 0, 255));
solidcircle(x, y, 25);
if (_kbhit())
{
if (_getch() == ' ')
{
while (_getch() != ' ');
}
}
//边界处理
if (y <= 25 || y >= 600 - 25)
{
dy = -dy;
}
if (x <= 25 || x >= 800 - 25)
{
dx = -dx;
}
//移动
x += dx;
y += dy;
Sleep(50);
}
//有兴趣的可以描述以下自由落地,考虑摩擦因素
closegraph();
return 0;
}
我知道你在看哟
新浪微博:@秀米XIUMI