putimage()显示图片,图片移动后原来留下来的图怎么处理,才能消失

0

#include <graphics.h>
#include <time.h>
#include <conio.h>
#include <ctype.h>
#include <iostream>

using namespace std;

#define MAXSTAR 600	// 星星总数

struct STAR
{
	double	x;
	int		y;
	double	step;
	int		color;
};

struct PLANE_XY
{
	int  	curr_x;
	int		curr_y;
	int		old_x;
	int		old_y;
};


STAR star[MAXSTAR];

// 初始化星星
void InitStar(int i)
{
	star[i].x = 0;
	star[i].y = rand() % 720;
	star[i].step = (rand() % 5000) / 1000.0 + 1;
	star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);	// 速度越快,颜色越亮
	star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}

// 移动星星
void MoveStar(int i)
{
	// 擦掉原来的星星
	putpixel((int)star[i].y, (int)star[i].x, 0);

	// 计算新位置
	star[i].x += star[i].step;
	if (star[i].x > 720)	InitStar(i);

	// 画新星星
	putpixel((int)star[i].y, (int)star[i].x, star[i].color);
}

// 主函数
int main(int argc, char *argv[])
{
	int w,h,ch;
	IMAGE img1,img2;

	PLANE_XY plane_xy;
	plane_xy.curr_x = 200 ; 
	plane_xy.curr_y = 670;
	plane_xy.old_x = 0;
	plane_xy.old_y = 0;

	srand((unsigned)time(NULL));	// 随机种子
	initgraph(480, 720);			// 创建绘图窗口

	// 初始化所有星星
	for(int i = 0; i < MAXSTAR; i++)
	{
		InitStar(i);
		star[i].x = rand() % 480;
	}

	
	// 加载图片
	loadimage(&img2,_T("image\\plane.png"));
	w = img2.getwidth();
	h = img2.getheight();

	// 显示图片
	putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);

	// 绘制星空,按任意键退出
	while(1)
	{
		plane_xy.old_x  = plane_xy.curr_x;
		plane_xy.old_y  = plane_xy.curr_y;
		if(_kbhit())//如果有按键按下,则_kbhit()函数返回真
		{
			 ch = _getch();//使用_getch()函数获取按下的键值
			 if(ch == 97 || ch  == 65)
			 {
				plane_xy.curr_x = plane_xy.curr_x-5;
				printf("curr_x-- : %d\n",plane_xy.curr_x);
				if(plane_xy.curr_x <0 || plane_xy.curr_x == 0)
				{
					plane_xy.curr_x = 0;
				}
				putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);
				putimage(plane_xy.old_x,plane_xy.old_y,&img1);
			 }
			 else if (ch == 100 || ch == 68)
			 {
				plane_xy.curr_x = plane_xy.curr_x+5;
				 printf("curr_x++ : %d\n",plane_xy.curr_x);
				if(plane_xy.curr_x >420)
				{
					plane_xy.curr_x = 420;
				}
				putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);
				putimage(plane_xy.old_x,plane_xy.old_y,&img1);
			 }
			 else if(ch == 119 || ch == 87)
			 {
				 plane_xy.curr_y = plane_xy.curr_y-5;
				  printf("curr_y-- : %d\n",plane_xy.curr_y);
				 if(plane_xy.curr_y <0 )
				{
					plane_xy.curr_y = 0;
				}
				putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);
				putimage(plane_xy.old_x,plane_xy.old_y,&img1);
			 }
			 else if(ch == 115 || ch == 83)
			 {
				 plane_xy.curr_y = plane_xy.curr_y+5;
				 printf("curr_y++ : %d\n",plane_xy.curr_y);
				 if(plane_xy.curr_y >680 )
				{
					plane_xy.curr_y = 680;
				}
				putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);
				putimage(plane_xy.old_x,plane_xy.old_y,&img1);
			 }

			 if(ch == 27)//当按下ESC时循环,ESC键的键值时27.
			{
				break;
			}
		}	
		for(int i = 0; i < MAXSTAR; i++)
			MoveStar(i);
		Sleep(20);	 
		putimage(plane_xy.curr_x,plane_xy.curr_y,w, h, &img2,0,0);
	}
	closegraph();					// 关闭绘图窗口
	return 0;
}
C C++
ava
你说呢

2021-1-21

0

图片没有“消失”的概念。“显示图片”的本质,是在显示缓冲区里面设置若干数据,即“内存赋值”。所以,没有什么让“内存的值消失”的技巧。你能做的,就是重新给这片内存赋值。

回到图片的概念上,你能做的,就是重新绘制原来位置的背景。

ava
慢羊羊

2021-1-21

不断重新绘制背景。 -  Forest  2021-1-28
技术讨论社区