如何在此程序中添加一个鼠标点击,每次在C#中点击球时,会将得分增加一?

时间:2013-11-23 02:14:33

标签: c# xamarin-studio

如何在此程序中添加鼠标点击,每次在C#中点击球时,会将分数增加1?

 namespace Ball_timer_2005
 {
  public class Form1 : System.Windows.Forms.Form
  {

    const int radius = 20;
    const int velocity = 5;

    int xC, yC, xDelta=10, yDelta=10, xSize, ySize;  // class level variables

    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;

    public Form1()
    {
        InitializeComponent();

        // TODO: Add any constructor code after InitializeComponent call
        this.ResizeRedraw = true;      // Tell form to redraw itself when         resized
        timer1.Start();
        Form1_Resize(this, EventArgs.Empty);  // Force a Resize Event as pgm     starts
        //
    }

    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
            if (components != null) 
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
    }

    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Interval = 25;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Name = "Form1";
        this.Text = "Bouncing Ball";
        this.Resize += new System.EventHandler(this.Form1_Resize);

    }
    #endregion


    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }



    private void Form1_Resize(object sender, System.EventArgs e)
    {
        xSize = this.ClientSize.Width;   // Set current window size
        ySize = this.ClientSize.Height;
        xC = xSize/2;                    // Place ball in center of window 
        yC = ySize/2;
        DrawBall();                       // Draw the ball in the window        
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
        DrawBall();                       // Draw ball in next frame of animation       
    }

    private void DrawBall()
    {
        Graphics g = this.CreateGraphics();
        Brush b = new SolidBrush(this.BackColor);
        g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius); //erase old ball
        xC += xDelta;                                                   //move ball
        yC += yDelta;
        if ((xC+radius >= ClientSize.Width) || (xC - radius <= 0)) //check for wall hits
            xDelta = -xDelta;
        if ((yC+radius >= ClientSize.Height) || (yC - radius <= 0))
            yDelta = -yDelta;
        b = new SolidBrush(Color.GreenYellow);                                   // draw new ball
        g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius);
        b.Dispose();
        g.Dispose();
    }




}
}

1.这是我到目前为止的代码可以帮助一些人??? 1.这是我到目前为止的代码可以帮助一些人??? 1.这是我到目前为止可以帮助的代码吗?

2 个答案:

答案 0 :(得分:0)

这有用吗

http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-form

您可以使用它来检测鼠标位置/球宽半径的重叠

答案 1 :(得分:0)

您可以通过处理表单的MouseClick事件来执行此操作。

第1步:Ellipse坐标存储到Region中,以便稍后确定Mouse Click的位置。

        //Declare it as class members
        List<Region> regionList = new List<Region>();
        int EclipseX = 10;
        int EclipseY = 50;
        int BallWidth = 100;
        int BallHeight = 100;

//inside paint function
SolidBrush brush = new SolidBrush(Color.Blue);           
e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));

第2步:现在声明MouseClick的{​​{1}}事件

Form

第3步:现在处理this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick); 的{​​{1}}事件。

  1. MouseClick事件在Form提供MouseClick参数。
  2. MouseEventArgs有两个属性。
  3. 首先e MouseEventArgs为您提供property位置 X
  4. X-axis Mouse Clickproperty提供了Y的{​​{1}}位置。
  5. 您需要使用Y-axis属性将这些Mouse ClickX值与现有Y值进行比较。
  6. 您需要从Region获取每个IsVisible(在绘制时添加了Region List}并使用Ellipse类的IsVisible属性 检测鼠标是否点击了Region

    Ellipse
  7. 完整解决方案(示例代码):

            //declare it as class member
            int MouseClicksCount = 0;
            bool isfound=false;
            //event handler
            private void Form1_MouseClick(object sender, MouseEventArgs e)
            {       
                isfound=false;
                foreach(Region r in regionList)
                {
                if (r.IsVisible(e.X, e.Y))
                {
                    isfound=true;
                    break;
                }
                }
                if (isfound)
                    MouseClicksCount++;
            }