C++ 實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺(jué)的詳細(xì)代碼
參考 《C和C++游戲趣味編程》 童晶
“旋轉(zhuǎn)蛇”錯(cuò)覺(jué)
繪制錯(cuò)覺(jué)圖片,使靜止的圓盤(pán)看起來(lái)有在轉(zhuǎn)動(dòng)的錯(cuò)覺(jué)
繪制扇形
函數(shù)solidpie(left, top, right, bottom, stangle, endangle)可以繪制無(wú)邊框的填充扇形。其中(left, top)、(right, bottom)為扇形對(duì)應(yīng)圓的外切矩形的左上角、右下角坐標(biāo),stangle、endangle為扇形的起始角、終止角(單位為弧度)
#include <graphics.h> #include <conio.h> #include <stdio.h> int main() { float PI = 3.14159; initgraph(600, 600); int centerX = 300; int centerY = 300; int radius = 200; circle(centerX, centerY, radius); // 畫(huà)出對(duì)應(yīng)的圓邊框 int left = centerX - radius; // 圓外切矩形左上角x坐標(biāo) int top = centerY - radius; // 圓外切矩形左上角y坐標(biāo) int right = centerX + radius; // 圓外切矩形右下角x坐標(biāo) int bottom = centerY + radius; // 圓外切矩形右下角y坐標(biāo) solidpie(left, top, right, bottom, PI / 6, PI / 3); // 畫(huà)出填充扇形 _getch(); closegraph(); return 0; }
RGB顏色模型
EasyX可以設(shè)定繪圖顏色:
setbkcolor(WHITE); // 設(shè)置背景顏色為白色 setlinecolor(RED); // 設(shè)置線條顏色為紅色 setfillcolor(GREEN); // 設(shè)置填充顏色為綠色 cleardevice(); // 以背景顏色清空畫(huà)布
也可以采用數(shù)字形式:
setbkcolor(RGB(255, 255, 255)); // 設(shè)置背景顏色為白色 setlinecolor(RGB(255, 0, 0)); // 設(shè)置線條顏色為紅色 setfillcolor(RGB(0, 255, 0)); // 設(shè)置填充顏色為綠色
繪制一組扇形單元
人腦處理高對(duì)比度顏色(如黑和白)的時(shí)間,要比處理低對(duì)比度顏色(如紅與青)短很多。我們會(huì)先感知到黑白圖案,后感知到紅青圖案,這個(gè)時(shí)間差會(huì)讓圖片產(chǎn)生相對(duì)運(yùn)動(dòng)的效果,所以我們會(huì)有圖片的錯(cuò)覺(jué)
為了進(jìn)一步強(qiáng)化這種錯(cuò)覺(jué),我們讓每個(gè)黑、白扇形的角度為PI/60,紅、青扇形的角度為PI/30。一組青、白、紅、黑扇形角度和為PI/10,逆時(shí)針依次繪制20組單元
#include <graphics.h> #include <conio.h> #include <stdio.h> int main() { float PI = 3.14159; initgraph(600, 600); setbkcolor(RGB(128, 128, 128)); // 設(shè)置背景顏色為白色 cleardevice(); // 以背景顏色清空畫(huà)布 int centerX = 300; int centerY = 300; int radius = 200; int left = centerX - radius; // 圓外切矩形左上角x坐標(biāo) int top = centerY - radius; // 圓外切矩形左上角y坐標(biāo) int right = centerX + radius; // 圓外切矩形右下角x坐標(biāo) int bottom = centerY + radius; // 圓外切矩形右下角y坐標(biāo) int i; float offset; for (i = 0; i < 20; i++) { offset = i * PI / 10; setfillcolor(RGB(0, 240, 220)); // 青色 solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset); setfillcolor(RGB(255, 255, 255)); // 白色 solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset); setfillcolor(RGB(200, 0, 0)); // 紅色 solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset); setfillcolor(RGB(0, 0, 0)); // 黑色 solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset); } _getch(); closegraph(); return 0; }
循環(huán)嵌套
利用雙重for循環(huán)語(yǔ)句,可以繪制出多層圓盤(pán)。先繪制半徑大的,在繪制半徑小的覆蓋。不同半徑的扇形之間有PI/20的角度偏移量。另外,對(duì)圓心坐標(biāo)進(jìn)行循環(huán)遍歷就可以實(shí)現(xiàn)多個(gè)圓盤(pán)效果
#include <graphics.h> #include <conio.h> #include <stdio.h> int main() { float PI = 3.14159; initgraph(1200, 800); setbkcolor(RGB(128, 128, 128)); // 設(shè)置背景顏色為灰色 cleardevice(); int centerX, centerY; int radius; int i; float offset; float totalOffset = 0; // 不同半徑之間的角度偏移量 for (centerX = 200; centerX < 1200; centerX += 400) { for (centerY = 200; centerY < 800; centerY += 400) { for (radius = 200; radius > 0; radius -= 50) { int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; for (i = 0; i < 20; i++) { offset = i * PI / 10 + totalOffset; setfillcolor(RGB(0, 240, 220)); // 青色 solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset); setfillcolor(RGB(255, 255, 255)); // 白色 solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset); setfillcolor(RGB(200, 0, 0)); // 紅色 solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset); setfillcolor(RGB(0, 0, 0)); // 黑色 solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset); } totalOffset += PI / 20; // 不同半徑間角度偏移 } } } _getch(); closegraph(); return 0; }
HSV顏色模型
HSV是一種根據(jù)顏色的直觀特性創(chuàng)建的顏色模型。H是Hue的首字母,表示色調(diào),取值范圍為0360,刻畫(huà)不同色彩;S是Saturation的首字母,表示飽和度,取值范圍為01,表示混合了白色的比例,值越高顏色越鮮艷;V是Value的首字母,表示明度,取值范圍為0~1,等于0時(shí)為黑色,等于1時(shí)最明亮
#include <graphics.h> #include <conio.h> #include <stdio.h> int main() { float PI = 3.14159; initgraph(600, 600); setbkcolor(RGB(255, 255, 255)); cleardevice(); int centerX = 300; int centerY = 300; int radius = 200; int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; int i; int step = 10; COLORREF color; for (i = 0; i < 360; i += step) { color = HSVtoRGB(i, 1, 1); // HSV設(shè)置的顏色 setfillcolor(color); solidpie(left, top, right, bottom, i * PI / 180, (i + step) * PI / 180); } _getch(); return 0; }
按鍵切換效果
利用while循環(huán)和_getch()函數(shù),可以實(shí)現(xiàn)每次按鍵后,重新生成隨機(jī)顏色。另外,利用srand()函數(shù)對(duì)隨機(jī)函數(shù)初始化,避免每次運(yùn)行的隨機(jī)顏色都一樣
#include <graphics.h> #include <conio.h> #include <stdio.h> #include <time.h> int main() { float PI = 3.14159; initgraph(800, 600); setbkcolor(RGB(128, 128, 128)); // 設(shè)置背景顏色為灰色 cleardevice(); srand(time(0)); // 隨機(jī)種子函數(shù) int centerX, centerY; int radius; int i; float offset; float totalOffset; // 不同半徑之間的角度偏移量 while (1) { for (centerX = 100; centerX < 800; centerX += 200) { for (centerY = 100; centerY < 600; centerY += 200) { totalOffset = 0; // 同一半徑內(nèi)各組扇形之間的角度偏移量 float h = rand() % 180; // 隨機(jī)色調(diào) COLORREF color1 = HSVtoRGB(h, 0.9, 0.8); // 色調(diào)1生成的顏色1 COLORREF color2 = HSVtoRGB(h + 180, 0.9, 0.8); // 色調(diào)2生成的顏色2 for (radius = 100; radius > 0; radius -= 20) { int left = centerX - radius; int top = centerY - radius; int right = centerX + radius; int bottom = centerY + radius; for (i = 0; i < 20; i++) { offset = i * PI / 10 + totalOffset; setfillcolor(color1); // 青色 solidpie(left, top, right, bottom, offset, 2 * PI / 60 + offset); setfillcolor(RGB(255, 255, 255)); // 白色 solidpie(left, top, right, bottom, 2 * PI / 60 + offset, 3 * PI / 60 + offset); setfillcolor(color2); // 紅色 solidpie(left, top, right, bottom, 3 * PI / 60 + offset, 5 * PI / 60 + offset); setfillcolor(RGB(0, 0, 0)); // 黑色 solidpie(left, top, right, bottom, 5 * PI / 60 + offset, 6 * PI / 60 + offset); } totalOffset += PI / 20; // 不同半徑間角度偏移 } } } _getch(); } closegraph(); return 0; }
到此這篇關(guān)于C++ 實(shí)現(xiàn)旋轉(zhuǎn)蛇錯(cuò)覺(jué)的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)C++旋轉(zhuǎn)蛇內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的井字棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的井字棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04C++中字符串與整型及浮點(diǎn)型轉(zhuǎn)換全攻略
C++算法刷題等過(guò)程中經(jīng)常會(huì)遇到字符串與數(shù)字類(lèi)型的轉(zhuǎn)換,在這其中雖然樸素的算法有不少,但是對(duì)于double等類(lèi)型還是可以說(shuō)遇到一些麻煩,所以今天就來(lái)說(shuō)說(shuō)使用C++標(biāo)準(zhǔn)庫(kù)中的函數(shù)實(shí)現(xiàn)這些功能。感興趣的小伙伴一起參與閱讀吧2021-09-09C語(yǔ)言二叉排序樹(shù)的創(chuàng)建,插入和刪除
本文主要介紹了Java實(shí)現(xiàn)二叉排序樹(shù)的查找、插入、刪除、遍歷等內(nèi)容。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2021-10-10C++指針運(yùn)算符(&和*)的實(shí)現(xiàn)
C++ 提供了兩種指針運(yùn)算符,一種是取地址運(yùn)算符 &,一種是間接尋址運(yùn)算符 *,本文就詳細(xì)的介紹一下這兩種運(yùn)算符的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Qt圖片繪圖類(lèi)之QPixmap/QImage/QPicture詳解
這篇文章主要為大家詳細(xì)介紹了Qt圖片繪圖類(lèi)中QPixmap、QImage和QPicture的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03