简单小游戏——“旋转蛇”错觉

这篇博客介绍了如何使用EasyX库和VS2013来实现旋转蛇错觉图形效果。通过绘制不同颜色和角度的扇形,结合人脑对颜色处理的差异,产生动态的视觉体验。代码示例展示了如何生成多个不同半径和位置的圆,形成连续的旋转蛇错觉图案。同时,第二个示例还引入了随机颜色,增加了图形的多样性。
摘要由CSDN通过智能技术生成

软件使用

EasyX、VS2013

效果图1

在这里插入图片描述

效果图2

在这里插入图片描述
在这里插入图片描述

具体实现

人脑处理高对比度颜色(比如黑和白)的时间要比处理低对比度颜色短很多,而这之间的时间差会让图片产生相对运动的效果,这就是旋转蛇错觉。
图上黑色和白色角度都是PI / 60,红色和青色都是PI / 30,而一组角(即黑+白+红+青)一共是PI / 10,所以一共要20组来组成一个圆。如图所示,一个成型的圆是由4个半径不同的圆组成的(先显示半径大的,再逐个显示半径小的),并且这些圆两两之间相差PI / 20。最后循环输出6个成型的圆即可

先看会用到的easyX库中的函数:

//该函数使用当前填充样式绘制无外框的填充扇形
void solidpie(
int left,//圆外切矩形左上角x坐标
int top,//圆外切矩形左上角y坐标
int right,//圆外切矩形右下角y坐标
int bottom,//圆外切矩形右下角y坐标
double stangle,//扇形的起始角的弧度
double endangle//扇形的终止角的弧度
);

//这个函数用于设置当前设备绘图背景色,学过html+css的可能会觉得熟悉
void setbkcolor(COLORREF color);
setbkcolor(RGB(128, 128, 128)); //背景设置为灰色

//这个函数用于设置当前设备填充颜色
void setfillcolor(COLORREF color);
setfillcolor(RGB(255, 255, 255)); //填充为白色

效果图1源代码

#include <conio.h>
#include <graphics.h>
#include <stdio.h>
int main()
{
	float PI = 3.14159;
	initgraph(1200, 800);
	setbkcolor(RGB(128, 128, 128)); //背景设置为灰色
	cleardevice(); //用当前背景色清空设备

	int coreX = 0, coreY = 0, radius = 0; //圆形坐标和半径
	int top_left_x = coreX - radius;
	int top_left_y = coreY - radius;
	int bot_right_x = coreX + radius;
	int bot_right_y = coreY + radius;
	float shift = 0 , begin = 0;
	/*
	因为要生成多个圆形,所以需要用嵌套for循环
	这里整个图表长1200,宽800
	所以每个原点横坐标分别是200、600、1000
	纵坐标分别是200和600
	*/
	for (coreX = 200; coreX < 1200; coreX += 400)
	{
		for (coreY = 200; coreY < 800; coreY += 400)
		{
			for (radius = 200; radius > 0; radius -= 50) //每个圆由4个圆组成
			{
				//每个圆位置不同,外切矩形的坐标也不同
				int top_left_x = coreX - radius;
				int top_left_y = coreY - radius;
				int bot_right_x = coreX + radius;
				int bot_right_y = coreY + radius;
				for (int i = 0; i <= 19; i++)
				{
					begin = i * PI / 10 + shift; //shift是偏移量
					setfillcolor(RGB(0, 240, 220));
					solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin, begin + PI / 30);
					setfillcolor(RGB(255, 255, 255));
					solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + PI / 30, begin + 3 * PI / 60);
					setfillcolor(RGB(200, 0, 0));
					solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 3 * PI / 60, begin + 5 * PI / 60);
					setfillcolor(RGB(0, 0, 0));
					solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 5 * PI / 60, begin + 6 * PI / 60);
				}
				shift += PI / 20;
			}
		}
	}
	_getch();
	closegraph();
	return 0;
}

效果图2源代码

#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <time.h>
int main()
{
	float PI = 3.14159;
	initgraph(800, 600);
	setbkcolor(RGB(128, 128, 128));
	cleardevice();
	srand(time(0));

	int coreX = 0, coreY = 0, radius = 0;
	int top_left_x = coreX - radius;
	int top_left_y = coreY - radius;
	int bot_right_x = coreX + radius;
	int bot_right_y = coreY + radius;
	float shift = 0, begin = 0;
	while (1)
	{
		for (coreX = 100; coreX < 800; coreX += 200)
		{
			for (coreY = 100; coreY < 600; coreY += 200)
			{
				float h = rand() % 180;
				COLORREF color1 = HSVtoRGB(h, 0.9, 0.8);
				COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8);
				for (radius = 100; radius > 0; radius -= 20)
				{
					int top_left_x = coreX - radius;
					int top_left_y = coreY - radius;
					int bot_right_x = coreX + radius;
					int bot_right_y = coreY + radius;
					for (int i = 0; i <= 19; i++)
					{
						begin = i * PI / 10 + shift;
						setfillcolor(color1);
						solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin, begin + PI / 30);
						setfillcolor(RGB(255, 255, 255));
						solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + PI / 30, begin + 3 * PI / 60);
						setfillcolor(color2);
						solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 3 * PI / 60, begin + 5 * PI / 60);
						setfillcolor(RGB(0, 0, 0));
						solidpie(top_left_x, top_left_y, bot_right_x, bot_right_y, begin + 5 * PI / 60, begin + 6 * PI / 60);
					}
					shift += PI / 20;
				}
			}
		}
		_getch();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值