简体   繁体   中英

how can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?

How can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?

 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.this is the code i have so far can some one help ??? 1.this is the code i have so far can some one help ??? 1.this is the code i have so far can some one help ???

Does this help at all

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

You may be able to us this to detect an overlap of mouse position/ ball width radius

You can do this by Handling MouseClick event of the Form.

Step1: Store the Ellipse Co-ordinates into the Region to identify the position of the Mouse Click later.

        //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)));

Step2: now declare the MouseClick event of the Form

this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);

Step3: now handle the MouseClick event of the Form .

  1. the MouseClick event provides MouseEventArgs parameter here e .
  2. this MouseEventArgs has two properties.
  3. first property is X gives you the X-axis Location of the Mouse Click .
  4. second property is Y gives you the Y-axis location of the Mouse Click .
  5. you need to compare these X and Y values with existing Region values using IsVisible Property.
  6. you need to get the each Region from the List (which was added while drawing Ellipse ) and use IsVisible Property of the Region class to detect whether mouse clicked on the Ellipse or not.

      //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(eX, eY)) { isfound=true; break; } } if (isfound) MouseClicksCount++; } 

Complete Solution(Sample code):

    public partial class Form1 : Form
    {
        int MouseClicksCount = 0;
        List<Region> regionList = new List<Region>();

        int EclipseX = 10;
        int EclipseY = 50;
        int BallWidth = 100;
        int BallHeight = 100;
        bool isfound=false;
        public Form1()
        {
            InitializeComponent();
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
        }



        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++;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            SolidBrush brush = new SolidBrush(Color.Blue);           
            e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
            regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM