为什么动不了

0

vs2017想写个史蒂夫移动的代码,可怎么也动不了,求大佬帮帮忙。 (=´ω`=)

#include <stdio.h>
#include <graphics.h>

int stx = 240;
int sty = 120;

void tra(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor)
{
	// 变量初始化
	DWORD* dst = GetImageBuffer(dstimg);
	DWORD* src = GetImageBuffer(srcimg);
	int src_width = srcimg->getwidth();
	int src_height = srcimg->getheight();
	int dst_width = (dstimg == NULL ? getwidth() : dstimg->getwidth());
	int dst_height = (dstimg == NULL ? getheight() : dstimg->getheight());

	// 计算贴图的实际长宽
	int iwidth = (x + src_width > dst_width) ? dst_width - x : src_width;
	int iheight = (y + src_height > dst_height) ? dst_height - y : src_height;

	// 修正贴图起始位置
	dst += dst_width * y + x;

	// 修正透明色,显示缓冲区中的数据结构为 0xaarrggbb
	transparentcolor = 0xff000000 | BGR(transparentcolor);

	// 实现透明贴图
	for (int iy = 0; iy < iheight; iy++)
	{
		for (int ix = 0; ix < iwidth; ix++)
		{
			if (src[ix] != transparentcolor)
				dst[ix] = src[ix];
		}
		dst += dst_width;
		src += src_width;
	}
}

int run_1()
{
	if (GetAsyncKeyState(65) && GetAsyncKeyState(VK_LEFT)) // 左
	{
		stx--;
	}

	else if (GetAsyncKeyState(68) && GetAsyncKeyState(VK_RIGHT)) // 右
	{
		stx++;
	}
	return 0;
}


int main()
{
	int a = 0;
	IMAGE steve; // 史蒂夫贴图
	initgraph(640, 480);
	setbkcolor(TRANSPARENT);
	settextcolor(BLUE);
	BeginBatchDraw();
	setbkcolor(WHITE);
	cleardevice();
	loadimage(&steve, _T("E:/新建文件夹/素材/史蒂夫.png"), 24, 90);
	tra(NULL, stx, sty, &steve, WHITE); // 透明史蒂夫
	while (1)
	{
		run_1();
		tra(NULL, stx, 120, &steve, WHITE);
		FlushBatchDraw();
		cleardevice();
		Sleep(100);
	}
	closegraph();
	EndBatchDraw();
	return 0;
}
ava
潇潇

2021-7-12

0

获取按键部分写错了。这么改:

int run_1()
{
	if (GetAsyncKeyState(VK_LEFT) & 0x8000)	 // 左
	{
		stx--;
	}

	else if (GetAsyncKeyState(VK_RIGHT) & 0x8000) // 右
	{
		stx++;
	}
	return 0;
}

具体请参考:https://codebus.cn/yangw/detect-combination-keys-and-advanced-key-handling

ava
慢羊羊

2021-7-13

原来如此,谢谢大佬 -  潇潇  2021-7-13
技术讨论社区
相关提问