I have to do an EtchaSketch program for my assignment. So far I have this code:
--------------------------------------…
public partial class Form1 : Form
{
int x = 50, y = 50; // starting position
Bitmap bm;
int x1 = 50, y1 = 50;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bm = new Bitmap(this.Width, this.Height); // create a form-size bitmap
Graphics g = Graphics.FromImage(bm); // get a graphic object for the bitmap
g.FillEllipse(Brushes.Blue, x, y, 20, 20); // put a circle in the bitmap
this.BackgroundImage = bm; // use the bitmap as the form background
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics h = e.Graphics; // get a graphic object for the bitmap
h.FillEllipse(Brushes.Blue, x1, y1, 20, 20);
}
At the moment when I press the down key, the second circle moves down the screen.
How do I make it so that when I press down, it forms a line (so basically makes a new circle and keeps the previous circle where it was), just like the etchasketch game.
You need to build up a list of the points that the circle as been to. Then in your Paint event handler, rather than just drawing the ellipse at x1, y1, you loop around all of the points drawing an ellipse at each....
Code:
// class members
private List<Point> _points = new List<Point>();
private Int32 _x = 50;
private Int32 _y = 50;
// paint handler
Graphics graphics = e.Graphics;
foreach (Point point in _points)
graphics.FillEllipse(Brushes.Blue, point.X, point.Y, 20, 20)
// in your process command...
if (input == "Down")
{
_y += 10;
// add the new point to the list
_points.Add(new Point(_x, _y);
// ... as before
}
You can now also easily implement a [Shake] button by simply calling:
_points.Clear();
and then refreshing....
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Bookmarks