using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace sand1 { public partial class Form1 : Form { Bitmap BackBuffer; String penType = "wall"; Graphics dotter; int[,] picture = new int[300,300]; int[,] old = new int[300, 300]; Bitmap pic; Random RandomClass = new Random(); SolidBrush b = new SolidBrush(Color.Black); SolidBrush stone = new SolidBrush(Color.Gray); SolidBrush sand = new SolidBrush(Color.SandyBrown); int Xpos, Ypos; Rectangle rc = new Rectangle(90, 90, 50, 50); int threadTest = 1; int threadTest3 = 1; int threadTest4 = 1; bool go = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles(); pic = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); dotter = Graphics.FromImage(pic); dotter.Clear(SystemColors.ControlText); BackBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); Graphics DrawingArea = Graphics.FromImage(BackBuffer); //set a border //top and bottom //for all numbers between 0 and 284 for (int border1 = 0; border1 < 284; border1++) { picture[border1, 0] = 1; picture[border1, 263] = 1; } //sides //for every num between 0 and 264 for (int border2 = 0; border2 < 264; border2++) { //draw a dot at the far left and right at the height of 'border2' //283=far right picture[0, border2] = 1; picture[283, border2] = 1; } } private void Form1_MouseClick(object sender, MouseEventArgs e) { } private void calcSandMove(int[,] picture) { //scroll through the array and move all the sand //Whilst X is less than 284 for (int X = 284; X >= 0; X--) { //and Y is less than 264 for (int Y = 264; Y >= 0; Y--) { //if youre not at the edge if (X > 0 && X < 284) { if (Y == 264) { picture[X, Y] = 0; } //if the current pixel value is 2 if (picture[X, Y] == 2) { //Make the current pixel 0(black) picture[X, Y] = 0; //Generate the random number, either 1,2 or 3 int RandomNumber = RandomClass.Next(0, 3); //If the random number was 0 if (RandomNumber == 0) { //if theres a gap where i want to move, do it, otherwise stay here if (picture[X - 1, Y + 1] == 0) { //'subtract' //make a 'bit of sand' at 'X-1, Y+1' which is one to the left and one below the currently picture[X - 1, Y + 1] = 2; } else { //if it cant move down, move across(if possible) if (picture[X - 1, Y] == 0) { picture[X - 1, Y] = 2; } else { picture[X, Y] = 2; } } } else if (RandomNumber == 1) { if (picture[X, Y + 1] == 0) { //Nothing picture[X, Y + 1] = 2; } else { //if it cant move down, move across(if possible) if (picture[X + 1, Y] == 0) { picture[X + 1, Y] = 2; } else { picture[X, Y] = 2; } } } else if (RandomNumber == 2) { if (picture[X + 1, Y + 1] == 0) { //Add picture[X + 1, Y + 1] = 2; } else { picture[X, Y] = 2; } } } } } } } //Draw a 'sand' at the mouse co-ords private void setSand(int p, int p_2) { Xpos = p; Ypos = p_2; if (p > 0 && p < 284 && p_2 > 0 && p_2 < 264) { if (picture[p, p_2] == 0) { picture[p, p_2] = 2; } } } private void clear(int p, int p2) { Xpos = p; Ypos = p2; picture[p+1, p2+1] = 0; picture[p+1, p2] = 0; picture[p+1, p2-1] = 0; picture[p, p2+1] = 0; picture[p, p2] = 0; picture[p, p2-1] = 0; picture[p-1, p2+1] = 0; picture[p-1, p2] = 0; picture[p-1, p2-1] = 0; } private void setStone(int p, int p_2) { Xpos = p; Ypos = p_2; if (p > 0 && p < 284 && p_2 > 0 && p_2 < 264) { //middle if (picture[p, p_2] == 0) { picture[p, p_2] = 1; } //top left if (picture[p-1, p_2-1] == 0) { picture[p-1, p_2-1] = 1; } //top if (picture[p , p_2 - 1] == 0) { picture[p , p_2 - 1] = 1; } //top right if (picture[p + 1, p_2 - 1] == 0) { picture[p + 1, p_2 - 1] = 1; } //mid left if (picture[p - 1, p_2 ] == 0) { picture[p - 1, p_2 ] = 1; } //mid right if (picture[p +1, p_2 ] == 0) { picture[p + 1, p_2 ] = 1; } //bottom left if (picture[p - 1, p_2 + 1] == 0) { picture[p - 1, p_2 + 1] = 1; } //bottom mid if (picture[p , p_2 + 1] == 0) { picture[p , p_2 + 1] = 1; } //bottom right if (picture[p + 1, p_2 + 1] == 0) { picture[p + 1, p_2 + 1] = 1; } } } private void drawThePicture(int[,] picture) { //Draw the picture //Whilst X is less than 284 for (int X = 284; X >= 0; X--) { //and Y is less than 264 for (int Y = 263; Y >= 0; Y--) { //if the current pixel has changed since last time if (old[X, Y] != picture[X, Y]) { if (picture[X, Y] == 0) { dotter.FillRectangle(b, X, Y, 1, 1); rc = new Rectangle(X, Y, 1, 1); //Invalidate(rc); } else if (picture[X, Y] == 1) { dotter.FillRectangle(stone, X, Y, 1, 1); rc = new Rectangle(X, Y, 1, 1); //Invalidate(rc); } else if (picture[X, Y] == 2) { dotter.FillRectangle(sand, X, Y, 1, 1); rc = new Rectangle(X, Y, 1, 1); //Invalidate(rc); } } } } //Store the array(ie, the image) for (int X = 0; X < 284; X++) { for (int Y = 0; Y < 264; Y++) { old[X, Y] = picture[X, Y]; } } } private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(pic, 0, 0, ClientRectangle.Width, ClientRectangle.Height); } int moX, moY; private void Form1_MouseMove(object sender, MouseEventArgs e) { moX = e.X; moY = e.Y; if (e.Button == MouseButtons.Left) { if (threadTest==1) { Thread thrd1 = new Thread(new ThreadStart(frame)); thrd1.Start(); } } if (e.Button == MouseButtons.Right) { if (threadTest == 1) { Thread thrd4 = new Thread(new ThreadStart(frame4)); thrd4.Start(); } } /* for (int timer = 0; timer < 1; timer++) { setSand(e.X, e.Y); calcSandMove(picture); drawThePicture(picture); }*/ } private void frame() { threadTest = 0; setSand(moX, moY); threadTest = 1; } private void frame2() { Graphics Viewable = this.CreateGraphics(); for (int timer = 0; timer > -1; timer++) { for (int sander = 50; sander < 75; sander++) { if (go) { setSand(sander, 2); } } calcSandMove(picture); drawThePicture(picture); Invalidate(); Thread.Sleep(10); } } private void frame3() { threadTest3 = 0; calcSandMove(picture); drawThePicture(picture); Invalidate(rc); threadTest3 = 1; } private void frame4() { threadTest4 = 0; if (penType == "clear") { clear(moX, moY); } else { setStone(moX, moY); } threadTest4 = 1; } private void button1_Click(object sender, EventArgs e) { Thread thrd2 = new Thread(new ThreadStart(frame2)); if (!go) { thrd2.Start(); button1.Text = "Stop"; go = true; } else { button1.Text = "Go"; go = false; } } private void button_clear_Click(object sender, EventArgs e) { if (penType == "wall") { penType = "clear"; button_clear.Text = "clear"; } else { penType = "wall"; button_clear.Text = "wall"; } } } }