Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For instance take a board with a form which moves from left to right and vice versa , i want this to be random. Actually, i want the landing of a ship to be random. Sample code that I wrote.I want to work only in the x-axis.



C#
public partial class theForm : Form
    {
        int xx;
        int x;
        int dx = 1;
        public theForm()
        {
            InitializeComponent();
        x = picBall.Location.X;
        xx = 0;

    }

    private void theForm_Load(object sender, EventArgs e)
    {

    }

    Random rnd = new Random();

    private void timer1_Tick(object sender, EventArgs e)
    {

        int rest = rnd.Next(picBoard.Location.X, this.ClientSize.Width);

        x += dx;
        if (x > rest)
        {
            dx *= -1;
            xx = rnd.Next(0, x - 5);
        }
        else if (x < 0)
        {
            dx *= -1;
        }
        else if (xx > x)
        {

            dx *= -1;
        }

        picBoard.Location = new Point(x, picBoard.Location.Y);
    }
}


What I have tried:

random erratic motion (x-axis) of a picture box control in c#
Posted
Comments
Kevin Marois 11-Mar-16 16:58pm    
What happens when you run it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900