用C++写一个星空
大家好,這是我學(xué)完C++后,完整的編寫(xiě)的一個(gè)程序之一,有興趣的可嘗試編寫(xiě),畫(huà)面(動(dòng)態(tài)的)還可以。
本程序總結(jié)有兩個(gè)版本,分別是對(duì)C++中的繼承、多態(tài)等一些方面的練習(xí)。
編寫(xiě)用的是VS2019,easyx。
首先,來(lái)寫(xiě)第一個(gè)版本,也是最基礎(chǔ)的版本,后續(xù)的都是在這個(gè)上進(jìn)行一些細(xì)節(jié)的優(yōu)化。有更好想法的小伙伴歡迎來(lái)一起討論。
#include<iostream> #include<graphics.h> #include<time.h> #include<coino.h> #define SCREEN_WIDTH 1024 #define SCREEN_HEIGHT 840 using namespace std; //創(chuàng)建一個(gè)星星類管理數(shù)據(jù) class star {public:star(){}void Init();void move();~star(){} private:double m_x=0;int m_y;int m_color;double m_step; }; void star::Init() //對(duì)星星的初始化 {if (m_x == 0){m_x = rand() % SCREEN_WIDTH;}else{m_x = 0;}m_y = rand() % SCREEN_HEIGHT;m_step = (rand() % 5000) / 1000.0 + 1;m_color = (int)(m_step * 255 / 6.0 + 0.5);m_color = RGB(m_color, m_color, m_color);} void star::move() //星星的移動(dòng) {putpixel((int)m_x, m_y, 0);m_x += m_step;if (m_x > SCREEN_WIDTH){this->Init(); \}putpixel((int)m_x, m_y, m_color); }int main() {srand((unsigned)time(NULL));initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);star star[MAXSTAR];for (int i = 0; i < MAXSTAR; i++){star[i].Init();}while (!_kbhit()){for (int i = 0; i < MAXSTAR; i++){star[i].move();}Sleep(30);}closegraph();return 0;}有編譯環(huán)境的可以上機(jī)跑一下,代碼不是很難。
?
接下來(lái)這個(gè)版本是升級(jí)版,對(duì)一些代碼進(jìn)行了優(yōu)化,從上述代碼中不難看出對(duì)星星的“畫(huà)”、“檫”、“新的位置”這三個(gè)用得較多,不如將這三個(gè)單獨(dú)用個(gè)函數(shù)寫(xiě)出,后面直接調(diào)用即可。
代碼優(yōu)化如下:
#include<iostream> #include<graphics.h> #include<windows.h> #include<time.h> #include<conio.h> #define SCREEN_WIDTH 1024 #define SCREEN_HEIGHT 840 #define MAXSTAR 400 using namespace std; class Star { public:Star() {}~Star() {}void Init();void Move();public:void Draw();void NewPos();void Remove();double m_x = 0;int m_y;double m_step;int m_color;}; class RectStar : public Star { public:RectStar() {}~RectStar() {}void Move(){Remove();NewPos();Draw();}protected:void Draw();void Remove(); }; void Star::Init() {if (m_x == 0){m_x = rand() % SCREEN_WIDTH;}else{m_x = 0;}m_y = rand() % SCREEN_HEIGHT;m_step = (rand() % 5000) / 1000.0 + 1;m_color = (int)(m_step * 255 / 6.0 + 0.5); // 速度越快,顏色越亮m_color = RGB(m_color, m_color, m_color);}void Star::Move() {Remove();NewPos();Draw();}/*void Star::Draw() {putpixel((int)m_x, m_y, m_color); }*/void Star::NewPos() {m_x += m_step;if (m_x > SCREEN_WIDTH)this->Init(); } void Star::Draw() {putpixel((int)m_x, m_y, m_color);setcolor(m_color);circle(m_x, m_y, 1); }void Star::Remove() {putpixel((int)m_x, m_y, 0);setcolor(0);circle(m_x, m_y, 1); } /*void Star::Remove() {putpixel((int)m_x, m_y, 0); }*/ int main() {srand((unsigned)time(NULL));initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);Star star[MAXSTAR];RectStar rstar[MAXSTAR];for (int i = 0; i < MAXSTAR; i++){star[i].Init();rstar[i].Init();}while (!_kbhit()){for (int i = 0; i < MAXSTAR; i++){star[i].Move();rstar[i].Move();}Sleep(50);}closegraph();return 0;}是不是在運(yùn)行后覺(jué)得一兩種星的形狀太枯燥了,接下來(lái)簡(jiǎn)單的用多態(tài)來(lái)實(shí)現(xiàn)不同的星星的形狀
代碼如下:
#include<iostream> #include<graphics.h> #include<time.h> #include<conio.h> #define SCREEN_WIDTH 1024 #define SCREEN_HEIGHT 840 #define MAXSTAR 400 using namespace std; class StarType { public:virtual void Draw(int x, int y, int color) = 0;virtual void Remove(int x, int y) = 0; }; class PointStar:public StarType {void Draw(int x, int y, int color){putpixel((int)x, y, color);setcolor(color);circle(x, y, 1);}void Remove(int x, int y){putpixel((int)x, y,0);setcolor(0);circle(x, y, 1);}}; class RecStar :public StarType {void Draw(int x, int y, int color){putpixel(x, y, color);setcolor(color);rectangle(x - 1,y - 1, x + 1, y + 1);}void Remove(int x, int y){putpixel(x, y, 0);setcolor(0);rectangle(x - 1, y - 1, x + 1, y + 1);} }; class XStar :public StarType {void Draw(int x, int y, int color){setcolor(color);outtextxy(x, y, _T("x"));}void Remove(int x, int y){settextcolor(0);outtextxy(x, y, _T("x"));} };class Star { public:Star(){}~Star() {}void Init();void Move();void Init(StarType* pStarType); public:void NewPos();double m_x;double m_y;int m_color;double m_step;StarType* m_pStarType; }; void Star::Init() {if (m_x == 0){m_x = rand() % SCREEN_WIDTH;}else{m_x = 0;}m_y = rand() % SCREEN_HEIGHT;m_step = (rand() % 5000) / 1000.0 + 1;m_color = (int)(m_step* 255 / 6.0 + 1);m_color = RGB(m_color, m_color, m_color);} void Star::Init(StarType* pStarType) {this->Init();m_pStarType = pStarType; } void Star::Move() {m_pStarType->Remove(m_x,m_y);NewPos();m_pStarType->Draw(m_x, m_y, m_color);} void Star::NewPos() {m_x += m_step;if (m_x > SCREEN_WIDTH){this->Init();}} void main() {srand((unsigned)time(NULL));initgraph(SCREEN_WIDTH, SCREEN_WIDTH);Star star[MAXSTAR];PointStar pointstar;XStar xstar;RecStar restar;for (int i = 0; i < MAXSTAR; i++){switch (i % 3){case 0:star[i].Init(&pointstar);break;case 1:star[i].Init(&pointstar);break;case 2:star[i].Init(&pointstar);break;default:break;}}while (!kbhit){for (int i = 0; i < MAXSTAR; i++){star[i].Move();}Sleep(50);}closegraph();}以上就是本次本人對(duì)對(duì)繪制星圖的就簡(jiǎn)單理解。歡迎有志于學(xué)好C++的伙伴來(lái)分享C++學(xué)習(xí)心得和友好地評(píng)論。如是有問(wèn)題看到了有時(shí)間的話會(huì)及時(shí)回復(fù)。感謝各位的觀看。
?
總結(jié)
- 上一篇: java多线程教程_java 基础教程之
- 下一篇: 超级计算机游戏电脑,Salad邀请PC玩