Iv made conways game of life, but I want the user to be able to select a pre defined pattern or for them to be able to choose different shaped brushes then draw with that brush.
Aswell, when I use the radio buttons to change the size, the program goes slow and stops working, does anyone know why that is?
And, Iv tried to create an eraser type tool using brush color.empty, but it doesnt work, how can I create an eraser tool, or a button to completely clear what the user has drawn, everything Iv tried has not worked.
One more thing, when I change the size the squares drawn do not align to the grid properly, can anyone see why?
Any help with any of these things would be great.
Cheers
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GameOfLife
{
public partial class Form1 : Form
{
cField Field = new cField();
bool MouseIsDown = false;
Brush mybrush = new SolidBrush(Color.White);
private int squaresize = 4;
private int gridSizex = 602;
private int gridSizey = 500;
private int Lines = 1;
private int squareheight = 4;
private int squarewidth = 4;
public Form1()
{
InitializeComponent();
timer1.Interval = trackBar1.Value;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (this.radioSmall.Checked)
{
squaresize = 4;
squareheight = 4;
squarewidth = 4;
this.pictureBox1.Refresh();
}
if (this.radioMedium.Checked)
{
squaresize = 8;
squareheight = 8;
squarewidth = 8;
this.pictureBox1.Refresh();
}
if (this.radioLarge.Checked)
{
squaresize = 16;
squareheight = 16;
squarewidth = 16;
this.pictureBox1.Refresh();
}
if (Lines == 1)
{
Graphics grid = e.Graphics;
Pen myPen = new Pen(Color.Yellow, 1); //create a pen object
for (int a = 0; a <= this.gridSizex; a += this.squaresize)
{
grid.DrawLine(myPen, 0, a, gridSizex, a); //use the DrawLine Horizontal
grid.DrawLine(myPen, a, 0, a, gridSizey); //use the DrawLine Vertical
}
myPen.Dispose();
}
for (int i = 0; i < 150; i++)
for (int j = 0; j < 100; j++)
if (Field.Cells[i, j] > 0)
e.Graphics.FillRectangle(mybrush, i * 4, j * 4, squarewidth, squareheight);
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
pictureBox1.Refresh();
btnStart.Text = timer1.Enabled ? "Stop" : "Start";
}
private void timer1_Tick(object sender, EventArgs e)
{
Field.Recalculate();
pictureBox1.Refresh();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
timer1.Enabled = false;
MouseIsDown = true;
Field.Cells[e.X / 4, e.Y / 4] = 1;
break;
case MouseButtons.Right:
timer1.Enabled = false;
MouseIsDown = true;
Field.Cells[e.X / 4, e.Y / 4] = 1;
break;
default:
break;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < 0) return;
if (e.Y < 0) return;
if (e.X / 4 > 149) return;
if (e.Y / 4 > 99) return;
if (MouseIsDown)
{
Field.Cells[e.X / 4, e.Y / 4] = 1;
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
MouseIsDown = false;
if (btnStart.Text == "Stop")
timer1.Enabled = true;
}
public class cField
{
public int[,] Cells = new int[150, 100];
public int[,] newCells = new int[150, 100];
public void Recalculate()
{
int neighbours = 0;
for (int i = 0; i < 150; i++)
for (int j = 0; j < 100; j++)
newCells[i, j] = 0;
for (int i = 0; i < 149; i++)
for (int j = 0; j < 99; j++)
{
neighbours = 0;
if (i > 0 && j > 0)
{
if (Cells[i - 1, j - 1] > 0)
neighbours++;
if (Cells[i - 1, j] > 0)
neighbours++;
if (Cells[i - 1, j + 1] > 0)
neighbours++;
if (Cells[i, j - 1] > 0)
neighbours++;
if (Cells[i, j + 1] > 0)
neighbours++;
if (Cells[i + 1, j - 1] > 0)
neighbours++;
}
if (Cells[i + 1, j] > 0)
neighbours++;
if (Cells[i + 1, j + 1] > 0)
neighbours++;
if (neighbours < 2)
newCells[i, j] = 0;
if (neighbours > 3)
newCells[i, j] = 0;
if ((neighbours == 2 || neighbours == 3) && Cells[i, j] > 0)
newCells[i, j] = Cells[i, j] + 10;
if (neighbours == 3 && Cells[i, j] == 0)
newCells[i, j] = 1;
}
for (int i = 0; i < 150; i++)
for (int j = 0; j < 100; j++)
Cells[i, j] = newCells[i, j];
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = timer1.Interval - 25;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Interval = timer1.Interval + 25;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
timer1.Interval = trackBar1.Value;
}
private void button1_Click_1(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "Black":
mybrush = new SolidBrush(Color.Black);
break;
case "Blue":
mybrush = new SolidBrush(Color.Blue);
break;
case "Brown":
mybrush = new SolidBrush(Color.Brown);
break;
case "Crimson":
mybrush = new SolidBrush(Color.Crimson);
break;
case "Cyan":
mybrush = new SolidBrush(Color.Cyan);
break;
case "Gold":
mybrush = new SolidBrush(Color.Gold);
break;
case "Gray":
mybrush = new SolidBrush(Color.Gray);
break;
case "Green":
mybrush = new SolidBrush(Color.Green);
break;
case "Indigo":
mybrush = new SolidBrush(Color.Indigo);
break;
case "Lime":
mybrush = new SolidBrush(Color.Lime);
break;
case "Magenta":
mybrush = new SolidBrush(Color.Magenta);
break;
case "Maroon":
mybrush = new SolidBrush(Color.Maroon);
break;
case "Navy":
mybrush = new SolidBrush(Color.Navy);
break;
case "Olive":
mybrush = new SolidBrush(Color.Olive);
break;
case "Orange":
mybrush = new SolidBrush(Color.Orange);
break;
case "Pink":
mybrush = new SolidBrush(Color.Pink);
break;
case "Purple":
mybrush = new SolidBrush(Color.Purple);
break;
case "Red":
mybrush = new SolidBrush(Color.Red);
break;
case "Silver":
mybrush = new SolidBrush(Color.Silver);
break;
case "Teal":
mybrush = new SolidBrush(Color.Teal);
break;
case "Violet":
mybrush = new SolidBrush(Color.Violet);
break;
case "Yellow":
mybrush = new SolidBrush(Color.Yellow);
break;
default:
break;
}
return;
}
private void trackBar1_Scroll_1(object sender, EventArgs e)
{
timer1.Interval = trackBar1.Value;
}
private void clearbtn_Click(object sender, EventArgs e)
{
mybrush = new SolidBrush(Color.Empty);
}
private void button1_Click_2(object sender, EventArgs e)
{
}
}
}