graphics.h是针对DOS下的一个C语言图形库 (c++也可以)
目前支持下载此头文件的常用的有两种:
1. EGE (Easy Graphics Engine)
2. EasyX Graphics Library
EGE, 全名Easy Graphics Engine, 是windows下的简易绘图库,是一个类似BGI(graphics.h)的面向C/C++语言新手的图形库
EasyX, 全名EasyX Graphics Library, 是针对 Visual C++ 的免费绘图库,支持 VC6.0 ~ VC2022,简单易用,学习成本极低,应用领域广泛。目前已有许多大学将 EasyX 应用在教学当中
我建议Visual Studio用EasyX, Dev-C++可以用EGE
解决的方法:去 EasyX官网 下载EasyX的exe程序,点击后直接对自己使用的VC版本进行安装就行了,很简单。.
Graphics.h常用用法:
1.initgraph(width, height)和closegraph()
initgraph可以创建一个宽度为width, 高度为height的图形界面窗口
运行结束后可以通过closegraph来关闭窗口
注, closegraph之前记得用getch()函数来接收用户输入, 防止程序刚绘画玩就关闭窗口了
2.line(x1,y1,x2,y2)
从x1,y1坐标画一条直线到x2,y2坐标
3.setcolor和RGB
setcolor()可以把line画出来的直线变成另外一种颜色
RGB可以返回setcolor的自定义参数
RGB(r,g,b)可以修改不同浓度的红, 绿, 蓝
EX:
setcolor(RGB(200,0,0))可以将line画出来的直线变成红色
4.circle(x,y,radius)
在x,y坐标点画一个半径为radius的圆
EX:
circle(200, 200, 100);
5.setfillcolor, bar(x1,y1,x2,y2)
bar函数可以画出一个矩形
范围为:x坐标从50-300,y坐标从100-200
setfillcolor()可以改变Bar的填充颜色值,参数也是RGB()函数的自定义颜色
6.fillellipse(x,y,xr,yr);
画一个在x, y坐标点的实心椭圆
x轴上的半径为xr
y轴上的半径为yr
注:setfillcolor也可以改变其填充颜色值
7.setfontbkcolor(), setfont(12, 0, "宋体")
setfontcolor可以修改创建的文字的背景颜色值
setfont(12,0,"宋体")的第一个参数是字体的高度
第二个参数是字体的宽度, 如果填0, 那就会使用默认比例值
第三个参数是设置文字的字体样式
8.outtextxy(x, y, 字符串);
这个函数可以在坐标x,y坐标点输出字符串
注:字符串也可以是中文
EX:
outtextxy(100, 150, "中文也是支持的");
小游戏运行效果图:
贪吃蛇游戏源代码:
// 贪吃蛇小游戏.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <graphics.h>
#include <Windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include "io.h"
#define S_LEN 400 //蛇的最大节数
#define S_SPEED 200 //蛇的速度
#define S_SIZE 20 //蛇的大小
//全局变量
int score = 0;
typedef enum Direction
{
up = 72,
down = 80,
left = 75,
right = 77
}; //蛇的方向
//坐标结构体
struct Coordinate
{
int x;
int y;
};
//蛇的结构体
struct Snake
{
Coordinate sCo[S_LEN]; //每一节蛇的坐标
int num; //蛇的节数
Direction direction; //蛇的方向
}snake;
//食物的结构体
struct Food
{
Coordinate fCo;
int flag; //食物是否被吃
}food;
//函数声明
void InitSnake(); //初始化蛇
void Snake_Food(); //食物坐标
void Snake_Food_Paint(); //画食物
void Snake_Paint(); //画蛇
void Snake_Move(); //蛇移动
void Snake_Change(); //方向性移动
void Snake_Eat(); //食物是否被吃
void Snake_Break(); //判断是否撞自己
void Game_Over(); //结束游戏
int main(int argc, char *argv[])
{
InitSnake();
while (true)
{
while (!_kbhit())
{
settextcolor(RED);
score = (snake.num - 2) * 100;
char s[5];
sprintf(s, "%d", score);
outtextxy(500, 60, s);
if (food.flag == 0)
{
Snake_Food();
}
Snake_Food_Paint();
Snake_Break();
Snake_Eat();
Snake_Move();
Sleep(S_SPEED);
}
Snake_Change();
}
return 0;
}
//初始化蛇
void InitSnake()
{
//初始化图形界面
initgraph(640, 480);
setbkcolor(RGB(168, 168, 168));
cleardevice();
settextcolor(RED);
settextstyle(32, 0, "黑体");
outtextxy(510, 20, "贪吃蛇");
settextcolor(BLACK);
settextstyle(16, 0, "宋体");
outtextxy(495, 100, "分数:");
outtextxy(500, 400, "By:逃逸的卡路里");
setcolor(RGB(46, 85, 6));
setfillcolor(RGB(46, 85, 6));
fillrectangle(0, 0, 480, 480);
setcolor(RGB(199, 177, 163));
setfillcolor(RGB(199, 177, 163));
fillrectangle(40, 40, 440, 440);
setcolor(RGB(102, 86, 0));
for (int i = 0; i < 5; i++)
{
rectangle(39 - i, 39 - i, 441 + i, 441 + i);
}
setcolor(RGB(168, 147, 130));
for (int i = 60; i < 440; i += 20)
{
line(i, 40, i, 440);
line(40, i, 440, i);
}
setorigin(40, 40);
setcolor(RGB(56, 89, 0));
setfillcolor(RGB(91, 193, 45));
fillcircle(10, 10, 9); //第一节蛇
setfillcolor(WHITE);
fillcircle(8, 5, 2);
fillcircle(8, 15, 2);
food.flag = 0; //食物被吃
//蛇坐标初始化
snake.sCo[0].x = 0 + S_SIZE;
snake.sCo[0].y = 0;
snake.sCo[1].x = 0;
snake.sCo[0].y = 0;
snake.num = 2;
snake.direction = right;
}
//食物坐标
void Snake_Food()
{
int i;
srand((unsigned)time(NULL));
while (true)
{
int num = 0;
food.fCo.x = rand() % (400 / S_SIZE)*S_SIZE;
food.fCo.y = rand() % (400 / S_SIZE)*S_SIZE;
for (i = 0; i < snake.num; i++)
{
if (snake.sCo[i].x == food.fCo.x&&snake.sCo[i].y == food.fCo.y)
{
num++;
}
}
if (num == 0)
{
break;
}
}
food.flag = 1;
}
//画食物
void Snake_Food_Paint()
{
setcolor(RED);
setfillcolor(RED);
fillcircle(food.fCo.x + 10, food.fCo.y + 10, 8);
}
//画蛇
void Snake_Paint()
{
for (int i = snake.num - 1; i >= 0; i--)
{
setcolor(RGB(56, 89, 0));
setfillcolor(RGB(91, 193, 45));
fillcircle(snake.sCo[i].x + 10, snake.sCo[i].y + 10, 9);
if (i == 0)
{
setfillcolor(WHITE);
switch (snake.direction)
{
case up:
fillcircle(snake.sCo[i].x + 5, snake.sCo[i].y + 12, 2);
fillcircle(snake.sCo[i].x + 15, snake.sCo[i].y + 12, 2);
break;
case down:
fillcircle(snake.sCo[i].x + 5, snake.sCo[i].y + 8, 2);
fillcircle(snake.sCo[i].x + 15, snake.sCo[i].y + 8, 2);
break;
case left:
fillcircle(snake.sCo[i].x + 12, snake.sCo[i].y + 5, 2);
fillcircle(snake.sCo[i].x + 12, snake.sCo[i].y + 15, 2);
break;
case right:
fillcircle(snake.sCo[i].x + 8, snake.sCo[i].y + 5, 2);
fillcircle(snake.sCo[i].x + 8, snake.sCo[i].y + 15, 2);
break;
}
}
}
setcolor(RGB(199, 177, 163));
setfillcolor(RGB(199, 177, 163));
fillcircle(snake.sCo[snake.num].x + 10, snake.sCo[snake.num].y + 10, 9);
}
//蛇移动
void Snake_Move()
{
for (int i = snake.num; i > 0; i--)
{
snake.sCo[i].x = snake.sCo[i - 1].x;
snake.sCo[i].y = snake.sCo[i - 1].y;
}
switch (snake.direction)
{
case up:
if (snake.sCo[0].y == 0)
{
snake.sCo[0].y = 400 - S_SIZE;
}
else
{
snake.sCo[0].y -= S_SIZE;
}
break;
case down:
if (snake.sCo[0].y == 400 - S_SIZE)
{
snake.sCo[0].y = 0;
}
else
{
snake.sCo[0].y += S_SIZE;
}
break;
case left:
if (snake.sCo[0].x == 0)
{
snake.sCo[0].x = 400 - S_SIZE;
}
else
{
snake.sCo[0].x -= S_SIZE;
}
break;
case right:
if (snake.sCo[0].x == 400 - S_SIZE)
{
snake.sCo[0].x = 0;
}
else
{
snake.sCo[0].x += S_SIZE;
}
break;
}
Snake_Paint();
}
//方向性移动
void Snake_Change()
{
int ch;
ch = getch();
switch (ch)
{
case 'w':
case 'W':
case up:
if (snake.direction != down)
{
snake.direction = up;
}
break;
case 's':
case 'S':
case down:
if (snake.direction != up)
{
snake.direction = down;
}
break;
case 'a':
case 'A':
case left:
if (snake.direction != right)
{
snake.direction = left;
}
break;
case 'd':
case 'D':
case right:
if (snake.direction != left)
{
snake.direction = right;
}
break;
}
}
//食物是否被吃
void Snake_Eat()
{
if (snake.sCo[0].x == food.fCo.x&&snake.sCo[0].y == food.fCo.y)
{
snake.num++;
food.flag = 0;
}
}
//判断是否撞自己
void Snake_Break()
{
for (int i = snake.num - 1; i > 3; i--)
{
if (snake.sCo[0].x == snake.sCo[i].x&&snake.sCo[0].y == snake.sCo[i].y)
{
Game_Over();
}
}
}
//结束游戏
void Game_Over()
{
cleardevice();
setcolor(WHITE);
outtextxy(200, 160, "GAME OVER!!!");
Sleep(10000);
closegraph();
}
最后
希望对看到的小伙伴有帮助,有需要改进的地方,欢迎一起讨论学习哦。