【发布时间】:2019-06-28 19:09:07
【问题描述】:
我正在尝试制作一个游戏,其中主要对象 Hero 在地图上移动并杀死敌人。
该对象表示为带有背景图像的矩形。
我找到了几种正确导入图像的解决方案,但似乎都不是很好,所有这些都只是在矩形内重复图像。 更准确地说,有一些“隐藏”的重复图像,当我移动矩形时,它只是将它们揭开。
Image image = new Bitmap(Properties.Resources.Untitled);
TextureBrush tBrush = new TextureBrush(image);
g.FillRectangle(tBrush, X, Y, characterWidth, characterHeight);
当我只画一个矩形并使用Move 函数时,一切都很好,
每次我移动时,他都会重新绘制自己。但是当我导入背景图片时,一切都变得一团糟。
Move函数的实现:
public void Move(int width, int height, String direction, List<Obstacles.Rectangle> rectangles)
{
int oldX = this.X;
int oldY = this.Y;
if (direction == "UP")
{
this.Y -= 10;
if (IsCollided(rectangles) || this.Y < 0)
{
this.Y = oldY;
}
}
if (direction == "DOWN")
{
this.Y += 10;
if (IsCollided(rectangles) || this.Y > height)
{
this.Y = oldY;
}
}
if (direction == "LEFT")
{
this.X -= 10;
if (IsCollided(rectangles) || this.X < 0)
{
this.X = oldX;
}
}
if (direction == "RIGHT")
{
this.X += 10;
if (IsCollided(rectangles) || this.X > width)
{
this.X = oldX;
}
}
}
【问题讨论】:
-
这是 Unity 内部的吗?
-
忘了说,Windows 窗体。
-
你也可以分享
Move function吗? -
@SᴇM
Move只是增加/减少 X/Y 坐标,没什么特别的。 -
可能是因为你没有在 TextureBrush 构造函数中使用
WrapMode.Clamp。DrawImageUnscaled有其缺点。但是,当然,如果您对此感到满意,那就太好了。