C# - 根据按钮单击向左,向右,向上,上下移动



我正在尝试通过开发一个小小的突破游戏来学习C#。它的表现不错,但我正在努力根据用户点击向上,向下,左右移动球。我什至不知道从哪里开始,因为我的代码很混乱。这是我的代码:

public partial class Form2 : Form
{
    Graphics drawingArea, paperBall, paperBat;
    Pen penBall;
    SolidBrush brushBAt = new SolidBrush(Color.Black);
    private int x, y, xChange, yChange;
    Random ranNum;
    public Form2()
    {
        InitializeComponent();
        drawingArea = playground.CreateGraphics();
        paperBall = playground.CreateGraphics();
        paperBat = playground.CreateGraphics();
        lblLives.Text = "5";
        lblScore.Text = "0";
        ranNum = new Random();
        drawBricks();
    }
    private void btnYes_Click(object sender, EventArgs e)
    {
        drawBricks();
        drawBat();
        drawBall();
    }

    public void drawBricks()
    {
        SolidBrush blackpen = new SolidBrush(Color.Black);
        SolidBrush redpen = new SolidBrush(Color.Red);
        SolidBrush yellowpen = new SolidBrush(Color.Yellow);
        SolidBrush bluepen = new SolidBrush(Color.Blue);
        SolidBrush greenpen = new SolidBrush(Color.Green);
        drawingArea.FillRectangle(blackpen, 0, 0, 80, 25);
        drawingArea.FillRectangle(redpen, 150, 0, 80, 25);
        drawingArea.FillRectangle(yellowpen, 350, 0, 80, 25);
        drawingArea.FillRectangle(bluepen, 550, 0, 80, 25);
        drawingArea.FillRectangle(greenpen, 750, 0, 80, 25);
    }
    public void drawBat()
    {
        paperBat = playground.CreateGraphics();
        playground.MouseMove += new
            System.Windows.Forms.MouseEventHandler(picDraw_MouseHover);
        //drawBall();
    }
    private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        //paperBat.Clear(Color.White);

    }
     private void picDraw_MouseHover(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        paperBat.FillRectangle(brushBAt, e.X + -30, playground.Height - 15, 75, 10); //First one: high up it is Second one: how long it is Third one: 
        drawBricks();
    }

    public void drawBall()
    {
        penBall = new Pen(Color.Red);
        penBall.Width = 10;
        timer1.Interval = 50;
        timer1.Enabled = true;
        x = ranNum.Next(-10, playground.Height); //MAkes the ball spawn randomly
        y = ranNum.Next(-10, playground.Width);  //MAkes the ball spawn randomly

        xChange = ranNum.Next(1, 20); yChange = ranNum.Next(1, 20); //Changes speed
        //drawBricks();
        //drawBat();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        x = x + xChange;
        y = y + yChange;
        if (x >= playground.Width)
            xChange = -xChange;
        if (y >= playground.Height)
            yChange = -yChange;
        if (x <= 0)
            xChange = -xChange;
        if (y <= 0)
            yChange = -yChange;
       // paperBall.Clear(Color.White);
        paperBat.Clear(Color.White);
        paperBall.DrawEllipse(penBall, x, y, 10, 10); //Height and width of ball
        drawBricks();

    }
    private void btnUp_Click(object sender, EventArgs e)
    {
    }
    private void btnRight_Click(object sender, EventArgs e)
    {
    }
    private void btnLeft_Click(object sender, EventArgs e)
    {
    }
    private void btnDown_Click(object sender, EventArgs e)
    {
    }
}

}

任何人可以帮我吗?

您需要在picdraw中添加一个键盘事件处理程序,然后编码每个键的作用(更改球位置)。

这是Microsoft的一个示例:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress(v = vs.110).aspx

如果我没记错的话,您需要查找特定键的ASCII密钥代码。

将来考虑使用Unity,因为目前您还没有为要实现的目标使用正确的技术。

相关内容

  • 没有找到相关文章

最新更新