當前位置:編程學習大全網 - 編程語言 - 用C語言畫簡單的小人

用C語言畫簡單的小人

TC2.0裏有壹個bgidemo.c的圖形編程示例程序。

其中有壹個演示屏幕貼圖的子程序,壹個外星人的飛船在屏幕上飛來飛去。

這個程序可以簡單地修改壹下就可以用於妳的需求了。

程序不難看懂。

這個代碼我找到了。大概說壹下。LZ需要自己去找壹下完整的代碼研究,這裏我只貼出相關的壹段。

void PutImageDemo(void)

{

static int r = 20;

static int StartX = 100;

static int StartY = 50;

struct viewporttype vp;

int PauseTime, x, y, ulx, uly, lrx, lry, size, i, width, height, step;

void *Saucer;

MainWindow("GetImage / PutImage Demonstration");

getviewsettings( &vp );

/* Draw Saucer */ 下面是用繪畫的方式畫了壹個簡單的飛碟。這個飛碟圖用於之前復制到內存裏備用。

setfillstyle( SOLID_FILL, getmaxcolor() );

fillellipse(StartX, StartY, r, (r/3)+2);

ellipse(StartX, StartY-4, 190, 357, r, r/3);

line(StartX+7, StartY-6, StartX+10, StartY-12);

circle(StartX+10, StartY-12, 2);

line(StartX-7, StartY-6, StartX-10, StartY-12);

circle(StartX-10, StartY-12, 2);

/* Read saucer image */ 這裏開始把那個飛碟的小圖圖復制到壹個內存緩沖區裏。先計算大小,需要的內存大小。

ulx = StartX-(r+1);

uly = StartY-14;

lrx = StartX+(r+1);

lry = StartY+(r/3)+3;

width = lrx - ulx + 1;

height = lry - uly + 1;

size = imagesize(ulx, uly, lrx, lry);

Saucer = malloc( size ); // 分配內存

getimage(ulx, uly, lrx, lry, Saucer); // 搞到了。

putimage(ulx, uly, Saucer, XOR_PUT); // 這就在原位置上,以異或的方式畫壹下。用異或的方式繪圖,兩次繪制後,圖像正好就會消失為原來的背景。

但是,LZ的可能這樣不行,那就需要復雜壹些的繪制了,用人物的黑輪廓圖或上背景,得到鏤空,然後再把黑背景的角色用or方式繪上。

下面就是隨機地繪制了。

/* Plot some "stars" */

for ( i=0 ; i<1000; ++i )

putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);

x = MaxX / 2;

y = MaxY / 2;

PauseTime = 70;

/* until a key is hit */

while ( !kbhit() ) {

/* Draw the Saucer */

putimage(x, y, Saucer, XOR_PUT); /* draw image */

delay(PauseTime);

putimage(x, y, Saucer, XOR_PUT); /* erase image */

上面的還是兩次 xor,顯示圖片,消除圖片。

/* Move Saucer */

step = random( 2*r );

if ((step/2) % 2 != 0 )

step = -1 * step;

x = x + step;

step = random( r );

if ((step/2) % 2 != 0 )

step = -1 * step;

y = y + step;

if (vp.left + x + width - 1 > vp.right)

x = vp.right-vp.left-width + 1;

else

if (x < 0)

x = 0;

if (vp.top + y + height - 1 > vp.bottom)

y = vp.bottom-vp.top-height + 1;

else

if (y < 0)

y = 0;

}

free( Saucer );

Pause();

}

另外,團IDC網上有許多產品團購,便宜有口碑

  • 上一篇:怎樣教學生學成語?
  • 下一篇:hll什麽意思
  • copyright 2024編程學習大全網