Advertisement
anlyx

1_A

Oct 3rd, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace _1_A
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Random rnd = new Random();
  16.         //Green Ball
  17.         private int ball1Size = 50;
  18.         private int ball1PosX;
  19.         private int ball1PosY;
  20.         private int step1X;
  21.         private int step1Y;
  22.         //Red Ball
  23.         private int ball2Size = 100;
  24.         private int ball2PosX;
  25.         private int ball2PosY;
  26.         private int step2X;
  27.         private int step2Y;
  28.  
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.  
  34.             //Green Ball random starting point and Stepping
  35.             ball1PosX = rnd.Next(0, this.panel1.Width - ball1Size);
  36.             ball1PosY = rnd.Next(0, this.panel1.Height - ball1Size);
  37.             step1X = rnd.Next(1, 5);
  38.             step1Y = rnd.Next(1, 5);
  39.             //Red Ball random starting point and Stepping
  40.             ball2PosX = rnd.Next(0, this.panel1.Width - ball2Size);
  41.             ball2PosY = rnd.Next(0, this.panel1.Height - ball2Size);
  42.             step2X = rnd.Next(-5, -1);
  43.             step2Y = rnd.Next(-5, -1);
  44.         }
  45.  
  46.         private void Button_Click(object sender, EventArgs e)
  47.         {
  48.             richTextBox1.Text = "Some Text HERE";
  49.         }
  50.  
  51.         private void button1_Click(object sender, EventArgs e)
  52.         {
  53.             this.timer1.Enabled = true;
  54.         }
  55.  
  56.         private void Paint_Circle(object sender, PaintEventArgs e)
  57.         {
  58.             e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  59.             e.Graphics.Clear(this.BackColor);
  60.             //Green Ball
  61.             e.Graphics.FillEllipse(Brushes.Green, ball1PosX, ball1PosY, ball1Size, ball1Size);
  62.             e.Graphics.DrawEllipse(Pens.Black, ball1PosX, ball1PosY, ball1Size, ball1Size);
  63.             //Red Ball
  64.             e.Graphics.FillEllipse(Brushes.Red, ball2PosX, ball2PosY, ball2Size, ball2Size);
  65.             e.Graphics.DrawEllipse(Pens.Black, ball2PosX, ball2PosY, ball2Size, ball2Size);
  66.         }
  67.  
  68.         private void MoveBall(object sender, EventArgs e)
  69.         {
  70.             //Green Ball Update position
  71.             ball1PosX += step1X;
  72.             if ( ball1PosX < 0 || ball1PosX + ball1Size > this.panel1.Width)
  73.             {
  74.                 step1X = -step1X;
  75.             }
  76.             ball1PosY += step1Y;
  77.             if (ball1PosY < 0 || ball1PosY + ball1Size > this.panel1.Height)
  78.             {
  79.                 step1Y = -step1Y;
  80.             }
  81.  
  82.             //Green Ball Update position
  83.             ball2PosX += step2X;
  84.             if (ball2PosX < 0 || ball2PosX + ball2Size > this.panel1.Width)
  85.             {
  86.                 step2X = -step2X;
  87.             }
  88.             ball2PosY += step2Y;
  89.             if (ball2PosY < 0 || ball2PosY + ball2Size > this.panel1.Height)
  90.             {
  91.                 step2Y = -step2Y;
  92.             }
  93.  
  94.             //Refresh Balls
  95.             this.panel1.Refresh();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement