在 Windows 窗体中移动图形

发布于 2024-10-09 11:31:11 字数 1483 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-10-16 11:31:11

以下链接对您有用吗? 旋转示例 VB .NET 或 C#

引用MSDN链接:
下面的例子是为
与 Windows 窗体一起使用,并且它
需要 PaintEventArgs e,一个 OnPaint
事件对象。该代码执行
以下操作:

先在屏幕上绘制一个矩形
应用旋转变换(
蓝色矩形)。创建一个矩阵并
将其旋转 45 度。适用于此
矩阵变换为矩形。
将变换后的矩形绘制到
屏幕(红色矩形)。

public void RotateExample(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 1);
Pen myPen2 = new Pen(Color.Red, 1);
// Draw the rectangle to the screen before applying the transform.
e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100);
// Create a matrix and rotate it 45 degrees.
Matrix myMatrix = new Matrix();
myMatrix.Rotate(45, MatrixOrder.Append);
// Draw the rectangle to the screen again after applying the
// transform.
e.Graphics.Transform = myMatrix;
e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100);
}

关于您所说的更好地学习语言,我认为创建一个围绕业务的应用程序可能是您更好的选择,以便更好地掌握 C#。如果你真的想制作游戏,我会建议 XNA,它基本上是一个在 .NET 中创建游戏的框架。

Would the following link be of any use to you? Rotate Example VB.NET or C#

Quote MSDN link:
The following example is designed for
use with Windows Forms, and it
requires PaintEventArgs e, an OnPaint
event object. The code performs the
following actions:

Draws a rectangle to the screen prior
to applying a rotation transform (the
blue rectangle). Creates a matrix and
rotates it 45 degrees. Applies this
matrix transform to the rectangle.
Draws the transformed rectangle to the
screen (the red rectangle).

public void RotateExample(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 1);
Pen myPen2 = new Pen(Color.Red, 1);
// Draw the rectangle to the screen before applying the transform.
e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100);
// Create a matrix and rotate it 45 degrees.
Matrix myMatrix = new Matrix();
myMatrix.Rotate(45, MatrixOrder.Append);
// Draw the rectangle to the screen again after applying the
// transform.
e.Graphics.Transform = myMatrix;
e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文