Matching Game Using Visual Studio

Published on June 2016 | Categories: Documents | Downloads: 52 | Comments: 0 | Views: 260
of 15
Download PDF   Embed   Report

Matching Game Using Visual Studio

Comments

Content

Create a Matching Game using visual studio
In this tutorial, you build a matching game, where the player must match pairs of
hidden icons. You learn how to:

Hold objects using a List object.

Use a foreach loop in Visual C# or a For Each loop in Visual Basic.




Keep track of a form's state using reference variables.
Build an event handler that you can use with multiple objects.
Make a timer fire exactly once when started.

Copy this code and create project on visual studio and run it.

AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the
following
// set of attributes. Change these attribute values to modify the
information
// associated with an assembly.
[assembly: AssemblyTitle("MatchingGame")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MatchingGame")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not
visible
// to COM components. If you need to access a type in this assembly
from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project
is exposed to COM
[assembly: Guid("2de0ddab-3f63-45bd-831a-8a749ec29cdf")]
// Version information for an assembly consists of the following
four values:
//
//
Major Version
//
Minor Version

//
Build Number
//
Revision
//
// You can specify all the values or you can default
the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Form1.Designer.cs
namespace MatchingGame
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();

this.tableLayoutPanel1 = new
System.Windows.Forms.TableLayoutPanel();
this.label16 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.BackColor =
System.Drawing.Color.CornflowerBlue;
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Controls.Add(this.label16, 3, 3);
this.tableLayoutPanel1.Controls.Add(this.label15, 2, 3);
this.tableLayoutPanel1.Controls.Add(this.label14, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.label13, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.label12, 3, 2);
this.tableLayoutPanel1.Controls.Add(this.label11, 2, 2);
this.tableLayoutPanel1.Controls.Add(this.label10, 1, 2);
this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.label8, 3, 1);
this.tableLayoutPanel1.Controls.Add(this.label7, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.label6, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 1);

this.tableLayoutPanel1.Controls.Add(this.label4, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.
SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.
SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.
SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.
SizeType.Percent, 25F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(484, 517);
this.tableLayoutPanel1.TabIndex = 0;
//
// label16
//
this.label16.AutoSize = true;
this.label16.Dock = System.Windows.Forms.DockStyle.Fill;
this.label16.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label16.Location = new System.Drawing.Point(366, 387);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(115, 130);
this.label16.TabIndex = 15;
this.label16.Text = "c";
this.label16.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label16.Click += new System.EventHandler(this.label_Click);
//
// label15
//
this.label15.AutoSize = true;
this.label15.Dock = System.Windows.Forms.DockStyle.Fill;
this.label15.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label15.Location = new System.Drawing.Point(245, 387);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(115, 130);
this.label15.TabIndex = 14;
this.label15.Text = "c";
this.label15.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label15.Click += new System.EventHandler(this.label_Click);
//
// label14
//
this.label14.AutoSize = true;
this.label14.Dock = System.Windows.Forms.DockStyle.Fill;
this.label14.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label14.Location = new System.Drawing.Point(124, 387);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(115, 130);
this.label14.TabIndex = 13;
this.label14.Text = "c";
this.label14.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label14.Click += new System.EventHandler(this.label_Click);
//
// label13
//
this.label13.AutoSize = true;
this.label13.Dock = System.Windows.Forms.DockStyle.Fill;
this.label13.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label13.Location = new System.Drawing.Point(3, 387);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(115, 130);
this.label13.TabIndex = 12;
this.label13.Text = "c";
this.label13.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label13.Click += new System.EventHandler(this.label_Click);
//
// label12
//
this.label12.AutoSize = true;
this.label12.Dock = System.Windows.Forms.DockStyle.Fill;

this.label12.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label12.Location = new System.Drawing.Point(366, 258);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(115, 129);
this.label12.TabIndex = 11;
this.label12.Text = "c";
this.label12.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label12.Click += new System.EventHandler(this.label_Click);
//
// label11
//
this.label11.AutoSize = true;
this.label11.Dock = System.Windows.Forms.DockStyle.Fill;
this.label11.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label11.Location = new System.Drawing.Point(245, 258);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(115, 129);
this.label11.TabIndex = 10;
this.label11.Text = "c";
this.label11.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label11.Click += new System.EventHandler(this.label_Click);
//
// label10
//
this.label10.AutoSize = true;
this.label10.Dock = System.Windows.Forms.DockStyle.Fill;
this.label10.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label10.Location = new System.Drawing.Point(124, 258);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(115, 129);
this.label10.TabIndex = 9;
this.label10.Text = "c";
this.label10.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label10.Click += new System.EventHandler(this.label_Click);
//
// label9
//
this.label9.AutoSize = true;

this.label9.Dock = System.Windows.Forms.DockStyle.Fill;
this.label9.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label9.Location = new System.Drawing.Point(3, 258);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(115, 129);
this.label9.TabIndex = 8;
this.label9.Text = "c";
this.label9.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label9.Click += new System.EventHandler(this.label_Click);
//
// label8
//
this.label8.AutoSize = true;
this.label8.Dock = System.Windows.Forms.DockStyle.Fill;
this.label8.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label8.Location = new System.Drawing.Point(366, 129);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(115, 129);
this.label8.TabIndex = 7;
this.label8.Text = "c";
this.label8.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label8.Click += new System.EventHandler(this.label_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Dock = System.Windows.Forms.DockStyle.Fill;
this.label7.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label7.Location = new System.Drawing.Point(245, 129);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(115, 129);
this.label7.TabIndex = 6;
this.label7.Text = "c";
this.label7.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label7.Click += new System.EventHandler(this.label_Click);
//
// label6
//

this.label6.AutoSize = true;
this.label6.Dock = System.Windows.Forms.DockStyle.Fill;
this.label6.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label6.Location = new System.Drawing.Point(124, 129);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(115, 129);
this.label6.TabIndex = 5;
this.label6.Text = "c";
this.label6.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label6.Click += new System.EventHandler(this.label_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Dock = System.Windows.Forms.DockStyle.Fill;
this.label5.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label5.Location = new System.Drawing.Point(3, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(115, 129);
this.label5.TabIndex = 4;
this.label5.Text = "c";
this.label5.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label5.Click += new System.EventHandler(this.label_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Dock = System.Windows.Forms.DockStyle.Fill;
this.label4.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label4.Location = new System.Drawing.Point(366, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(115, 129);
this.label4.TabIndex = 3;
this.label4.Text = "c";
this.label4.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label4.Click += new System.EventHandler(this.label_Click);
//
// label3

//
this.label3.AutoSize = true;
this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
this.label3.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label3.Location = new System.Drawing.Point(245, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(115, 129);
this.label3.TabIndex = 2;
this.label3.Text = "c";
this.label3.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label3.Click += new System.EventHandler(this.label_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
this.label2.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label2.Location = new System.Drawing.Point(124, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(115, 129);
this.label2.TabIndex = 1;
this.label2.Text = "c";
this.label2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label2.Click += new System.EventHandler(this.label_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Webdings", 72F,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(2)));
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(115, 129);
this.label1.TabIndex = 0;
this.label1.Text = "c";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label_Click);
//

// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(484, 517);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private
private
private
private
private
private
private
private
private
private
private
private
private
private
private
private
private
private
}
}

System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
System.Windows.Forms.Label label16;
System.Windows.Forms.Label label15;
System.Windows.Forms.Label label14;
System.Windows.Forms.Label label13;
System.Windows.Forms.Label label12;
System.Windows.Forms.Label label11;
System.Windows.Forms.Label label10;
System.Windows.Forms.Label label9;
System.Windows.Forms.Label label8;
System.Windows.Forms.Label label7;
System.Windows.Forms.Label label6;
System.Windows.Forms.Label label5;
System.Windows.Forms.Label label4;
System.Windows.Forms.Label label3;
System.Windows.Forms.Label label2;
System.Windows.Forms.Label label1;
System.Windows.Forms.Timer timer1;

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MatchingGame
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.cs

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace MatchingGame
{
public partial class Form1 : Form
{
int count = 0;
// Use this Random object to choose random icons for the squares
Random random = new Random();
// firstClicked points to the first Label control
// that the player clicks, but it will be null
// if the player hasn't clicked a label yet
Label firstClicked = null;
// secondClicked points to the second Label control
// that the player clicks
Label secondClicked = null;
// Each of these letters is an interesting icon
// in the Webdings font,
// and each icon appears twice in this list

List<string> icons = new List<string>()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
/// <summary>
/// Assign each icon from the list of icons to a random square
/// </summary>
private void AssignIconsToSquares()
{
// The TableLayoutPanel has 16 labels,
// and the icon list has 16 icons,
// so an icon is pulled at random from the list
// and added to each label
foreach (Control control in tableLayoutPanel1.Controls)
{
// The timer is only on after two non-matching
// icons have been shown to the player,
// so ignore any clicks if the timer is running
if (timer1.Enabled == true)
return;
Label iconLabel = control as Label;
if (iconLabel != null)
{
int randomNumber = random.Next(icons.Count);
iconLabel.Text = icons[randomNumber];
// iconLabel.ForeColor = iconLabel.BackColor
icons.RemoveAt(randomNumber);
}
}
}

public Form1()
{
//AssignIconsToSquares();
InitializeComponent();
AssignIconsToSquares();
}
private void label_Click(object sender, EventArgs e)
{
// The timer is only on after two non-matching
// icons have been shown to the player,

// so ignore any clicks if the timer is running
if (timer1.Enabled == true)
return;
Label clickedLabel = sender as Label;
if (clickedLabel != null)
{
// If the clicked label is black, the player clicked
// an icon that's already been revealed -// ignore the click
if (clickedLabel.ForeColor == Color.Black)
return;

// If firstClicked is null, this is the first icon
// in the pair that the player clicked,
// so set firstClicked to the label that the player
// clicked, change its color to black, and return
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black;
return;
}
// If the player gets this far, the timer isn't
// running and firstClicked isn't null,
// so this must be the second icon the player clicked
// Set its color to black
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
// Check to see if the player won
//CheckForWinner();
//
//
//
//
//
//
//
if
{

If the player gets this far, the player
clicked two different icons, so start the
timer (which will wait three quarters of
a second, and then hide the icons)
If the player clicked two matching icons, keep them
black and reset firstClicked and secondClicked
so the player can click another icon
(firstClicked.Text != secondClicked.Text)

firstClicked = null;
secondClicked = null;
//timer1.Start();
MessageBox.Show("first and second not match");
return;
}
timer1.Start();
CheckForWinner();
}
}
/// <summary>
/// This timer is started when the player clicks
/// two icons that don't match,
/// so it counts three quarters of a second
/// and then turns itself off and hides both icons
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the timer
timer1.Stop();
// Hide both icons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}

/// <summary>
/// Check every icon to see if it is matched, by
/// comparing its foreground color to its background color.
/// If all of the icons are matched, the player wins
/// </summary>
private void CheckForWinner()
{
//int count=0;
// Go through all of the labels in the TableLayoutPanel,
// checking each one to see if its icon is matched
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)

{
// MessageBox.Show("iconlabel isnot null");
if (iconLabel.ForeColor != iconLabel.BackColor)
{
//MessageBox.Show("return statement");
count++;
// MessageBox.Show("count",count.ToString());
//return;
break;
}
}
}
// If the loop didn’t return, it didn't find
// any unmatched icons
// That means the user won. Show a message and close the form
if (count == 8)
{
MessageBox.Show("You matched all the icons!", "Congratulations");
Close();
}
}
}
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close