在 Windows 窗体中移动图形
我正在尝试使用 C# 创建一个简单的游戏,以更好地了解该语言。
游戏很简单:玩家控制周围的船只,并可以射击一些也在周围移动的东西。
到目前为止,我有这个:到目前为止
public partial class Form1 : Form
{
Rectangle r1 = new Rectangle(new Point(100, 100), new Size(100, 150));
Matrix rotateMatrix = new Matrix();
GraphicsPath gp = new GraphicsPath();
public Form1()
{
InitializeComponent();
gp.AddRectangle(r1);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawRectangle(new Pen(Color.Beige), r1);
this.lblPoint.Text = "X-pos: " + r1.X + " Y-pos: " + r1.Y;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.D:
r1.X += 10;
break;
case Keys.A:
r1.X -= 10;
break;
case Keys.W:
r1.Y -= 10;
break;
case Keys.S:
r1.Y += 10;
break;
case Keys.T:
rotateMatrix.RotateAt(45, new Point(50, 50));
gp.Transform(rotateMatrix);
break;
default:
break;
}
Invalidate();
Update();
}
}
,我可以很好地移动矩形(容器),但是当使用键旋转矩形时,没有发生太多事情,而且我似乎无法弄清楚出了什么问题。 我希望能够顺时针和逆时针旋转容器。
我做错了什么,或者根本没做什么?
I'm trying to create a simple game using c# to get to know the language better.
The game is to be simple: the player controls a vessel around and can shoot at some stuff which also moves around.
So far I have this:
public partial class Form1 : Form
{
Rectangle r1 = new Rectangle(new Point(100, 100), new Size(100, 150));
Matrix rotateMatrix = new Matrix();
GraphicsPath gp = new GraphicsPath();
public Form1()
{
InitializeComponent();
gp.AddRectangle(r1);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawRectangle(new Pen(Color.Beige), r1);
this.lblPoint.Text = "X-pos: " + r1.X + " Y-pos: " + r1.Y;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.D:
r1.X += 10;
break;
case Keys.A:
r1.X -= 10;
break;
case Keys.W:
r1.Y -= 10;
break;
case Keys.S:
r1.Y += 10;
break;
case Keys.T:
rotateMatrix.RotateAt(45, new Point(50, 50));
gp.Transform(rotateMatrix);
break;
default:
break;
}
Invalidate();
Update();
}
}
So far I can move the rectangle (vessel) around fine, but when it comes to rotating the rectangle using a key not much happens, and I can't seem to figure out what's wrong.
I want to be able to rotate the vessel both clockwise and counter-clockwise.
What am I doing wrong, or not doing at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下链接对您有用吗? 旋转示例 VB .NET 或 C#
关于您所说的更好地学习语言,我认为创建一个围绕业务的应用程序可能是您更好的选择,以便更好地掌握 C#。如果你真的想制作游戏,我会建议 XNA,它基本上是一个在 .NET 中创建游戏的框架。
Would the following link be of any use to you? Rotate Example VB.NET or C#
Regarding what you said to learn the language better I think creating an application which revolves around business might be a better option for you in order to get the hang of C# better. If you really want to make a game however I would suggest XNA which is basically a framework to create games in .NET.