Moving Graphics in Windows Forms

I am trying to create a simple game using C # to learn the language better.

The game should be simple: the player controls the vessel around and can shoot at some material that also moves.

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 the fine, but when it comes to turning the rectangle with a key, it’s not so much, and I can’t understand what happened. I want to be able to rotate the vessel both clockwise and counterclockwise.

What am I doing wrong or not doing at all?

+3
source share
1 answer

Could the following link be useful to you? Rotate VB.NET or C # Example

MSDN:
Windows Forms, 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);
}

, , , , , , , #. , XNA, .NET.

+2

Source: https://habr.com/ru/post/1782637/


All Articles