为什么我用了TransparentBlt和BitBLT两个函数来实现双缓冲制动画时,会有这么一个情况。如果在Bitblt之前不加一个背景图,则背景会闪,如果加了一个背景图,则前景会闪。

0

由于有图片的插入,所以不能调试,大神帮我康康吧!谢谢啦!

#include <graphics.h>
#include <conio.h>
#include<stdio.h>
#include<math.h>
#pragma  comment(lib,"Msimg32.lib")
#undef UNICODE
#define length 1000
#define height 1000
#define PAI 3.1415
#define sharkfishnumber 3
typedef struct snode {          //鲨鱼鱼儿结构体定义
	int lifenumber;               //生命
	int x;                            //坐标
	int y;
	double vx;                             //速度
	double vy;
	time_t jzjs;
	int sfjs;
	struct snode* next;
}SFISH;	
HDC current_hdc = NULL, buffer_hdc = NULL;
		HDC srcDC = NULL;
		HBITMAP bmp;
int CON = 0; IMAGE back, shayufore,shayufore1,fishxz1; SFISH* head1 = NULL, * P = NULL, * Q = NULL;
int main()
{
	int i = 0, j = 0; double fishjd;
	initgraph(length, height);
	for (i = 0; i <= sharkfishnumber - 1; i++) {                      //鲨鱼列表初始化
		P = (SFISH*)malloc(sizeof(SFISH));
		if (i == 0) {
			P->next = head1;
			head1 = P;
			head1->x = rand() % 800;
			head1->y = rand() % 800;
			head1->vx = -15;
			head1->vy = 14;
			head1->sfjs = 0;
			Q = head1;
		}
		else
		{
			P->next = NULL;
			P->x = rand() % 800;
			P->y = rand() % 800;
			P->sfjs = 0;
			P->vx = 15;
			P->vy = 14;
			Q->next = P;
			Q = P;
		}
	}
	loadimage(&shayufore1, _T("D:\\捕鱼达人背景图.jpg"), length , height);
	putimage(0, 0, &shayufore1);
	while (1) {                                     
	
		current_hdc = GetDC(GetHWnd());
		buffer_hdc = CreateCompatibleDC(NULL);
		bmp = CreateCompatibleBitmap(current_hdc, length , height);
		SelectObject(buffer_hdc, bmp);
		loadimage(&back, _T("D:\\捕鱼达人背景图.jpg"), length , height);
		srcDC = GetImageHDC(&back);
		TransparentBlt(buffer_hdc, 0, 0, length , height, srcDC, 0, 0, length , height, 0x000000);
		for (P = head1; P->next != NULL; P = P->next) {     //蓝鲨鱼的显示
			fishjd = atan(P->vy / P->vx);
			if (fishjd > 0) {
				if (P->vx > 0)
					fishjd = -(PAI - fishjd);
			}
			else if (fishjd <= 0) {
				if (P->vx > 0) {
					fishjd = PAI + fishjd;
				}
			}
			    if (j == 0)
				loadimage(&shayufore, _T("D:\\shark1.png"), 180, 180);//鲨鱼动作1
				if (j == 1)
				loadimage(&shayufore, _T("D:\\shark2.png"), 180, 180);//2
				if (j == 2)
				loadimage(&shayufore, _T("D:\\shark3.png"), 180, 180);//3
				if (j == 3)
				loadimage(&shayufore, _T("D:\\shark4.png"), 180, 180);//4
			rotateimage(&fishxz1, &shayufore, fishjd);
			srcDC = GetImageHDC(&fishxz1);
			TransparentBlt(buffer_hdc, P->x, P->y, 180, 180, srcDC, 0, 0, 180, 180, 0x000000);
		}
		
		BitBlt(current_hdc, 0, 0, length , height, buffer_hdc, 0, 0, SRCCOPY);

			j++;
			if (j > 3) {                                          //更新位置
				for (P = head1; P->next != NULL; P = P->next) {
                P->x = P->x + P->vx / 2;
				P->y = P->y - P->vy / 2;
				if (P->x < -200) {
					P->x = -200; P->vx = -P->vx;
				}
				if (P->x > length) {
					P->x = length;	P->vx = -P->vx;

				}
				if (P->y < -200) {
					P->y = -200;	P->vy = -P->vy;

				}
				if (P->y > height + 200) {
					P->y = height + 200;	P->vy = -P->vy;

				}
				}
				j = 0;
			
		}	
	}	
		closegraph();
}
ava
思密达

2021-5-6

0

整个 while 还需要使用个缓冲 

BeginBatchDraw()
while(1)
{
	...
	FlushBatchDraw()
}
EndBatchDraw();
...

可参考文档:https://docs.easyx.cn/zh-cn/BeginBatchDraw

ava
xiongfj ◑◑

2021-5-7