开发者

moving graphics in Windows Forms

开发者 https://www.devze.com 2023-02-01 21:46 出处:网络
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.

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 c开发者_JAVA百科an 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消