我是靠谱客的博主 完美红牛,最近开发中收集的这篇文章主要介绍Easyx的使用一、基本介绍二、游戏实践,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

文件目录

  • 一、基本介绍
  • 二、游戏实践

一、基本介绍

介绍:easy-x可以帮助用户实现有色彩的画面,从而使得程序更加的完善
下载:通过在官网直接下载即可

1、引入图形库、建立窗口、关闭窗口

#include <graphics.h> //引入Easyx图形库
#include <conio.h>

int main()
{
	int y;
	initgraph(640, 480);//创建窗口,初始化画布
	for (y = 0; y < 480; y = y + 48)
		line(0, y, 640, y);
	_getch(); //按任意键继续
	closegraph(); //关闭绘图窗口
	return 0;
}

2、一些绘图函数介绍

line(0, 10, 30, 60);//画直线
circle(3, 4, 7);//画圆
putpixel(5,6,4);//画点
solidrectangle(5, 6, 9, 9);//画填充的矩形

setlinecolor(BLUE); //设置线条颜色
setfillcolor(RED);//设置填充颜色
setbkcolor(RED);//设置背景颜色
setcolor(RED);//设置前景颜色

二、游戏实践

1、小球碰撞
(1)用数组存储多个小球的速度个坐标从而实现小球和墙壁之间的碰撞

BeginBatchDraw():开始批量绘图,执行后任何绘图操作都暂时不输出到屏幕
FlushBatchDraw():执行未完成的绘制任务,执行批量绘制
EndBatchDraw():结束批量绘制,执行未完成的绘制任务
减少画面闪烁使用,分布在不同位置

setcolor(RED);  //设置边框
setfillcolor(BLUE);        // 填充为蓝色
fillcircle(200, 200, 100); // 画圆,圆心(200, 200),半径 100,fillcircle是右框的圆
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define High 480
#define Width 640  //游戏画面尺寸
#define BallNum 5  //小球的个数


int main()
{
	float ball_x[BallNum], ball_y[BallNum]; //小球坐标
	float ball_vx[BallNum], ball_vy[BallNum];//小球速度
	float radius;
	int i,j;
	radius = 20;

	for (i = 0; i < BallNum; i++)
	{
		ball_x[i] = (i + 2) * radius * 3;
		ball_y[i] = High / 2;
		ball_vx[i] = 1;
		ball_vy[i] = 1;
	}
	initgraph(Width, High);
	BeginBatchDraw();

	while (1)
	{
		//绘制黑线黑色填充的圆
		setcolor(BLACK);
		setfillcolor(BLACK);
		for (i = 0; i < BallNum; i++)
		{
			fillcircle(ball_x[i], ball_y[i], radius);
		}
			//更新小圆坐标
			for (i = 0; i < BallNum; i++)
			{
				ball_x[i] = ball_x[i] + ball_vx[i];
				ball_y[i] = ball_y[i] + ball_vy[i];

				
			}

			//判断是否发生碰撞
			for (i = 0; i< BallNum; i++)
			{
				if ((ball_x[i] <= radius) || (ball_x[i] >= Width - radius))
					ball_vx[i] = -ball_vx[i];
				if ((ball_y[i] <= radius) || ball_y[i] >= High - radius)
					ball_vy[i] = -ball_vy[i];

			}


		
			//绘制黄线、绿色填充的圆
			setcolor(YELLOW);
			setfillcolor(GREEN);
			for (i = 0; i < BallNum; i++)
			{
				fillcircle(ball_x[i], ball_y[i], radius);
			}
			FlushBatchDraw();


			//延时
			Sleep(3);
	
	}

	EndBatchDraw();
	closegraph();
	return 0;
}




(2)利用圆的中心坐标和两点间距离来实现球与球间发生碰撞和球与墙壁发生碰撞的结果

float minDistance2[BallNum][2]; 利用数纵坐标的不同的值来存储不同变量


#include <graphics.h>
#include <conio.h>
#include <math.h>
#define High 480
#define Width 640  //游戏画面尺寸
#define BallNum 5  //小球的个数


int main()
{
	float ball_x[BallNum], ball_y[BallNum]; //小球坐标
	float ball_vx[BallNum], ball_vy[BallNum];//小球速度
	float radius;
	int i, j;
	radius = 20;

	for (i = 0; i < BallNum; i++)
	{
		ball_x[i] = rand() % int(Width - 4 * radius) + 2 * radius;
		ball_y[i] = rand() % int(High - 4 * radius) + 2 * radius;
		ball_vx[i] = (rand() % 2) * 2 - 1;
		ball_vy[i] = (rand() % 2) * 2 - 1;
	}
	initgraph(Width, High);
	BeginBatchDraw();

	while (1)
	{
		//绘制黑线黑色填充的圆
		setcolor(BLACK);
		setfillcolor(BLACK);
		for (i = 0; i < BallNum; i++)
		{
			fillcircle(ball_x[i], ball_y[i], radius);
		}
		//更新小圆坐标
		for (i = 0; i < BallNum; i++)
		{
			ball_x[i] = ball_x[i] + ball_vx[i];
			ball_y[i] = ball_y[i] + ball_vy[i];

			//将超出边界的小球拉回来
			if (ball_x[i] < radius)
			{
				ball_x[i] = radius;
			}
			if (ball_y[i] < radius)
			{
				ball_y[i] = radius;
			}
			if (ball_x[i] > Width - radius)
				ball_x[i] = Width - radius;
			if (ball_y[i] > High - radius)
				ball_y[i] = High - radius;

		}

		//判断是否发生碰撞
		for (i = 0; i < BallNum; i++)
		{
			if ((ball_x[i] <= radius) || (ball_x[i] >= Width - radius))
				ball_vx[i] = -ball_vx[i];
			if ((ball_y[i] <= radius) || ball_y[i] >= High - radius)
				ball_vy[i] = -ball_vy[i];

		}

		float minDistance2[BallNum][2]; //记录某个小球和与它最近的小球以及它的下标


		for (i = 0; i < BallNum; i++)
		{
			minDistance2[i][0] = 9999999;//将纵坐标为0设置为距离
			minDistance2[i][1] = -1; //纵坐标为1为小球的下标
		}
		//将所有小球两两之间的距离的平方
		for (i = 0; i < BallNum; i++)
		{
			for (j = 0; j < BallNum; j++)
			{
				if (i != j)  //自己不需要和自己比
				{
					float dist2;
					dist2 = (ball_x[i] - ball_x[j] * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] * ball_y[j]));

					if (dist2 < minDistance2[i][0])
					{
						minDistance2[i][0] = dist2;
						minDistance2[i][1] = j;
					}

				}
			}
		}


		//判断球之间是否碰撞
		for (i = 0; i < BallNum; i++)
		{
			if (minDistance2[i][0] <= 4 * radius * radius)
			{
				j = minDistance2[i][1];
				int temp; //利用第三个变量进行速度交换
				temp = ball_vx[i]; ball_vy[i] = ball_vx[j]; ball_vx[j] = temp;
				temp = ball_vy[i]; ball_vy[i] = ball_vy[j]; ball_vy[j] = temp;

				minDistance2[j][0] = 999999999; //避免两次交换速度
				minDistance2[j][1] = -1;
			}

		}

		//绘制黄线、绿色填充的圆
		setcolor(YELLOW);
		setfillcolor(GREEN);
		for (i = 0; i < BallNum; i++)
		{
			fillcircle(ball_x[i], ball_y[i], radius);
		}
		FlushBatchDraw();


		//延时
		Sleep(3);
	}

	EndBatchDraw();
	closegraph();
	return 0;
}




2、画钟表
(1)画一个秒针:

setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
setcolor(WHITE); 线条颜色
line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针

#include <graphics.h>
#include <conio.h>
#include <math.h>

#define High 480
#define Width 640
#define PI 3.14159


int main()
{
	initgraph(Width, High); //初始化绘图窗口
	int center_x, center_y; //中点坐标
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength = Width / 5;  //秒针长度

	int secondEnd_x, secondEnd_y; //秒针的终点

	secondEnd_x = center_x + secondLength;
	secondEnd_y = center_y;

	setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
	setcolor(WHITE);
	line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针
	
	_getch();  //按任意建继续
	closegraph(); //关闭绘图窗口
	return 0;

}

(2)根据三角函数来确定秒针的终点坐标,用角度的变化来实现秒针的移动



#include <graphics.h>
#include <conio.h>
#include <math.h>

#define High 480
#define Width 640
#define PI 3.14159


int main()
{
	initgraph(Width, High); //初始化绘图窗口
	int center_x, center_y; //中点坐标
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength = Width / 5; //秒针长度


	int secondEnd_x, secondEnd_y; //秒针的终点
	
	float secondAngle = 0; //秒针对应的角度
	



	while (1)
	{


		//由角度决定的秒针端点坐标
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);

		setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针
		
		Sleep(10);

		setcolor(BLACK);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//隐藏前一帧的秒针
	

		//秒针角度的变化
		secondAngle = secondAngle * 2*PI/60;  //一圈一共2*PI,


	}


	_getch();  //按任意建继续
	closegraph(); //关闭绘图窗口
	return 0;


}

(3)获取当前时间实现秒针移动

系统变量类型SYSTEMTIME,用来定义时间对象(SYSTEMTIME ti),然后用GetLocalTime(&ti);获取当前时间
ti.wSecond可以获得每走一次的角度


#include <graphics.h>
#include <conio.h>
#include <math.h>

#define High 480
#define Width 640
#define PI 3.14159


int main()
{
	initgraph(Width, High); //初始化绘图窗口
	int center_x, center_y; //中点坐标
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength =  Width / 5; //秒针长度
	int secondEnd_x, secondEnd_y; //秒针的终点	
	float secondAngle; //秒针对应的角度

	SYSTEMTIME ti;   //定义变量保存当前时间

	while (1)
	{
		GetLocalTime(&ti);  //获取当前时间

		//秒针角度的变化
		secondAngle = ti.wSecond * 2 * PI / 60;
		//由角度决定的秒针终点坐标
		
		//由角度决定的秒针端点坐标
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);

		setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针

		Sleep(100);

		setcolor(BLACK);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//隐藏前一帧的秒针

		//秒针角度的变化
		//secondAngle = secondAngle * 2*PI/60;  //一圈一共2*PI,


	}

	_getch();  //按任意建继续
	closegraph(); //关闭绘图窗口
	return 0;


}

(4)使用相同办法添加分针和时针

分别为:1、针的中心 2、针的长度 3、针的终点 4、针的角度
通过每次针走一次将其变为黑色以隐藏针

#include <graphics.h>
#include <conio.h>
#include <math.h>

#define High 480
#define Width 640
#define PI 3.14159


int main()
{
	initgraph(Width, High); //初始化绘图窗口
	int center_x, center_y; //中点坐标
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength = Width / 7;  //秒针长度
	int minuteLength = Width / 6; //分针的长度
	int hourLength = Width / 5; //时针长度


	int secondEnd_x, secondEnd_y; //秒针的终点
	int minuteEnd_x, minuteEnd_y; //分针终点
	int hourEnd_x, hourEnd_y;  //时针终点

	float secondAngle; //秒针对应的角度
	float minuteAngle; //分针对应的角度
	float hourAngle; //时针对应的角度




	SYSTEMTIME ti;   //定义变量保存当前时间


	BeginBatchDraw();
	while (1)
	{

		

		GetLocalTime(&ti);  //获取当前时间

		//秒针角度的变化
		secondAngle = ti.wSecond * 2 * PI / 60;
		//由角度决定的秒针终点坐标

		//分针的变化
		minuteAngle = ti.wMinute * 2 * PI / 60;

		//时针角度的变化
		hourAngle = ti.wHour * 2 * PI / 12;

		//由角度决定的秒针端点坐标
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);


		//由角度决定的分针端点做坐标
		minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
		minuteEnd_y = center_y - minuteLength * cos(minuteAngle);

		//由角度决定的时针端点坐标
		hourEnd_x = center_x + hourLength * sin(hourAngle);
		hourEnd_y = center_y - hourLength * cos(hourAngle);



		setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针


		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y); //画分针

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(center_x, center_y, hourEnd_x, hourEnd_y);


		FlushBatchDraw();
		Sleep(10);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//隐藏前一帧的秒针
		setlinestyle(PS_SOLID, 5);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);//隐藏分针
		setlinestyle(PS_SOLID, 10);
		line(center_x, center_y, hourEnd_x, hourEnd_y); //隐藏时针



		//秒针角度的变化
		//secondAngle = secondAngle * 2*PI/60;  //一圈一共2*PI,


	}




	EndBatchDraw();
	_getch();  //按任意建继续
	closegraph(); //关闭绘图窗口
	return 0;


}

(5)添加表盘

bar(x - 5, y - 5, x + 5, y + 5); 无边框的矩形
outtextxy( int x,int y,输出字符);


#include <graphics.h>
#include <conio.h>
#include <math.h>

#define High 480
#define Width 640
#define PI 3.14159


int main()
{
	initgraph(Width, High); //初始化绘图窗口
	int center_x, center_y; //中点坐标
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength = Width / 7;  //秒针长度
	int minuteLength = Width / 6; //分针的长度
	int hourLength = Width / 5; //时针长度


	int secondEnd_x, secondEnd_y; //秒针的终点
	int minuteEnd_x, minuteEnd_y; //分针终点
	int hourEnd_x, hourEnd_y;  //时针终点

	float secondAngle; //秒针对应的角度
	float minuteAngle; //分针对应的角度
	float hourAngle; //时针对应的角度

	SYSTEMTIME ti;   //定义变量保存当前时间


	BeginBatchDraw();
	while (1)
	{

		//绘制一个简单的表盘
		setlinestyle(PS_SOLID, 1); //设置当前设备画线样式。
		setcolor(WHITE);
		circle(center_x, center_y, Width / 4); //画无填充的圆

		//画刻度
		int x, y, i;
		for (i = 0; i < 60; i++)
		{
			x = center_x + int(Width / 4.3 * sin(PI * 2 * i / 60));
			y = center_y + int(Width / 4.3 * cos(PI * 2 * i / 60));

			if (i % 15 == 0)
			{
				bar(x - 5, y - 5, x + 5, y + 5);
			}
			else if (i % 5 == 0)
			{
				circle(x, y, 3);
			}
			else
			{
				putpixel(x, y, WHITE);
			}
		}

		outtextxy(center_x - 25, center_y + Width / 6, "我的时钟");



		GetLocalTime(&ti);  //获取当前时间

		//秒针角度的变化
		secondAngle = ti.wSecond * 2 * PI / 60;
		//由角度决定的秒针终点坐标

		//分针的变化
		minuteAngle = ti.wMinute * 2 * PI / 60;

		//时针角度的变化
		hourAngle = ti.wHour * 2 * PI / 12;

		//由角度决定的秒针端点坐标
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);


		//由角度决定的分针端点做坐标
		minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
		minuteEnd_y = center_y - minuteLength * cos(minuteAngle);

		//由角度决定的时针端点坐标
		hourEnd_x = center_x + hourLength * sin(hourAngle);
		hourEnd_y = center_y - hourLength * cos(hourAngle);

		setlinestyle(PS_SOLID, 2);//画实线,宽度为两个像素
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//画秒针


		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y); //画分针

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(center_x, center_y, hourEnd_x, hourEnd_y);


		FlushBatchDraw();
		Sleep(10);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondEnd_x, secondEnd_y);//隐藏前一帧的秒针
		setlinestyle(PS_SOLID, 5);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);//隐藏分针
		setlinestyle(PS_SOLID, 10);
		line(center_x, center_y, hourEnd_x, hourEnd_y); //隐藏时针

		//秒针角度的变化
		//secondAngle = secondAngle * 2*PI/60;  //一圈一共2*PI,


	}
	EndBatchDraw();
	_getch();  //按任意建继续
	closegraph(); //关闭绘图窗口
	return 0;


}

最后

以上就是完美红牛为你收集整理的Easyx的使用一、基本介绍二、游戏实践的全部内容,希望文章能够帮你解决Easyx的使用一、基本介绍二、游戏实践所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部