Advertisement
maxkhl

Untitled

Feb 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. namespace WindowsFormsApplication2
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         Timer ticker = new Timer();
  6.  
  7.         List<ButtonPos> buttons = new List<ButtonPos>();
  8.  
  9.         struct ButtonPos
  10.         {
  11.             public Button button;
  12.             public Vector2 Velocity;
  13.             public int LifeTime;
  14.         }
  15.         struct Vector2
  16.         {
  17.             public float X;
  18.             public float Y;
  19.             public Vector2(float X, float Y)
  20.             {
  21.                 this.X = X;
  22.                 this.Y = Y;
  23.             }
  24.         }
  25.  
  26.         Random rnd = new Random();
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.             ticker.Tick += ticker_Tick;
  31.             ticker.Interval = 15;
  32.             ticker.Start();
  33.         }
  34.  
  35.  
  36.         DateTime time = DateTime.Now;
  37.         Vector2 Gravitation = new Vector2(0, 5.5f);
  38.         float factor = 0.05f;
  39.         void ticker_Tick(object sender, EventArgs e)
  40.         {
  41.             var diff = (DateTime.Now - time);
  42.             var ntime = new TimeSpan(2000000);
  43.             if (diff > ntime)
  44.             {
  45.                 buttons.Add(new ButtonPos
  46.                     {
  47.                         button = new Button(),
  48.                         Velocity = new Vector2(rnd.Next(-100, 100), -150 + rnd.Next(-100, 0)),
  49.                         LifeTime = 200
  50.                     });
  51.                 var newbutton = buttons[buttons.Count-1];
  52.  
  53.                 newbutton.button.Parent = this;
  54.                 newbutton.button.Location = label1.Location;
  55.                 newbutton.button.Text = "+";
  56.                 newbutton.button.Show();
  57.                 newbutton.button.Enabled = true;
  58.                 time = DateTime.Now;
  59.             }
  60.  
  61.  
  62.             for (int i = 0; i < buttons.Count; i++ )
  63.             {
  64.                 var btn = buttons[i];
  65.                 btn.button.Location = new Point((int)(btn.button.Location.X + btn.Velocity.X * factor), (int)(btn.button.Location.Y + btn.Velocity.Y * factor));
  66.  
  67.                 if (btn.button.Location.Y > this.ClientSize.Height)
  68.                     btn.Velocity = new Vector2(btn.Velocity.X, btn.Velocity.Y * -1);
  69.  
  70.                 btn.Velocity = new Vector2(btn.Velocity.X + Gravitation.X, btn.Velocity.Y + Gravitation.Y);
  71.                 btn.LifeTime--;
  72.  
  73.                 if (btn.LifeTime < 0)
  74.                 {
  75.                     buttons.RemoveAt(i);
  76.                     btn.button.Hide();
  77.                 }
  78.                 else
  79.                     buttons[i] = btn;
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement