using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class Form1 : Form
List<Ball> listOfBalls = new List<Ball>();
List<PictureBox> listOfPics = new List<PictureBox>();
List<Thread> listThreads = new List<Thread>();
static Boolean paused = false;
public static Boolean Paused
get { return Form1.paused; }
set { Form1.paused = value; }
static public Int32 Count
private void Form1_Load(object sender, EventArgs e)
foreach (var i in listOfPics)
Random rand = new Random();
for (int i = 0; i < 15; i++)
listOfBalls.Add(new Ball(Width - 5 - 30, Height - 25 - 30));
listOfPics.Add(new PictureBox());
PictureBox p = listOfPics[i];
listThreads.Add(b.Thread);
p.Location = new System.Drawing.Point(b.X, b.Y);
p.Size = new System.Drawing.Size(20, 20);
p.BackColor = Color.FromArgb(rand.Next(256),
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, 20, 20);
p.Region = new Region(gp);
Controls.Add(listOfPics[i]);
private void timer1_Tick(object sender, EventArgs e)
private void Form1_Paint(object sender, PaintEventArgs e)
for (int i = 0; i <15; i++)
PictureBox p = listOfPics[i];
private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
foreach (var i in listThreads)
pauseToolStripMenuItem.Text = "Resume";
foreach (var i in listThreads)
pauseToolStripMenuItem.Text = "Pause";
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
foreach (var i in listThreads)
stopToolStripMenuItem.Text = "Start";
stopToolStripMenuItem.Text = "Stop";
Thread thread = new Thread(Move);
int x, y, xmax, ymax, vx, vy;
public Ball(int x, int y)
Random random = new Random();
fVx = (double)random.Next(-6, 6);
fVy = (double)random.Next(-6, 6);
thread.IsBackground = true;
static void Move(Object oBall)
Ball ball = oBall as Ball;
if ((ball.x >= ball.xmax && ball.vx >= 0) || (ball.x <= 0 && ball.vx <= 0))
if ((ball.y >= ball.ymax && ball.vy >= 0) || (ball.y <= 0 && ball.vy <= 0))