// ------------------------------------------------------ // Using Directives // ------------------------------------------------------ using System; using System.Drawing; using System.Windows.Forms; // ------------------------------------------------------ // BallForm // ------------------------------------------------------ public class BallForm : Form { public BallForm() { // ------------------------------------------------------ // Give our ball an initial location and speed // ------------------------------------------------------ mBallX = GetX(); mBallY = GetY(); mBallXSpeed = GetSpeed(); mBallYSpeed = GetSpeed(); // ------------------------------------------------------ // Set up our timer and our form // ------------------------------------------------------ mMoveBallTimer.Enabled = true; mMoveBallTimer.Interval = 10; mMoveBallTimer.Tick += new System.EventHandler(MoveBallTick); // ------------------------------------------------------ // Use double buffering for reducing flicker // ------------------------------------------------------ Size = new Size(FORM_WIDTH, FORM_HEIGHT); FormBorderStyle = FormBorderStyle.Fixed3D; Text = "Bouncing Ball"; Paint += new PaintEventHandler(RenderBall); SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true ); UpdateStyles(); } // ------------------------------------------------------ // Get a random: 'speed', 'x' or 'y' location // ------------------------------------------------------ private int GetSpeed() { return(mRandom.Next(MIN_SPEED, MAX_SPEED)); } private int GetX() { return(mRandom.Next(0, ClientSize.Width - BALL_WIDTH)); } private int GetY() { return(mRandom.Next(0, ClientSize.Height - BALL_HEIGHT)); } // ------------------------------------------------------ // MoveBallTick - Update the ball's location // and handle collisions // ------------------------------------------------------ private void MoveBallTick(object sender, EventArgs e) { mBallX += mBallXSpeed; if (mBallX < 0 || mBallX + BALL_WIDTH > ClientSize.Width) mBallXSpeed = -mBallXSpeed; mBallY += mBallYSpeed; if (mBallY < 0 || mBallY + BALL_HEIGHT > ClientSize.Height) mBallYSpeed = -mBallYSpeed; Refresh(); } // ------------------------------------------------------ // RenderBall - Draw the ball at its current location // ------------------------------------------------------ private void RenderBall(object sender, PaintEventArgs e) { e.Graphics.Clear(BackColor); e.Graphics.FillEllipse( Brushes.Blue, mBallX, mBallY, BALL_WIDTH, BALL_HEIGHT ); e.Graphics.DrawEllipse( Pens.Black, mBallX, mBallY, BALL_WIDTH, BALL_HEIGHT ); } // ------------------------------------------------------ // Settings // ------------------------------------------------------ private const int FORM_WIDTH = 500; private const int FORM_HEIGHT = 500; private const int MIN_SPEED = 1; private const int MAX_SPEED = 5; private const int BALL_WIDTH = 25; private const int BALL_HEIGHT = 25; // ------------------------------------------------------ // Member variables // ------------------------------------------------------ private Random mRandom = new Random(); private Timer mMoveBallTimer = new System.Windows.Forms.Timer(); private int mBallX; private int mBallY; private int mBallXSpeed; private int mBallYSpeed; // ------------------------------------------------------ // Main // ------------------------------------------------------ [STAThread] static void Main() { Application.Run(new BallForm()); } }