Advertisement
benshepherd

Simple bouncy balls anim

Oct 17th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. /* Note to pastebin viewers */
  2. /* Create a PictureBox on your form and set Dock property to full */
  3. /* Rename created PictureBox to pb */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13.  
  14. namespace SimpleAnimation
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         /// <summary>
  19.         /// Store information about balls. Positions, speeds etc
  20.         /// </summary>
  21.         List<Ball> balls = new List<Ball>();
  22.  
  23.         /// <summary>
  24.         /// Set event handlers
  25.         /// </summary>
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.  
  30.             pb.MouseDown += new MouseEventHandler(pb_MouseDown);
  31.             pb.Paint += new PaintEventHandler(pb_Paint);
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Updates and draw balls on screen
  36.         /// </summary>
  37.         /// <param name="sender"></param>
  38.         /// <param name="e"></param>
  39.         void pb_Paint(object sender, PaintEventArgs e)
  40.         {
  41.             e.Graphics.DrawString("Left click to create a ball", SystemFonts.DefaultFont, Brushes.Black, new PointF(15, 15));
  42.             e.Graphics.DrawString("Right click to remove last ball", SystemFonts.DefaultFont, Brushes.Black, new PointF(15, 30));
  43.  
  44.             for (int i = 0; i < balls.Count; i++)
  45.             {
  46.                 Ball b = balls[i];
  47.  
  48.                 //Screen bounds
  49.                 int top = 0;
  50.                 int right = pb.Width - b.Size;
  51.                 int bottom = pb.Height - b.Size;
  52.                 int left = 0;
  53.  
  54.                 //Left wall
  55.                 if (b.PosX < left)
  56.                 {
  57.                     b.PosX = left;
  58.                     b.VelX *= -1;
  59.                 }
  60.  
  61.                 //Right wall
  62.                 if (b.PosX > right)
  63.                 {
  64.                     b.PosX = right;
  65.                     b.VelX *= -1;
  66.                 }
  67.  
  68.                 //Top wall
  69.                 if (b.PosY < top)
  70.                 {
  71.                     b.PosY = top;
  72.                     b.VelY *= -1;
  73.                 }
  74.                
  75.                 //Bottom wall
  76.                 if (b.PosY > bottom)
  77.                 {
  78.                     b.PosY = bottom;
  79.                     b.VelY *= -1 + 0.5f; // Add a bounce
  80.                 }
  81.  
  82.                 //Gravity
  83.                 if (b.PosY < bottom)
  84.                 {
  85.                     b.VelY += 0.25f;
  86.                 }
  87.  
  88.                 //Update positions
  89.                 b.PosX += b.VelX;
  90.                 b.PosY += b.VelY;
  91.  
  92.                 //Draw ball
  93.                 e.Graphics.FillEllipse(Brushes.Black, b.PosX, b.PosY, b.Size, b.Size);
  94.  
  95.                 balls[i] = b;
  96.             }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Left click - Create ball
  101.         /// Right click - Remove last ball
  102.         /// </summary>
  103.         /// <param name="sender"></param>
  104.         /// <param name="e"></param>
  105.         void pb_MouseDown(object sender, MouseEventArgs e)
  106.         {
  107.             if (e.Button == System.Windows.Forms.MouseButtons.Left)
  108.             {
  109.                 balls.Add(new Ball(e.X-(Ball.DEFAULT_SIZE/2), e.Y-(Ball.DEFAULT_SIZE/2), 0, 1));
  110.             }
  111.             if (e.Button == System.Windows.Forms.MouseButtons.Right)
  112.             {
  113.                 if (balls.Count > 0)
  114.                 {
  115.                     balls.RemoveAt(balls.Count - 1);
  116.                 }
  117.             }
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Setup game timer to run every 10 ms
  122.         /// </summary>
  123.         /// <param name="sender"></param>
  124.         /// <param name="e"></param>
  125.         private void Form1_Load(object sender, EventArgs e)
  126.         {
  127.             Timer gameTimer = new Timer();
  128.             gameTimer.Tick += new EventHandler(gameTimer_Tick);
  129.             gameTimer.Interval = 10;
  130.             gameTimer.Enabled = true;
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Game timer tick, redraw picture box
  135.         /// </summary>
  136.         /// <param name="sender"></param>
  137.         /// <param name="e"></param>
  138.         void gameTimer_Tick(object sender, EventArgs e)
  139.         {
  140.             pb.Invalidate();
  141.         }
  142.     }
  143.  
  144.     public class Ball
  145.     {
  146.         public float PosX { get; set; }
  147.         public float PosY { get; set; }
  148.         public float VelX { get; set; }
  149.         public float VelY { get; set; }
  150.         public int Size { get; set; }
  151.         public const int DEFAULT_SIZE = 25;
  152.  
  153.         public Ball(int x, int y, int speedx, int speedy, int size = DEFAULT_SIZE)
  154.         {
  155.             PosX = x;
  156.             PosY = y;
  157.             VelX = speedx;
  158.             VelY = speedy;
  159.             Size = size;
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement