网站建设 公司 天津,学校网站建设运行简介,做店铺装修的公司网站,大连网络开发公司大家好#xff0c;这是我学完C后#xff0c;完整的编写的一个程序之一#xff0c;有兴趣的可尝试编写#xff0c;画面#xff08;动态的#xff09;还可以。 本程序总结有两个版本#xff0c;分别是对C中的继承、多态等一些方面的练习。
编写用的是VS2019#xff0c;e…大家好这是我学完C后完整的编写的一个程序之一有兴趣的可尝试编写画面动态的还可以。 本程序总结有两个版本分别是对C中的继承、多态等一些方面的练习。
编写用的是VS2019easyx。
首先来写第一个版本也是最基础的版本后续的都是在这个上进行一些细节的优化。有更好想法的小伙伴欢迎来一起讨论。
#includeiostream
#includegraphics.h
#includetime.h
#includecoino.h
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 840
using namespace std;
//创建一个星星类管理数据
class star
{public:star(){}void Init();void move();~star(){}
private:double m_x0;int m_y;int m_color;double m_step;
};
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() //星星的移动
{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;}
有编译环境的可以上机跑一下代码不是很难。 接下来这个版本是升级版对一些代码进行了优化从上述代码中不难看出对星星的“画”、“檫”、“新的位置”这三个用得较多不如将这三个单独用个函数写出后面直接调用即可。
代码优化如下
#includeiostream
#includegraphics.h
#includewindows.h
#includetime.h
#includeconio.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;}
是不是在运行后觉得一两种星的形状太枯燥了接下来简单的用多态来实现不同的星星的形状
代码如下
#includeiostream
#includegraphics.h
#includetime.h
#includeconio.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();}
以上就是本次本人对对绘制星图的就简单理解。欢迎有志于学好C的伙伴来分享C学习心得和友好地评论。如是有问题看到了有时间的话会及时回复。感谢各位的观看。