En constante movimiento para conseguir la lista de estrellas EasyX biblioteca de gráficos
código:
# include
# include <graphics.h>
#include <Windows.h>
using namespace std;
// el ancho de la pantalla y la altura de una macro definición
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
// estrella macrodefinición cada propiedad
#define MAX_STAR 100 //: Número
. #Define a MAX_RADIUS 3
. MAX_STEP #define 5
// estrella moviéndose estado
enum {clase _STATUS
STOP,
la UP,
ABAJO,
IZQUIERDA,
rigth,
ALL_STATUS
};
// lista almacenada dentro de la estructura de estrella es una estructura de la dirección de la otra estrella
typedef struct {_LinkNode
estructura _LinkNode * PREV; // en una estrella de dirección de nodo
estructura _LinkNode * siguiente; // siguiente dirección de nodo en una estrella
} _LinkNode;
//星星结构体
typedef struct {
int x;
y int;
radio unsigned int;
_STATUS estado;
paso int;
de color int;
Nodo _LinkNode;
}_ESTRELLA;
// inicializar el primer nodo está protagonizada por
BOOL starInit (_STAR * y L_star) {
L_star = new nueva _STAR;
if (! L_star) {
retorno a false;
}
//不初始化星星属性
//这里是首节点
L_star->node.next = NULL;
L_star->node.prev = NULL;
return true;
}
// Inicialización estrellas en las propiedades
vacío initStar (* P & _STAR) {
SI (P!) {
tribunal << "ningún espacio de las estrellas!" << endl;
retorno;
}
p->x = rand() % SCREEN_WIDTH; //0 - 639
p->y = rand() % SCREEN_HEIGHT; //0 - 479
p->radius = rand() % MAX_RADIUS + 1; // 1 - 3
p->status = _STATUS::UP; //状态向上
p->step = rand() % MAX_STEP + 1; //步长 1- 5
int rgb = 255 * (p->step / MAX_STEP);
p->color = RGB(rgb, rgb, rgb); //颜色
}
// Añadir la lista (desmarques Francia)
BOOL linkInsert_front (_LinkNode * L, _LinkNode * nodo) {
// L: Las primeras estrellas en la dirección del nodo
// nodo: Adición de las estrellas en la dirección del nodo
si (L || nodo !!) {
false return;
}
if (L->next) {
L->next->prev = node;
}
node->next = L->next;
node->prev = L;
L->next = node;
return true;
}
//后插法
bool linkInsert_back (_LinkNode * L, _LinkNode * nodo) {
si (L || nodo!) {
Falsa retorno;
}
_LinkNode *last = L;
//找到最后一个的地址
while (last->next) {
last = last->next;
}
node->next = NULL;
node->prev = last;
last->next = node;
return true;
}
// pantalla Estrella
vacío starDisplay (_STAR * y L_star) {
SI {(L_star || L_star-> node.next !!)
de retorno;
}
//指向第一个链表节点
_LinkNode *p = L_star->node.next;
//查看距离的_STAR头的位置
int offset = offsetof(_STAR, node);
while (p) {
_STAR *tmp = (_STAR *)((size_t)p - offset);
//绘制图像
setfillcolor(tmp->color);
solidcircle(tmp->x, tmp->y, tmp->radius);
//指向下一个星星结构体里的node地址
p = p->next;
}
}
// estrella móvil
moveStar void (* _STAR y L_star) {
SI (L_star || L_star-> node.next!) {
Return;
}
//指向第一个星星
_LinkNode *p = L_star->node.next;
//查看距离的_STAR头的位置
int offset = offsetof(_STAR, node);
while (p) {
_STAR *tmp = (_STAR *)((size_t)p - offset);
//擦除原来位置的星星
setfillcolor(BLACK);
solidcircle(tmp->x, tmp->y, tmp->radius);
//判断移动状态,通过步长移动坐标
//默认都是UP
switch (tmp->status) {
case _STATUS::UP:
tmp->y -= tmp->step;
if (tmp->y <= 0) tmp->y = SCREEN_HEIGHT;
break;
case _STATUS::DOWN:
tmp->y += tmp->step;
if (tmp->y >= SCREEN_HEIGHT) tmp->y = 0;
break;
case _STATUS::LEFT:
tmp->x -= tmp->step;
if (tmp->x <= 0) tmp->x = SCREEN_WIDTH;
break;
case _STATUS::RIGTH:
tmp->x += tmp->step;
if (tmp->x >= SCREEN_WIDTH) tmp->x = 0;
break;
case _STATUS::ALL_STATUS:
default:
break;
}
//绘制新位置图像
setfillcolor(tmp->color);
solidcircle(tmp->x, tmp->y, tmp->radius);
//遍历到下一个星星的node地址
p = p->next;
}
}
int main (void) {
// primero puntero estructura en estrella
_STAR * L_star = NULL;
//存其他的添加的星星结构体
_STAR *s_star = NULL;
//初始化星星首节点
starInit(L_star);
//添加MAX_STAR个星星
for (int i = 0; i < MAX_STAR; i++) {
s_star = new _STAR;
//初始化分配空间里的星星各个属性
initStar(s_star);
//分配星星里node的链表,形成双向链表[后插法]
linkInsert_back(&(L_star->node), &(s_star->node));
}
//显示图像
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
HWND hwnd = GetHWnd();
SetWindowText(hwnd, _T("星空图"));
//显示星空
starDisplay(L_star);
//星星不断移动
while (1) {
//移动完所有星星
moveStar(L_star);
//就休息50ms
Sleep(50);
}
system("pause");
//关闭图像
closegraph();
return 0;
}