5.3C - Drawing Program - Saving and Loading
5.3C - Drawing Program - Saving and Loading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyGame;
using SplashKitSDK;
namespace Shapedrawerr
{
_background = value;
}
}
//Start
//This is an additional function to keep the color of the shape changing
//Like the previous part
public void ChangingShapeColor()
{
foreach (Shape s in _shapes)
{
if (s.Selected)
{
s.COLOR = Color.RandomRGB(255);
}
}
}
//End
}
}
//save n load
public void Save(string filename)
{
5.3C - Drawing Program - Saving and Loading
}
}
5.3C - Drawing Program - Saving and Loading
ExtensionMethod.cs
using System;
using System.IO;
using SplashKitSDK;
namespace MyGame
{
public static class ExtensionMethods
{
public static int ReadInteger(this StreamReader reader)
{
return Convert.ToInt32(reader.ReadLine ());
}
public static float ReadSingle(this StreamReader reader)
{
return Convert.ToSingle(reader.ReadLine ());
}
public static Color ReadColor(this StreamReader reader)
{
return Color.RGBColor(reader.ReadSingle(), reader.ReadSingle(),
reader.ReadSingle());
}
public static void WriteColor(this StreamWriter writter, Color clr)
{
writter.WriteLine("{0}\n{1}\n{2}", clr.R, clr.G, clr.B);
}
}
}
MyCircle.cs
using MyGame;
using SplashKitSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shapedrawerr
{
}
SplashKit.FillCircle(COLOR, X, Y, _radius);
}
}
}
Myline.cs
using MyGame;
using SplashKitSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shapedrawerr
5.3C - Drawing Program - Saving and Loading
{
public class MyLine : Shape
{
private int _length;
}
5.3C - Drawing Program - Saving and Loading
MyRectangle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyGame;
using SplashKitSDK;
namespace Shapedrawerr
{
public class MyRectangle : Shape
{
private int _width, _height;
}
}
5.3C - Drawing Program - Saving and Loading
Program.cs
using System;
using SplashKitSDK;
namespace Shapedrawerr
{
public class Program
{
private enum ShapeKind
{
Rectangle,
Circle,
Line
}
public static void Main()
{
if (SplashKit.KeyTyped(KeyCode.RKey))
{
kindToAdd = ShapeKind.Rectangle;
}
if (SplashKit.KeyTyped(KeyCode.LKey))
{
kindToAdd = ShapeKind.Line;
}
if (SplashKit.KeyTyped(KeyCode.CKey))
{
kindToAdd = ShapeKind.Circle;
}
//Start
//Use this to draw shape
if (SplashKit.MouseClicked(MouseButton.LeftButton))
{
if (kindToAdd == ShapeKind.Rectangle)
{
MyRectangle ShapeDrawn = new MyRectangle();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
if (kindToAdd == ShapeKind.Circle)
{
MyCircle ShapeDrawn = new MyCircle();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
5.3C - Drawing Program - Saving and Loading
if (kindToAdd == ShapeKind.Line)
{
MyLine ShapeDrawn = new MyLine();
ShapeDrawn.X = SplashKit.MouseX();
ShapeDrawn.Y = SplashKit.MouseY();
drawShape.AddShape(ShapeDrawn);
}
Console.WriteLine("Mouse Left");
//End
if (SplashKit.MouseClicked(MouseButton.RightButton))
{
drawShape.SelectedShapeAt(SplashKit.MousePosition());
Console.WriteLine("Mouse Right");
}
//Start
//Additional Function
if (SplashKit.KeyDown(KeyCode.EscapeKey))
{
drawShape.ChangingShapeColor();
Console.WriteLine("ESC");
}
//End
if (SplashKit.KeyTyped(KeyCode.BackspaceKey) ||
SplashKit.KeyTyped(KeyCode.DeleteKey))
{
if (SplashKit.KeyTyped(KeyCode.BackspaceKey))
{
Console.WriteLine("Backspace");
}
if (SplashKit.KeyTyped(KeyCode.DeleteKey))
{
Console.WriteLine("Delete");
}
drawShape.RemoveShape();
}
if (SplashKit.KeyTyped(KeyCode.SpaceKey))
{
drawShape.Background = SplashKit.RandomRGBColor(255);
Console.WriteLine("SpaceKey");
}
//save n load
if (SplashKit.KeyDown(KeyCode.SKey))
{
drawShape.Save("G:/shapes (2).txt");
if (SplashKit.KeyTyped(KeyCode.OKey))
{
try
{
drawShape.Load("G:/shapes (2).txt");
5.3C - Drawing Program - Saving and Loading
}
catch (Exception e)
{
Console.Error.WriteLine("Error loading file: {0}",
e.Message);
}
}
drawShape.Draw();
SplashKit.RefreshScreen();
}
while (!SplashKit.WindowCloseRequested("Drawing Shape"));
}
}
}
Shape.cs
using MyGame;
using SplashKitSDK;
namespace Shapedrawerr
{
public abstract class Shape
{
private Color _color;
private float _x, _y;
private int _width, _height;
private bool _selected;
public float X
{
get
{
return _x;
}
set
{
_x = value;
}
}
5.3C - Drawing Program - Saving and Loading
public float Y
{
get
{
return _y;
}
set
{
_y = value;
}
}