1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define WIDTH 800//窗口的宽
#define HEIGHT 800//窗口的高
#define PI 3.14159//PI
typedef enum RADIES {//星球半径
sunradies = 150,//太阳
earthradies = 30,//地球
moonradies = 5,//月亮
}radies;
typedef enum COLOR {//颜色
black = 0,//黑色
blue = 0xAA0000,//蓝色
green = 0x00AA00,//绿色
cyan = 0xAAAA00,//青色
red = 0x0000AA,//红色
magenta = 0xAA00AA,//紫色
brown = 0x0055AA,//棕色
lightgray = 0xAAAAAA,//浅灰
yellow = 0x55FFFF,//黄色
}color;
typedef struct Plant {//行星
int radies;//行星半径
int center_x;//行星中心X坐标
int center_y;//行星中心Y坐标
}plant;
void drawsun(color c, int x, int y, radies r);//绘制太阳
void creatplant(plant* p, int x, int y, int radies);//创建星球
void drawplant(plant* p1, plant* p2, double angle, int a, color c, radies r);//绘制星球
int main()
{
plant s, e, m;
creatplant(&s, WIDTH / 2, HEIGHT / 2, sunradies);//创建太阳属性
creatplant(&e, WIDTH / 8, HEIGHT / 8, earthradies);//创建地球属性
creatplant(&m, WIDTH / 10, HEIGHT / 10, moonradies);//创建月球属性
color c1 = red;
initgraph(WIDTH, HEIGHT);
double eangle = 0;
double mangle = 0;
char key = 0;
int i = 1;//作为闪烁颜色的开关
int m1 = 1;//作为暂停的开关
int n1 = 1;//作为逆时针转动的开关
while (key != 27) {
if (_kbhit()) {
switch (key = _getch()) {
case 49:c1 = blue; break;
case 50:c1 = yellow; break;
case 52:c1 = brown; break;
case 53:c1 = magenta; break;
case 54:c1 = red; break;
case 55:c1 = lightgray; break;
case 56:i = i * -1; break;
case 32:m1 = m1 * -1; break;
case 27:break;
case 72:n1 = n1 * -1; break;
}
drawsun(c1, s.center_x, s.center_y, sunradies);
}
drawsun(c1, s.center_x, s.center_y, sunradies);
if (i < 0) {
color c[8] = { black,blue,red,blue,yellow,cyan,magenta,blue };
srand((unsigned)time(NULL));
int n = rand() % 8;
Sleep(1);
drawsun(c[n], s.center_x, s.center_y, sunradies);
}
BeginBatchDraw();
drawplant(&e, &s, eangle, 3, blue, earthradies);
drawplant(&m, &e, mangle, 13, yellow, moonradies);
if (m1 > 0) {
if (n1 > 0) {
eangle = eangle + 2 * PI / 60;
mangle = mangle + 2 * PI / 30;
}
else {
eangle = eangle - 2 * PI / 60;
mangle = mangle - 2 * PI / 30;
}
}
Sleep(50);
FlushBatchDraw();
clearcircle(e.center_x, e.center_y, earthradies);
clearcircle(m.center_x, m.center_y, moonradies);
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}
void drawsun(color c, int x, int y, radies r) {
setfillcolor(c);
solidcircle(x, y, r);
}
void creatplant(plant* p, int x, int y, int radies) {
p->center_x = x;
p->center_y = y;
p->radies = radies;
}
void drawplant(plant* p1, plant* p2, double angle, int a, color c, radies r) {
p1->center_x = p2->center_x + (WIDTH / a) * sin(angle);
p1->center_y = p2->center_y - (WIDTH / a) * cos(angle);
setfillcolor(c);
solidcircle(p1->center_x, p1->center_y, p1->radies);
}
/*
地球要围绕着太阳转动
月球要围绕着地球转动
按下1-7的按键时,太阳会变色
按下8的时候太阳会依次的变色
当按下空格键的时候,太阳会停止转动
当按下上键的时候地球和月亮会逆向的转动,再按下上键的时候,地球和月亮会顺时针转动
*/
|