![]() |
Platforms, Frameworks & Libraries »
Game Development »
Games
Beginner
License: The Code Project Open License (CPOL)
"Memory". Playing in order to improve your memory.By Gerard Castelló ViaderMaking a simple game in C# and WinForms. |
C#, .NET (.NET3.5), Visual-Studio (VS2008), WinForms, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
When I was a child, I used to play to a game called "Memory". It consisted in find a couples of images hidden among a mixture of items. It was funny and it served to improved your memory.
Now, I have developed a similar game. It is easy, but if you want, you can do it more difficult, increasing the number of similar items that you have to search or decreasing the number of allowed mistakes.
Well, it is simple.
There is the class Table that it have a two-dimensional matrix. The bounds of this matrix will depend of the number of groups that the user had configured. When the app is thrown, this class is instantiated in order to fill this matrix with a group of random number from 1 to 10. Each group has one number defined.
Then, the app generates the GUI, creating one panel per number. At the same time, it links the OnClick Event for every panel created.
At the same time that you are discovering items, each panel is put into a collection. When the number of tries for each turn is completed, the items into the collection are compared.
If they are equal, they stay unconcealed, by the contrary, they are removed from the collection and the panels are hidden again.
The game lasts until the number of errors allowed is exceeded.
private void Initialize()
{
Random rnd = new Random();
int x = 0;
int y = 0;
int currentNum = 0;
int totalNums = (this.panel.GetLength(0) * this.panel.GetLength(1)) / this.groups;
while (x < this.panel.GetLength(0))
{
while (y < this.panel.GetLength(1))
{
do
{
currentNum = rnd.Next(1, totalNums+1);
}
while (CheckGroup(currentNum, this.groups));
this.panel[x, y] = currentNum;
y++;
}
y = 0;
x++;
}
}
private void ShowTable()
{
int x = 0;
int y = 0;
int xStart = 20;
int yStart = 30;
int width = this.tb.Panel.GetLength(1);
int height = this.tb.Panel.GetLength(0);
this.Width = width * 50;
this.Height = height * 62;
while (x < height)
{
while (y < width)
{
Panel p = new Panel();
p.Name = x.ToString() + "-" + y.ToString();
p.BorderStyle = BorderStyle.Fixed3D;
p.Location = new Point(xStart, yStart);
p.Size = new Size(40, 40);
p.BackColor = Color.Gray;
p.Click += new EventHandler(this.ClickPanel);
this.Controls.Add(p);
xStart += 40;
y++;
}
yStart += 40;
xStart = 20;
y = 0;
x++;
}
}
private void ClickPanel(object sender, EventArgs e)
{
Panel p = (Panel)sender;
if (!p.HasChildren)
{
string[] axis = p.Name.Split('-');
int x = int.Parse(axis[0]);
int y = int.Parse(axis[1]);
PictureBox pBox = this.GetNumber(this.tb.Panel[x, y]);
p.Controls.Add(pBox);
p.BackColor = Color.Beige;
this.Refresh();
this.openPanels.Add(p);
if (this.find == 0)
{
this.find = this.tb.Panel[x, y];
this.tries++;
}
else if (this.tries == this.groups)
{
if (!this.AreEqual())
{
this.CleanMistake();
this.SetMistake();
if (awdMistakes == mistakes)
{
if (MessageBox.Show("YOU LOSE!" + Environment.NewLine + "A new game will start!", "Lose",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
this.Restart();
}
}
this.find = 0;
this.tries = 1;
this.openPanels.Clear();
}
else
{
this.tries++;
}
}
}
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Nov 2009 Editor: |
Copyright 2009 by Gerard Castelló Viader Everything else Copyright © CodeProject, 1999-2010 Web19 | Advertise on the Code Project |