You are on page 1of 13

simple Math Quiz Game

in which players must give answer within 30 seconds.This game may not be suitable
to play for entertainment but this is very helpful for reading and creating project in
windows form.
In this tutorial, I build a timed math quiz game, where the player must answer four
random arithmetic problems within a specified time. You learn how to:

Generate random numbers using the Random class.

Trigger events using a Timer control.

Control program flow using if else statements.

Perform basic arithmetic operations.


In game when player fill all four answer within specific time the message box
display's congratulation message if not then display's sorry message.

Here is a source code written in c sharp copy each block of code and save them in
separate file and create new project on windows form application and compile it.

|--------------------------------------------------------------------------|
|
Program.cs
|
|
|
|--------------------------------------------------------------------------|

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

namespace Math_Quiz_Game
{
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 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 Math_Quiz_Game
{
public partial class Form1 : Form
{
// Create a Random object to generate random numbers.
Random randomizer = new Random();

// These ints will store the numbers


// for the addition problem.
int addend1;
int addend2;

// These ints will store the numbers


// for the subtraction problem.
int minuend;
int subtrahend;

// These ints will store the numbers for the multiplication problem.
int multiplicand;
int multiplier;

// These ints will store the numbers for the division problem.
int dividend;
int divisor;

// This int will keep track of the time left.


int timeLeft;

/// <summary>
/// This start the quiz filling all the problems
/// and start the timer
/// </summary>
public void StartTheQuiz()
{
addend1 = randomizer.Next(51);
addend2 = randomizer.Next(51);
LeftPluslabel.Text = addend1.ToString();
RightPluslabel.Text = addend2.ToString();
sum.Value = 0;

// Fill in the subtraction problem.


minuend = randomizer.Next(1, 101);
subtrahend = randomizer.Next(1, minuend);
LeftMinuslabel.Text = minuend.ToString();
RightMinuslabel.Text = subtrahend.ToString();
difference.Value = 0;

// Fill in the multiplication problem.


multiplicand = randomizer.Next(2, 11);
multiplier = randomizer.Next(2, 11);
LeftMultilabel.Text = multiplicand.ToString();
RightMultilabel.Text = multiplier.ToString();
product.Value = 0;

// Fill in the division problem.


divisor = randomizer.Next(2, 11);
int temporaryQuotient = randomizer.Next(2, 11);
dividend = divisor * temporaryQuotient;
LeftDividelabel.Text = dividend.ToString();
RightDividelable.Text = divisor.ToString();
quotient.Value = 0;

// Start the timer.


timeLeft = 30;
Timelable.Text = "30 seconds";
timer1.Start();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

/// <summary>
/// Check the answer to see if the user got everything right.
/// </summary>
/// <returns>True if the answer's correct, false otherwise.</returns>
private bool CheckTheAnswer()
{
if ((addend1 + addend2 == sum.Value)
&& (minuend - subtrahend == difference.Value)
&& (multiplicand * multiplier == product.Value)
&& (dividend / divisor == quotient.Value))
return true;
else
return false;

private void timer1_Tick(object sender, EventArgs e)


{
if (CheckTheAnswer())
{
// If the user got the answer right, stop the timer
// and show a MessageBox.
timer1.Stop();
MessageBox.Show("You got all the answers right!",
"Congratulations");
StartGameButton.Enabled = true;
}
else if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
Timelable.Text = timeLeft + " seconds";

}
else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
timer1.Stop();
Timelable.Text = "Time's up!";
MessageBox.Show("You didn't finish in time.", "Sorry");
sum.Value = addend1 + addend2;
difference.Value = minuend - subtrahend;
product.Value = multiplicand * multiplier;
quotient.Value = dividend / divisor;
StartGameButton.Enabled = true;
}
}

private void answer_Enter(object sender, EventArgs e)


{
// Select the whole answer in the NumericUpDown control.
NumericUpDown answerBox = sender as NumericUpDown;

if (answerBox != null)
{
int lengthOfAnswer = answerBox.Value.ToString().Length;
answerBox.Select(0, lengthOfAnswer);
}
}

private void StartGameButton_Click_1(object sender, EventArgs e)


{
StartTheQuiz();
StartGameButton.Enabled = false;
}
}
}

|--------------------------------------------------------------------------|
|
Form.Designer.cs
|
|
|
|--------------------------------------------------------------------------|
namespace Math_Quiz_Game
{
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.Timelabel = new System.Windows.Forms.Label();
this.LeftPluslabel = new System.Windows.Forms.Label();
this.Pluslabel = new System.Windows.Forms.Label();
this.RightPluslabel = new System.Windows.Forms.Label();
this.Equal_label1 = new System.Windows.Forms.Label();
this.LeftMinuslabel = new System.Windows.Forms.Label();
this.Minuslabel = new System.Windows.Forms.Label();
this.RightMinuslabel = new System.Windows.Forms.Label();
this.Equal_lable2 = new System.Windows.Forms.Label();
this.LeftMultilabel = new System.Windows.Forms.Label();
this.Multilabel = new System.Windows.Forms.Label();
this.RightMultilabel = new System.Windows.Forms.Label();
this.Equal_lable3 = new System.Windows.Forms.Label();
this.LeftDividelabel = new System.Windows.Forms.Label();
this.Dividelabel = new System.Windows.Forms.Label();
this.RightDividelable = new System.Windows.Forms.Label();
this.Equal_lable4 = new System.Windows.Forms.Label();
this.sum = new System.Windows.Forms.NumericUpDown();
this.difference = new System.Windows.Forms.NumericUpDown();
this.product = new System.Windows.Forms.NumericUpDown();
this.quotient = new System.Windows.Forms.NumericUpDown();

this.StartGameButton = new System.Windows.Forms.Button();


this.timer1 = new System.Windows.Forms.Timer(this.components);
this.Timelable = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.sum)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.difference)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.product)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.quotient)).BeginInit();
this.SuspendLayout();
//
// Timelabel
//
this.Timelabel.AutoSize = true;
this.Timelabel.Font = new System.Drawing.Font("Century", 12F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Timelabel.Location = new System.Drawing.Point(96, 38);
this.Timelabel.Name = "Timelabel";
this.Timelabel.Size = new System.Drawing.Size(95, 20);
this.Timelabel.TabIndex = 0;
this.Timelabel.Text = "Time Left ";
//
// LeftPluslabel
//
this.LeftPluslabel.AutoSize = true;
this.LeftPluslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LeftPluslabel.Location = new System.Drawing.Point(26, 102);
this.LeftPluslabel.Name = "LeftPluslabel";
this.LeftPluslabel.Size = new System.Drawing.Size(18, 20);
this.LeftPluslabel.TabIndex = 2;
this.LeftPluslabel.Text = "?";
//
// Pluslabel
//
this.Pluslabel.AutoSize = true;
this.Pluslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Pluslabel.Location = new System.Drawing.Point(97, 102);
this.Pluslabel.Name = "Pluslabel";
this.Pluslabel.Size = new System.Drawing.Size(18, 20);
this.Pluslabel.TabIndex = 3;
this.Pluslabel.Text = "+";
//
// RightPluslabel
//
this.RightPluslabel.AutoSize = true;
this.RightPluslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.RightPluslabel.Location = new System.Drawing.Point(141, 102);


this.RightPluslabel.Name = "RightPluslabel";
this.RightPluslabel.Size = new System.Drawing.Size(18, 20);
this.RightPluslabel.TabIndex = 4;
this.RightPluslabel.Text = "?";
//
// Equal_label1
//
this.Equal_label1.AutoSize = true;
this.Equal_label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Equal_label1.Location = new System.Drawing.Point(215, 102);
this.Equal_label1.Name = "Equal_label1";
this.Equal_label1.Size = new System.Drawing.Size(18, 20);
this.Equal_label1.TabIndex = 5;
this.Equal_label1.Text = "=";
//
// LeftMinuslabel
//
this.LeftMinuslabel.AutoSize = true;
this.LeftMinuslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LeftMinuslabel.Location = new System.Drawing.Point(26, 161);
this.LeftMinuslabel.Name = "LeftMinuslabel";
this.LeftMinuslabel.Size = new System.Drawing.Size(18, 20);
this.LeftMinuslabel.TabIndex = 6;
this.LeftMinuslabel.Text = "?";
//
// Minuslabel
//
this.Minuslabel.AutoSize = true;
this.Minuslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Minuslabel.Location = new System.Drawing.Point(97, 161);
this.Minuslabel.Name = "Minuslabel";
this.Minuslabel.Size = new System.Drawing.Size(14, 20);
this.Minuslabel.TabIndex = 7;
this.Minuslabel.Text = "-";
//
// RightMinuslabel
//
this.RightMinuslabel.AutoSize = true;
this.RightMinuslabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.RightMinuslabel.Location = new System.Drawing.Point(141, 161);
this.RightMinuslabel.Name = "RightMinuslabel";
this.RightMinuslabel.Size = new System.Drawing.Size(18, 20);

this.RightMinuslabel.TabIndex = 8;
this.RightMinuslabel.Text = "?";
//
// Equal_lable2
//
this.Equal_lable2.AutoSize = true;
this.Equal_lable2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Equal_lable2.Location = new System.Drawing.Point(215, 159);
this.Equal_lable2.Name = "Equal_lable2";
this.Equal_lable2.Size = new System.Drawing.Size(18, 20);
this.Equal_lable2.TabIndex = 9;
this.Equal_lable2.Text = "=";
//
// LeftMultilabel
//
this.LeftMultilabel.AutoSize = true;
this.LeftMultilabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LeftMultilabel.Location = new System.Drawing.Point(26, 216);
this.LeftMultilabel.Name = "LeftMultilabel";
this.LeftMultilabel.Size = new System.Drawing.Size(18, 20);
this.LeftMultilabel.TabIndex = 10;
this.LeftMultilabel.Text = "?";
//
// Multilabel
//
this.Multilabel.AutoSize = true;
this.Multilabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Multilabel.Location = new System.Drawing.Point(96, 216);
this.Multilabel.Name = "Multilabel";
this.Multilabel.Size = new System.Drawing.Size(15, 20);
this.Multilabel.TabIndex = 11;
this.Multilabel.Text = "*";
//
// RightMultilabel
//
this.RightMultilabel.AutoSize = true;
this.RightMultilabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.RightMultilabel.Location = new System.Drawing.Point(141, 216);
this.RightMultilabel.Name = "RightMultilabel";
this.RightMultilabel.Size = new System.Drawing.Size(18, 20);
this.RightMultilabel.TabIndex = 12;
this.RightMultilabel.Text = "?";
//

// Equal_lable3
//
this.Equal_lable3.AutoSize = true;
this.Equal_lable3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Equal_lable3.Location = new System.Drawing.Point(215, 216);
this.Equal_lable3.Name = "Equal_lable3";
this.Equal_lable3.Size = new System.Drawing.Size(18, 20);
this.Equal_lable3.TabIndex = 13;
this.Equal_lable3.Text = "=";
//
// LeftDividelabel
//
this.LeftDividelabel.AutoSize = true;
this.LeftDividelabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LeftDividelabel.Location = new System.Drawing.Point(26, 278);
this.LeftDividelabel.Name = "LeftDividelabel";
this.LeftDividelabel.Size = new System.Drawing.Size(18, 20);
this.LeftDividelabel.TabIndex = 14;
this.LeftDividelabel.Text = "?";
//
// Dividelabel
//
this.Dividelabel.AutoSize = true;
this.Dividelabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Dividelabel.Location = new System.Drawing.Point(96, 278);
this.Dividelabel.Name = "Dividelabel";
this.Dividelabel.Size = new System.Drawing.Size(13, 20);
this.Dividelabel.TabIndex = 15;
this.Dividelabel.Text = "/";
//
// RightDividelable
//
this.RightDividelable.AutoSize = true;
this.RightDividelable.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.RightDividelable.Location = new System.Drawing.Point(141, 278);
this.RightDividelable.Name = "RightDividelable";
this.RightDividelable.Size = new System.Drawing.Size(18, 20);
this.RightDividelable.TabIndex = 16;
this.RightDividelable.Text = "?";
//
// Equal_lable4
//
this.Equal_lable4.AutoSize = true;

this.Equal_lable4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,


System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Equal_lable4.Location = new System.Drawing.Point(215, 278);
this.Equal_lable4.Name = "Equal_lable4";
this.Equal_lable4.Size = new System.Drawing.Size(18, 20);
this.Equal_lable4.TabIndex = 17;
this.Equal_lable4.Text = "=";
//
// sum
//
this.sum.Location = new System.Drawing.Point(255, 100);
this.sum.Name = "sum";
this.sum.Size = new System.Drawing.Size(91, 20);
this.sum.TabIndex = 18;
this.sum.Enter += new System.EventHandler(this.answer_Enter);
//
// difference
//
this.difference.Location = new System.Drawing.Point(255, 159);
this.difference.Name = "difference";
this.difference.Size = new System.Drawing.Size(91, 20);
this.difference.TabIndex = 19;
this.difference.Enter += new System.EventHandler(this.answer_Enter);
//
// product
//
this.product.Location = new System.Drawing.Point(255, 209);
this.product.Name = "product";
this.product.Size = new System.Drawing.Size(91, 20);
this.product.TabIndex = 20;
this.product.Enter += new System.EventHandler(this.answer_Enter);
//
// quotient
//
this.quotient.Location = new System.Drawing.Point(255, 276);
this.quotient.Name = "quotient";
this.quotient.Size = new System.Drawing.Size(91, 20);
this.quotient.TabIndex = 21;
this.quotient.Enter += new System.EventHandler(this.answer_Enter);
//
// StartGameButton
//
this.StartGameButton.AutoSize = true;
this.StartGameButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.StartGameButton.Location = new System.Drawing.Point(255, 344);
this.StartGameButton.Name = "StartGameButton";

this.StartGameButton.Size = new System.Drawing.Size(102, 34);


this.StartGameButton.TabIndex = 22;
this.StartGameButton.Text = "Start Game";
this.StartGameButton.UseVisualStyleBackColor = true;
this.StartGameButton.Click += new
System.EventHandler(this.StartGameButton_Click_1);
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Timelable
//
this.Timelable.Location = new System.Drawing.Point(214, 43);
this.Timelable.Name = "Timelable";
this.Timelable.Size = new System.Drawing.Size(132, 23);
this.Timelable.TabIndex = 23;
this.Timelable.Text = "30 sec left";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(368, 415);
this.Controls.Add(this.Timelable);
this.Controls.Add(this.StartGameButton);
this.Controls.Add(this.quotient);
this.Controls.Add(this.product);
this.Controls.Add(this.difference);
this.Controls.Add(this.sum);
this.Controls.Add(this.Equal_lable4);
this.Controls.Add(this.RightDividelable);
this.Controls.Add(this.Dividelabel);
this.Controls.Add(this.LeftDividelabel);
this.Controls.Add(this.Equal_lable3);
this.Controls.Add(this.RightMultilabel);
this.Controls.Add(this.Multilabel);
this.Controls.Add(this.LeftMultilabel);
this.Controls.Add(this.Equal_lable2);
this.Controls.Add(this.RightMinuslabel);
this.Controls.Add(this.Minuslabel);
this.Controls.Add(this.LeftMinuslabel);
this.Controls.Add(this.Equal_label1);
this.Controls.Add(this.RightPluslabel);
this.Controls.Add(this.Pluslabel);
this.Controls.Add(this.LeftPluslabel);

this.Controls.Add(this.Timelabel);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.sum)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.difference)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.product)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.quotient)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label Timelabel;
private System.Windows.Forms.Label LeftPluslabel;
private System.Windows.Forms.Label Pluslabel;
private System.Windows.Forms.Label RightPluslabel;
private System.Windows.Forms.Label Equal_label1;
private System.Windows.Forms.Label LeftMinuslabel;
private System.Windows.Forms.Label Minuslabel;
private System.Windows.Forms.Label RightMinuslabel;
private System.Windows.Forms.Label Equal_lable2;
private System.Windows.Forms.Label LeftMultilabel;
private System.Windows.Forms.Label Multilabel;
private System.Windows.Forms.Label RightMultilabel;
private System.Windows.Forms.Label Equal_lable3;
private System.Windows.Forms.Label LeftDividelabel;
private System.Windows.Forms.Label Dividelabel;
private System.Windows.Forms.Label RightDividelable;
private System.Windows.Forms.Label Equal_lable4;
private System.Windows.Forms.NumericUpDown sum;
private System.Windows.Forms.NumericUpDown difference;
private System.Windows.Forms.NumericUpDown product;
private System.Windows.Forms.NumericUpDown quotient;
private System.Windows.Forms.Button StartGameButton;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label Timelable;
}
}

You might also like