已经生成的release版本exe文件图形移动很慢,但是打开vs2019后,运行马上正常。

0

萌新求助!!

已经生成的release版本exe文件,单独运行时图形移动很慢,但是打开vs2019后,运行马上正常。

只需要打开vs2019,哪怕不进行任何操作,移动速度都会马上恢复正常。关闭后,速度又会变慢。

#include <graphics.h>
#include <conio.h>
#include <math.h>

double GetDistance(double a, double b, double c, double d)
{
	int m, n;
	m = pow(fabs(a - c), 2.0);
	n = pow(fabs(b - d), 2.0);
	return sqrt(m+n);
}

int main(void)
{
	double x, y, vx, vy, x1, y1, vx1, vy1;
	x = 320.0;
	y = 240.0;
	vx = 1.0;
	vy = 1.0;
	x1 = 100.0;
	y1 = 150.0;
	vx1 = 1.0;
	vy1 = 1.0;

	initgraph(640, 480);
	BeginBatchDraw();
	while (1)
	{
		setcolor(BLUE);
		setfillcolor(GREEN);
		fillcircle(x, y, 20.0);
		fillcircle(x1, y1, 20.0);
		FlushBatchDraw();
		Sleep(3);

		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 20.0);
		fillcircle(x1, y1, 20.0);

		x = x + vx;
		y = y + vy;
		x1 = x1 - vx1;
		y1 = y1 - vy1;

		if (x == 620.0 || x == 20.0)
		{
			vx = -1 * vx;
		}

		if (y == 460.0 || y == 20.0)
		{
			vy = -1 * vy;
		}

		if (x1 == 620.0 || x1 == 20.0)
		{
			vx1 = -1 * vx1;
		}

		if (y1 == 460.0 || y1 == 20.0)
		{
			vy1 = -1 * vy1;
		}

		if (GetDistance(x,y,x1,y1) <= 40.0)
		{
			vx = -1 * vx;
			vy = -1 * vy;
			vx1 = -1 * vx1;
			vy1 = -1 * vy1;
		}

		
	}
	EndBatchDraw();
	closegraph();

	return 0;
}
ava
迷雾冰川

2021-8-15

1

我本机测试,并不能重现你的问题。

你可以发给同学,看看他们是否能重现你的问题。如果不能,可能是你的系统有问题。

同时,不建议使用 Sleep(3),因为 Sleep 的精度没有那么高。通常来说,Sleep 的精度是 18ms 左右。高精度延时可以参考:https://codebus.cn/yangw/accurate-delay

ava
慢羊羊

2021-8-15

windows 10 2004之后的Sleep()精度提高了吧?现在已经是1ms了 -  无名氏  2021-8-16
@无名氏  我觉得如果你的软件只考虑支持Windows 10 2004以上的版本可以直接用Sleep -  Margoo  2021-8-17
@无名氏 我当前用的 Win10 21H1,Sleep 精度没有任何变化。 -  慢羊羊  2021-8-17
技术讨论社区