Word Search Style Game
-
Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince
public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel
-
Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince
public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel
You should try a more object-oriented approach, you are really complexifying your life. Split this code in multiple small elements responsible of a single responsibility. The rectangle that holds a letter is one object. If you make a UserControl for this simple responsibility, you can delegate the complexity of catching MouseEnter/MouseLeave/etc... events in the control itself. Via two small event handlers, you can highlight and unhighlight your control without having to determine any coordinate. The "host" of you multiple rectangles is another UserControl. Its only responsibility is to put all the small rectangles in a bigger rectangle. Eventually, it computes the size that a single "letter rectangle" can have, as it knows it own size and the total number of smaller rectangles to put in itself. And finally, generating letters is another responsibility. The generation of the letters occurs outside, is provided to the big UserControl, and it will instantiate the correct number of small rectanges, providing the text and the size. Don't play with positioning that you compute yourself, use docking. All this code can be reduced to 10 lines.
Jean-Christophe Grégoire
-
Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince
public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel
You can draw text using the .DrawString method. That would go in the _Paint handler;
// Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected\[cellCol, cellRow\] == true) { this.customColor = Color.FromArgb(100, 100, 100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); gfx.DrawString("X", this.Font, Brushes.Wheat, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); myBrush.Dispose(); } else { // Cell is not selected gfx.DrawString("X", this.Font, Brushes.Green, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); } cellCol++; } cellRow++;
I are troll :)
-
You can draw text using the .DrawString method. That would go in the _Paint handler;
// Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected\[cellCol, cellRow\] == true) { this.customColor = Color.FromArgb(100, 100, 100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); gfx.DrawString("X", this.Font, Brushes.Wheat, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); myBrush.Dispose(); } else { // Cell is not selected gfx.DrawString("X", this.Font, Brushes.Green, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); } cellCol++; } cellRow++;
I are troll :)
Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?
-
Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?
Don't do that like that, compute the letters list where you want, give it to your "grid container" UserControl which it is supposed to know the list of "letter controls" that it contains. Loop in your letters list and give a letter to each sub-control. Each sub-control is supposed to have a stupid Text property, in which you can set the value of a label that would be docked full in your sub-control. Really, don't put all this useless complexity like that.
Jean-Christophe Grégoire
-
Don't do that like that, compute the letters list where you want, give it to your "grid container" UserControl which it is supposed to know the list of "letter controls" that it contains. Loop in your letters list and give a letter to each sub-control. Each sub-control is supposed to have a stupid Text property, in which you can set the value of a label that would be docked full in your sub-control. Really, don't put all this useless complexity like that.
Jean-Christophe Grégoire
Hi Jean-Christophe, i'm only quite new to C# and programming as a whole and i'm not really understanding what your meaning. Maybe a good see what you mean with some pseudo code or just initial code to get me on my way?
-
Hi Jean-Christophe, i'm only quite new to C# and programming as a whole and i'm not really understanding what your meaning. Maybe a good see what you mean with some pseudo code or just initial code to get me on my way?
Okay sorry, give me some more time and I will mail you a sample project ;-)
Jean-Christophe Grégoire
-
Okay sorry, give me some more time and I will mail you a sample project ;-)
Jean-Christophe Grégoire
Thank you Jean-Christophe, i really appreciate your help. I think once i see what your meaning i will be able to get a grasp of it! Many thanks
-
Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?
string drawThis = "Hello world";
int cellRow = 0;
while (cellRow < this.maxRow)
{
int cellCol = 0;
while (cellCol < this.maxCol)
{
if (this.cellSelected[cellCol, cellRow] == true)
{
this.customColor = Color.FromArgb(100, 100, 100);SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); if (cellCol < drawThis.Length) gfx.DrawString( drawThis\[cellCol\].ToString(), this.Font, Brushes.Wheat, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); myBrush.Dispose(); } else { // Cell is not selected if (cellCol < drawThis.Length) gfx.DrawString( drawThis\[cellCol\].ToString(), this.Font, Brushes.Green, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize)); } cellCol++; } cellRow++;
}
..and yes, there are more paths to Rome :)
I are troll :)
-
Thank you Jean-Christophe, i really appreciate your help. I think once i see what your meaning i will be able to get a grasp of it! Many thanks
Hi VinceAshbySmith, I made your sample project, how can I provide the zip file ?
Jean-Christophe Grégoire
-
Hi VinceAshbySmith, I made your sample project, how can I provide the zip file ?
Jean-Christophe Grégoire
Hi Jean-Christophe not sure if you got my email, so if you could send the code sample to vinny.as@tiscali.co.uk, that would be great!