调用方法时矩形不会向上移动



我现在喜欢图形,我正在尝试创建一个随关键事件移动的矩形。这是代码:

用于创建矩形的类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
class RectangleClass
{
   // private Rectangle _rectangle;
    private int _positionX;
    private int _positionY;
    private int _height;
    private int _width;

    public RectangleClass(int positionX, int positionY, int height, int width)
    {
        _positionX = positionX;
        _positionY = positionY;
        _height = height;
        _width = width;
    }
    public int PositionX
    {
        get
        {
            return _positionX;
        }
        set
        {
            _positionX = value;
        }
    }
    public int PositionY
    {
        get
        {
            return _positionY;
        }
        set
        {
            _positionY = value;
        }
    }
    public int Height
    {
        get
        {
            return _height;
        }
        set
        {
            _height = value;
        }
    }
    public int Width
    {
        get
        {
            return _width;
        }
        set
        {
            _width = value;
        }
    }

    public Rectangle CreateRectangle()
    {
         Rectangle rectangle = new Rectangle(PositionX, PositionY, Width, Height);
            return rectangle;
    }

    public int MoveYUpWard()
    {
        return PositionY += 10;
    }
    public int MoveYDownWard()
    {
        return PositionY -= 10;
    }

  }
}

和形式:

 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 Snake
    {
        public partial class Form1 : Form
        {
            RectangleClass rc;
            public Form1()
            {

                InitializeComponent();
                rc = new RectangleClass(30, 90, 50, 50);
            }
            private Graphics g;

            private void Form1_Paint(object sender, PaintEventArgs e)
            {


                if (g == null)
                {
                    g = this.CreateGraphics();
                }
                Pen pen = new Pen(Color.Blue, 10);
                g.DrawRectangle(pen, rc.CreateRectangle());
            }

            private void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up)
                {
                    MessageBox.Show("UP");
                    //Sets position y to 100
                    rc.MoveYUpWard();


                }
            }

我已经调试了代码,当按下箭头键时,方法MoveYUpWard()确实会更改矩形的位置,但矩形的位置是相同的。

我将不胜感激。

可能是问题就在这里。 试试这段代码。

public int MoveYUpWard()
{
    PositionY -= 10;
    return PositionY;
}
public int MoveYDownWard()
{
    PositionY += 10;
    return PositionY;
}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        MessageBox.Show("UP");
        //Sets position y to 100
        rc.MoveYUpWard();
        this.Invalidate();
    }
}

相关内容

  • 没有找到相关文章