一个下雪小程序,刚刚学写的
snow.zip
(86.99 KB)
某人让我发的,不过源码要等一会发,感觉有BUG,一会儿吃完饭再改改,御坂御坂说道
------
补充:修改了一下,不然没有VC2005的人运行不了
-----------------
再补充:
这里是新版本的程序:
snow.zip
(86.98 KB)
以下是源代码:
程序代码:
#define _GRAPHICS_LIB_LINK_ 1 #include"yzfy_graphics.h" #include"yzfy_dos.h" #define MAXSNOW 500 struct SNOW { int n; int lx, ly; int color; double x; double y; double cx; double dcx; double vx; double vy; double k; }; const int sc_width = 640; const int sc_height = 480; void InitSnow(struct SNOW *snow) { double vx = 0.5; double fvx = 0.5; double vy = (double)random(1000) / 1000; snow->x = random(sc_width); snow->y = 0; snow->cx = snow->x; snow->dcx = (double)random(1000) / 1000 * 2 * vx - vx; snow->k = (double)random(1000) / 1000 * -0.001; snow->vx = (double)random(1000) / 1000 * 2 * fvx - fvx; snow->vy = vy * 2.0 + 0.5; snow->color = (int)(192 * vy) + 64; snow->color = (((snow->color<<8) | snow->color) << 8) | snow->color; } void MoveSnow(struct SNOW *snow, double dt) { double d = dt * 60 / 1000; putpixel(snow->lx, snow->ly, 0); snow->x += snow->dcx * d; snow->cx += snow->dcx * d; snow->y += snow->vy * d; if (snow->y > sc_height) InitSnow(snow); snow->lx = (int)snow->x; snow->ly = (int)snow->y; putpixel(snow->lx, snow->ly, snow->color); } int __stdcall OnUpdate(void* param, float ms) { struct SNOW* snow = (struct SNOW *)param; int i; for (i = 1; i <= snow->n; i++) { snow[i].vx += (snow[i].x - snow[i].cx) * snow[i].k * 0.99; snow[i].x += snow[i].vx; } return 0; } int __stdcall OnRender(void* param, float ms) { int i; for (i = 1; i <= ((struct SNOW*)param)->n; i++) MoveSnow((struct SNOW*)param+i, ms); return 0; } int main() { { int g = TRUECOLORSIZE, m = sc_width | (sc_height << 16); initgraph(&g, &m, "Snow"); randomize(); } { struct SNOW snow[MAXSNOW+1]; int i; snow[0].n = MAXSNOW; for (i = 1; i <= MAXSNOW; i++) { InitSnow(snow + i); snow[i].y = random(sc_height); } ege_gameloop(OnUpdate, snow, 60, OnRender, snow, 0); } closegraph(); return 0; }至于代码所用到的库,请自己下载吧,御坂说
[ 本帖最后由 御坂美琴 于 2010-8-22 14:09 编辑 ]