0
我需要通過在兩個不同方向上移動Panel上的對象來獲得幫助。例如,通過按下右側和左側按鈕在右側和下方向。 Java有一個關鍵的發佈方法嗎c#有這樣一個類似的方法嗎?c#需要通過向兩個方向移動對象來提供幫助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace runningRectangle
{
enum Position {Left, Right, Down, Up, LeftUp, LeftDown, RightUp, RightDown }
public partial class Form1 : Form
{
private int x;
private int y;
private Position objPos;
private int speed = 10;
private bool stopPressed = false;
public Form1()
{
InitializeComponent();
x = 50;
y = 50;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, x, y, 50, 50);
}
private void timer1_Tick(object sender, EventArgs e)
{
if(objPos == Position.Right)
{
x += speed;
}else if(objPos == Position.Left)
{
x -= speed;
}
else if (objPos == Position.Down)
{
y += speed;
}
else if (objPos == Position.Up)
{
y -= speed;
} else if (objPos == Position.RightUp)
{
x += speed;
y -= speed;
}
else if (objPos == Position.RightDown)
{
x += speed;
y += speed;
}
else if (objPos == Position.LeftUp)
{
x -= speed;
y += speed;
}
else if (objPos == Position.LeftDown)
{
x -= speed;
y -= speed;
}
Invalidate();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
objPos = Position.Right;
}
else if (e.KeyCode == Keys.Left)
{
objPos = Position.Left;
}
else if (e.KeyCode == Keys.Down)
{
objPos = Position.Down;
}
else if (e.KeyCode == Keys.Up)
{
objPos = Position.Up;
}
else if(e.KeyCode == Keys.Right && e.KeyCode == Keys.Down)
{
objPos = Position.RightDown;
}
else if (e.KeyCode == Keys.Right && e.KeyCode == Keys.Up)
{
objPos = Position.RightUp;
}
else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Down)
{
objPos = Position.LeftDown;
}
else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Up)
{
objPos = Position.LeftUp;
}
else if(e.KeyCode == Keys.Space)
{
if (!stopPressed)
{
speed = 0;
stopPressed = true;
}
else if(stopPressed)
{
speed += 10;
stopPressed = false;
}
}
}
}
}
C#有[KeyUp](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup(v = vs.110).aspx)事件。 –