Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views3 pages

Circles

The document defines a C# class 'Particles' for a Windows Forms application that manages animated circles on a form. It includes methods for setting up the circles, moving them based on their speed, and drawing them on the form using double buffering for smoother rendering. The 'Circles' class represents individual circles with properties such as size, position, speed, color, and opacity.

Uploaded by

fer.tiktk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Circles

The document defines a C# class 'Particles' for a Windows Forms application that manages animated circles on a form. It includes methods for setting up the circles, moving them based on their speed, and drawing them on the form using double buffering for smoother rendering. The 'Circles' class represents individual circles with properties such as size, position, speed, color, and opacity.

Uploaded by

fer.tiktk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.Properties
{
public class Particles
{
#region variables
public static Form _Instance;
public static (int, int)[] AvailableSpeeds;
public static int _CircleNum;
public static Random rand = new Random();
public static List<Circles> _Particles = new List<Circles>();
private static Bitmap _bufferBitmap;
#endregion
public static void Setup(Form Instance, int CircleNum, Color
CircleColor) //start
{
_Instance = Instance;
_CircleNum = CircleNum;
Instance.Paint += DrawCircles;
_bufferBitmap = new Bitmap(_Instance.Width, _Instance.Height);
List<(int, int)> speedsList = new List<(int, int)>();

for (int j = -1; j <= 1; j++)


{
for (int k = -1; k <= 1; k++)
{
if (j != 0 || k != 0)
{
speedsList.Add((j, k));
}
}
}

Shuffle(speedsList);

while (speedsList.Count < CircleNum * 2)


{
speedsList.AddRange(speedsList);
Shuffle(speedsList);
}

AvailableSpeeds = speedsList.Take(CircleNum * 2).ToArray();

for (int i = 0; i < CircleNum; i++)


{
var speedIndex = i % AvailableSpeeds.Length;
var (speedX, speedY) = AvailableSpeeds[speedIndex];
_Particles.Add(new Circles()
{
Size = rand.Next(10, 30), // Adjusted bubble size
Pos = new Point(rand.Next(Instance.Width),
rand.Next(Instance.Height)),
Speed = new Point(speedX, speedY),
color = CircleColor,
Opacity = 255,
});
}
}

public static void Shuffle<T>(List<T> list)


{
int n = list.Count;
while (n > 1)
{
n--;
int k = rand.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}

public static void MoveCircles(List<Circles> circles)


{
Random random = new Random();

for (int i = 0; i < circles.Count; i++)


{
Circles circle = circles[i];

int newX = circle.Pos.X + circle.Speed.X;


int newY = circle.Pos.Y + circle.Speed.Y;

if (newX - circle.Size < 0 || newX + circle.Size >


_Instance.ClientSize.Width)
{
circle.Speed = new Point(-circle.Speed.X, circle.Speed.Y);
}

if (newY - circle.Size < 0 || newY + circle.Size >


_Instance.ClientSize.Height)
{
circle.Speed = new Point(circle.Speed.X, -circle.Speed.Y);
}

circle.Pos = new Point(newX, newY);


circles[i] = circle;
}
}

public static void DrawCircles(object sender, PaintEventArgs e)


{
if (_Instance.WindowState != FormWindowState.Minimized)
{
using (Graphics bufferGraphics = Graphics.FromImage(_bufferBitmap))
{
bufferGraphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
bufferGraphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
bufferGraphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
bufferGraphics.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
bufferGraphics.CompositingMode =
System.Drawing.Drawing2D.CompositingMode.SourceOver;
bufferGraphics.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
bufferGraphics.Clear(_Instance.BackColor);
DrawCircles(bufferGraphics);
}

e.Graphics.DrawImage(_bufferBitmap, Point.Empty);
}
}

public static void DrawCircles(Graphics g)


{
foreach (var circle in _Particles)
{
g.FillEllipse(new SolidBrush(Color.FromArgb(circle.Opacity,
circle.color)),
circle.Pos.X - circle.Size, circle.Pos.Y -
circle.Size,
circle.Size * 2, circle.Size * 2);
}
}
}

public class Circles


{
public int Size { get; set; }
public Point Pos { get; set; }
public Point Speed { get; set; }
public Color color { get; set; }
public int Opacity { get; set; }
}
}

You might also like