我正在努力制作一个具有特定主题的迷你游戏。但是,我在使用精灵时遇到了麻烦。每当我移动它时,它会不断地绘制精灵,留下一堆垃圾。 我该如何解决这个问题?
这是一张图片。
:编辑:
我应该详细说明:
我使用BitMap作为精灵图像,使用Graphics对象来显示它们。但是,因为我对C#图形很新,并且还没有完全理解这些对象(图形,矩形,位图等)。
以下是整个班级的代码。 如果我做错了,我道歉。
private void Tmr_Tick(object sender, EventArgs e)
{
//This method changes the position of the sprite
if (u && Area.Top > -3) { Area.Y -= speed; Sprite.DrawImage(ReDraw(), Area); lastDirection = 0; }
if (d && Area.Bottom < (this.Height - 30)) { Area.Y += speed; Sprite.DrawImage(ReDraw(), Area); lastDirection = 2; }
if (r && Area.Right < (this.Width - 13)) { Area.X += (speed + 1); Sprite.DrawImage(ReDraw(), Area); lastDirection = 1; }
if (l && Area.Left > 0) { Area.X -= (speed + 1); Sprite.DrawImage(ReDraw(), Area); lastDirection = 3; }
this.Text = lastDirection.ToString();
}
private void btStart_Click(object sender, EventArgs e)
{
//This is irrelevant for the most part
btStart.Visible = false;
Tmr.Interval = 10;
StepControl.Interval = 89;
StepControl.Start();
Tmr.Start();
this.Focus();
Sprite.DrawImage(ReDraw(), Area);
}
private void btStart_KeyUp(object sender, KeyEventArgs e)
{
}
private void VS2013_KeyDown(object sender, KeyEventArgs e)
{
//Uses keys to control movement
switch (e.KeyCode)
{
case Keys.Up: { u = true; break; }
case Keys.Down: { d = true; break; }
case Keys.Left: { l = true; break; }
case Keys.Right: { r = true; break; }
case Keys.P:
{
if (speed == 1) { speed = 2; break; }
else { speed = 1; break; }
}
}
}
private void VS2013_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up: { u = false; break; }
case Keys.Down: { d = false; break; }
case Keys.Left: { l = false; break; }
case Keys.Right: { r = false; break; }
}
}
private Bitmap ReDraw()
{
//This method returns an image for the Graphics object "Scene" to draw.
//"Draw" will change based on certain directional circumstaces.
try
{
//Below line allows for transparency
//Scene.Clear(Color.Transparent);
Scene.Clear(this.BackColor);
Scene.DrawImage(Draw, Area);
return Background;
}
catch
{
//Below line allows for transparency
//Scene.Clear(Color.Transparent);
Scene.Clear(this.BackColor);
Scene.DrawImage(LinkDown, Area);
return Background;
}
}
private void StepControl_Tick(object sender, EventArgs e)
{
if (lastDirection == 2)
{
if (mirror) { mirror = false; Draw = LinkDown2; }
else { mirror = true; Draw = LinkDown; }
}
if (lastDirection == 1)
{
if (mirror) { mirror = false; Draw = LinkRight2; }
else { mirror = true; Draw = LinkRight; }
}
if (lastDirection == 3)
{
{
if (mirror) { mirror = false; Draw = LinkLeft2; }
else { mirror = true; Draw = LinkLeft; }
}
}
if (lastDirection == 0)
{
{
if (mirror) { mirror = false; Draw = LinkUp2; }
else { mirror = true; Draw = LinkUp; }
}
}
}
private void VS2013_Load(object sender, EventArgs e)
{
}
private void EraseBack()
{
LinkDown.MakeTransparent(this.BackColor);
LinkDown2.MakeTransparent(this.BackColor);
LinkRight.MakeTransparent(this.BackColor);
LinkRight2.MakeTransparent(this.BackColor);
LinkLeft.MakeTransparent(this.BackColor);
LinkLeft2.MakeTransparent(this.BackColor);
LinkUp.MakeTransparent(this.BackColor);
LinkUp2.MakeTransparent(this.BackColor);
}