Create a Maths quiz game in visual studio using C# V1
- Subject: C# Tutorials
- Learning Time: 3 hours
Follow the codes on this carefully.
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 maths_quiz
{
public partial class Form1 : Form
{
int numA;
int numB;
int totalA;
int randMath;
int check;
public Form1()
{
InitializeComponent();
playGame();
}
private void KeyisDown(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.P || e.KeyChar == 112)
{
check = 1;
checkGame();
}
else if (e.KeyChar == (char)Keys.M || e.KeyChar == 109)
{
check = 2;
checkGame();
}
else if (e.KeyChar == (char)Keys.X || e.KeyChar == 120)
{
check = 3;
checkGame();
}
else if (e.KeyChar == (char)Keys.D || e.KeyChar == 100)
{
check = 4;
checkGame();
}
}
public void playGame()
{
Random ranNumber = new Random();
numA = ranNumber.Next(1, 40);
numB = ranNumber.Next(1, 50);
randMath = ranNumber.Next(1, 5);
symbol.Image = Properties.Resources.qq;
switch (randMath)
{
case 1:
addNum();
break;
case 2:
substractNum();
break;
case 3:
multiplyNum();
break;
case 4:
divideNum();
break;
}
num1.Text = Convert.ToString(numA);
num2.Text = Convert.ToString(numB);
total.Text = Convert.ToString(totalA);
}
public void addNum()
{
totalA = numA + numB;
}
public void substractNum()
{
totalA = numA - numB;
}
public void multiplyNum()
{
totalA = numA * numB;
}
public void divideNum()
{
totalA = numA / numB;
}
public void checkGame()
{
if (check == randMath)
{
switch (randMath)
{
case 1:
symbol.Image = Properties.Resources.plus;
MessageBox.Show("Correct");
playGame();
break;
case 2:
symbol.Image = Properties.Resources.minus;
MessageBox.Show("Correct");
playGame();
break;
case 3:
symbol.Image = Properties.Resources.multi;
MessageBox.Show("Correct");
playGame();
break;
case 4:
symbol.Image = Properties.Resources.divide;
MessageBox.Show("Correct");
playGame();
break;
}
}
else
{
MessageBox.Show("Wrong Answer");
}
}
}
}
Pages: 1 2