Você está na página 1de 4

private void saveFile()

{
if (FileName == null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Balloons doc file (*.bal)|*.bal";
saveFileDialog.Title = "Save balloons doc";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog.FileName;
}
}
if (FileName != null)
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Create))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, balloonDoc);
}
}
}
private void openFile()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Balloons doc file (*.bal)|*.bal";
openFileDialog.Title = "Open balloons file";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog.FileName;
try
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
{
IFormatter formater = new BinaryFormatter();
balloonDoc = (BalloonDoc)formater.Deserialize(fileStream);
}
}
catch (Exception ex)
{
MessageBox.Show("Could not read file: " + FileName);
FileName = null;
return;
}
Invalidate(true);
}
}
// kod za dvizenje na topce

public double Velocity { get; set; } Velocity = 20;


public double Angle { get; set; }
Random r = new Random();
private float velocityX;
Angle = r.NextDouble() * 2 * Math.PI;
private float velocityY;
velocityX = (float)(Math.Cos(Angle) * Velocity);
velocityY = (float)(Math.Sin(Angle) * Velocity);

public void Move(int left, int top, int width, int height)
{
float nextX = center.X + velocityX;
float nextY = center.Y + velocityY;
if (nextX - 20 <= left || nextX + 20 >= width + left)
{
velocityX = -velocityX;
}
if (nextY - 20 <= top || nextY + 20 >= height + top)
{
velocityY = -velocityY;
}
center = new Point((int)(center.X + velocityX), (int)(center.Y + velocityY));
}
// color dialog

ColorDialog colorDialog = new ColorDialog();


if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentColor = colorDialog.Color;
}

DialogResult result = MessageBox.Show("Save document and exit", "Save document?",


MessageBoxButtons.YesNoCancel);
if (result == System.Windows.Forms.DialogResult.Cancel)
{
e.Cancel = true;
}
if (result == System.Windows.Forms.DialogResult.Yes)
{
saveFile();
}
if (e.KeyCode == Keys.Down)

polygonDoc.Draw(e.Graphics);
Pen squarePen = new Pen(Color.Black, 2);
squarePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Pen linePen = new Pen(Color.Black, 1);
linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (drawSquare)
{
e.Graphics.DrawRectangle(squarePen, startPoint.X - 5, startPoint.Y - 5, 10, 10);
}

namespace BlowingBalloons

[Serializable]
public class Balloon
{
public Point Center { get; set; }
public int Radius { get; set; }
public Color Color { get; set; }
public bool Flag { get; set; }
public Balloon(Point center, int radius)
{
Center = center;
Radius = radius;
Random r = new Random();
Color = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
Flag = false;
}

public bool IsTouching(Balloon ballon)


{
double distance = (Center.X - ballon.Center.X) * (Center.X - ballon.Center.X) + (Center.Y ballon.Center.Y) * (Center.Y - ballon.Center.Y);
return distance <= ((Radius + ballon.Radius) * (Radius + ballon.Radius));
}
public void Draw(Graphics g)
{
Brush brush = new SolidBrush(Color);
g.FillEllipse(brush, Center.X - Radius, Center.Y - Radius, 2 * Radius, 2 * Radius);
brush.Dispose();
}
}

namespace BlowingBalloons
{
[Serializable]
public class BalloonsDoc
{
public List<Balloon> Balloons { get; set; }
public BalloonsDoc()
{
Balloons = new List<Balloon>();
}
public void AddBalloon(Point point)
{
Random r = new Random();
Balloon balloon = new Balloon(point, r.Next(20, 30));
Balloons.Add(balloon);
}
public void BlowBalloons()

foreach (Balloon balloon in Balloons)


{
balloon.Radius += 5;
}

public void CheckForExplosions()


{
for (int i = 0; i < Balloons.Count; i++)
{
for (int j = i + 1; j < Balloons.Count; j++)
{
if (Balloons[i].IsTouching(Balloons[j]))
{
Balloons[i].Flag = true;
Balloons[j].Flag = true;
}
}
}
for (int i = Balloons.Count - 1; i >= 0; i--)
{
if (Balloons[i].Flag)
{
Balloons.RemoveAt(i);
}
}
}

public void DrawBalloons(Graphics g)


{
foreach (Balloon balloon in Balloons)
{
balloon.Draw(g);
}
}

toolStripStatusLabel1.Text = string.Format("# Balls: {0}", ballsDoc.Balls.Count);

Você também pode gostar