Original Post
Hi, i'm doing a simple game with C#, but i have a problem. To make things clear, y - points , x - time. The problem is that if answer is correct you can keep pressing check answer and it will give you points. How could i avoid that? Here's the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MathGame
{
public partial class Form1 : Form
{
Random randomNo = new Random();
int no1;
int no2;
string problemType = "+";
Random randomNumber = new Random();
public Form1()
{
InitializeComponent();
Answer.KeyDown += new KeyEventHandler(Answer_KeyDown);
Answer.KeyDown += new KeyEventHandler(NextQuestion_KeyDown);
}
private void atsitiktinisRadimas()
{
darkart:
int tempNumber = randomNumber.Next(4);
if (tempNumber == 0) { goto darkart; }
if (tempNumber == 1)
{
radioButton1.Checked = true;
radioButton2.Checked = false;
radioButton3.Checked = false;
}
if (tempNumber == 2)
{
radioButton1.Checked = false;
radioButton2.Checked = true;
radioButton3.Checked = false;
}
if (tempNumber == 3)
{
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = true;
}
}
private void StartGame_Click(object sender, EventArgs e)
{
Clock = new Timer();
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
StartGame.Visible = false;
CheckAnswer.Visible = true;
Question.Visible = true;
Answer.Visible = true;
NextQuestion.Visible = true;
Feedback.Text = "";
Feedback.Visible = true;
label1.Visible = true;
Gen_Question();
}
private void Gen_Question()
{
no1 = randomNo.Next(100);
no2 = randomNo.Next(100);
if (radioButton1.Checked)
{
problemType = "+";
}
else if (radioButton2.Checked)
{
problemType = "-";
}
else if (radioButton3.Checked)
{
problemType = "*";
}
Question.Text = no1 + problemType + no2;
}
int y = 0;
private void CheckAnswer_Click(object sender, EventArgs e)
{
int result = 0;
if (problemType == "+")
{
result = no1 + no2;
}
else if (problemType == "-")
{
result = no1 - no2;
}
else if (problemType == "*")
{
result = no1 * no2;
}
if (Answer.Text == result.ToString())
{
Feedback.Text = "Correct";
}
else
{
Feedback.Text = "Incorrect";
}
if (Feedback.Text == "Correct")
{
y = y + 50;
if (15 > x)
if (x > 1)
{
y = y + 25 - 50;
}
}
this.label4.Text = "" + y;
}
private void NextQuestion_Click(object sender, EventArgs e)
{
x = 30;
Clock.Start();
this.label1.Text = "" + x;
atsitiktinisRadimas();
Gen_Question();
Answer.Text = "";
Feedback.Text = "";
}
int x = 30;
private void Timer_Tick(object sender, EventArgs e)
{
if (x == 1) { Clock.Stop(); }
x = x - 1;
this.label1.Text = "" + x;
if (x == 0) { stop(); }
}
private void stop()
{
this.Question.Text = "Time's Up";
}
private void Answer_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int result = 0;
if (problemType == "+")
{
result = no1 + no2;
}
else if (problemType == "-")
{
result = no1 - no2;
}
else if (problemType == "*")
{
result = no1 * no2;
}
if (Answer.Text == result.ToString())
{
Feedback.Text = "Correct";
}
else
{
Feedback.Text = "Incorrect";
}
if (Feedback.Text == "Correct")
{
y = y + 50;
if (15 > x)
if (x > 1)
{
y = y + 25 - 50;
}
}
this.label4.Text = "" + y;
}
}
private void NextQuestion_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
x = 30;
Clock.Start();
atsitiktinisRadimas();
this.label1.Text = "" + x;
Gen_Question();
Answer.Text = "";
Feedback.Text = "";
}
}
}
}