Browse Source

Improve drawing

Isolate configurable parameters.
master
TheoryOfNekomata 3 years ago
parent
commit
78f1471cc6
1 changed files with 16 additions and 10 deletions
  1. +16
    -10
      GameOfLife/MainForm.cs

+ 16
- 10
GameOfLife/MainForm.cs View File

@@ -1,11 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace GameOfLife namespace GameOfLife
@@ -20,11 +15,13 @@ namespace GameOfLife
private Random random; private Random random;
private int gridSize = 4;
private int gridSize = 8;
private int magicCells = 0; private int magicCells = 0;
private double density = 0.45;
private double density = 0.35;
private int interval = 500;
public MainForm(Rectangle bounds) public MainForm(Rectangle bounds)
{ {
@@ -49,7 +46,7 @@ namespace GameOfLife
Cursor.Hide(); Cursor.Hide();
TopMost = true; TopMost = true;
timer.Interval = 50;
timer.Interval = interval;
timer.Tick += new EventHandler(timer_Tick); timer.Tick += new EventHandler(timer_Tick);
timer.Start(); timer.Start();
} }
@@ -159,20 +156,29 @@ namespace GameOfLife
} }
private void Redraw(Graphics graphics) { private void Redraw(Graphics graphics) {
graphics.FillRectangle(Brushes.Black, Bounds);
List<Rectangle> liveCells = new List<Rectangle>();
List<Rectangle> deadCells = new List<Rectangle>();
for (var x = 0; x < newCells.GetLength(0); x++) for (var x = 0; x < newCells.GetLength(0); x++)
{ {
for (var y = 0; y < newCells.GetLength(1); y++) for (var y = 0; y < newCells.GetLength(1); y++)
{ {
if (newCells[x, y]) if (newCells[x, y])
{ {
graphics.FillRectangle(Brushes.White, x * gridSize, y * gridSize, gridSize, gridSize);
liveCells.Add(new Rectangle(x * gridSize, y * gridSize, gridSize, gridSize));
}
else {
deadCells.Add(new Rectangle(x * gridSize, y * gridSize, gridSize, gridSize));
} }
//else if (!newCells[x, y]) { //else if (!newCells[x, y]) {
// graphics.FillRectangle(Brushes.Black, x * gridSize, y * gridSize, gridSize, gridSize); // graphics.FillRectangle(Brushes.Black, x * gridSize, y * gridSize, gridSize, gridSize);
//} //}
} }
} }
graphics.FillRectangle(Brushes.Black, Bounds);
//graphics.FillRectangles(Brushes.Black, deadCells.ToArray());
graphics.FillRectangles(Brushes.White, liveCells.ToArray());
} }
private void MainForm_Paint(object sender, PaintEventArgs e) private void MainForm_Paint(object sender, PaintEventArgs e)


Loading…
Cancel
Save