c语言——easyx小人行走

#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment( lib, "MSIMG32.LIB")
#define High 600
#define  Width 1024

int bird_x[40];
int bird_y[40];
int hm_x,hm_y;
int i;
int left_i=0;
int right_i=0;
int x,y;
int direction=-1;
IMAGE img_bd1,img_bd2,img_hm,img_bk,img_hm2;

void startup() //数据初始化
{
	srand((unsigned int)time(NULL));
    x = 0;
	y = High - 100;
	initgraph(Width,High);
	loadimage(&img_bk,"D:\\resources3\\bk.jpg");
	loadimage(&img_bd1,"D:\\resources3\\bird1.jpg");
    loadimage(&img_bd2,"D:\\resources3\\bird2.jpg");
	loadimage(&img_hm,"D:\\resources3\\hmwalk.jpg");
	loadimage(&img_hm2,"D:\\resources3\\hmwalk2.jpg");
	for (i = 0; i < 40; i++)
	{
		bird_x[i] = rand() % Width;
		bird_y[i] = rand() % High;
	}  
    BeginBatchDraw();
}



void show()
{	putimage(0,0,&img_bk);

	for (i = 0; i < 40; i++)
	{
	
		putimage(bird_x[i],bird_y[i],&img_bd1,NOTSRCERASE);
		putimage(bird_x[i],bird_y[i],&img_bd2,SRCINVERT);
	}
	clearrectangle(x,y,x+75,y+130);
    if (direction==-1)
	{
	
		putimage(x,y,75,130,&img_hm2,left_i*75,0,NOTSRCERASE);
		putimage(x,y,75,130,&img_hm,left_i*75,0,SRCINVERT);
	}
	if (direction==1)
	{
		
		putimage(x,y,75,130,&img_hm2,right_i*75,130,NOTSRCERASE);
		putimage(x,y,75,130,&img_hm,right_i*75,130,SRCINVERT);
	}
	 FlushBatchDraw();
	 Sleep(20);
}

void updateWithInput()   //与用户输入有关的更新
{
	char input;
	if (kbhit())
	{
	input = getch();
	if (input == 'a' )
	{
		left_i++;
		x = x-10;
	    direction = -1;
	}
	if (input == 'd' )
	{
		right_i++;
		x = x+10;
		direction = 1;
	}
	}

}


void  updateWithoutInput()   //与用户输入无关的更新
{
	

   	for (i = 0; i < 40; i++)
	{
		if (bird_y[i]==High)
		{
			bird_y[i]=0;
			bird_x[i]= rand()% Width;
		}
	}
	for (i = 0; i < 40; i++)
	{
		bird_y[i]+=3;
	}
	if (right_i==3)
		right_i=0;
	if (left_i==3)
		left_i=0;
}


int main()
{
	
	startup(); 
	while(1)  
	{
		show();    //显示画面
		updateWithoutInput(); 
		updateWithInput(); 
	}
	EndBatchDraw();
	closegraph();
	return 0;
	
}

你可能感兴趣的:(单片机)