问题描述
- 打字游戏出现的字母出现重复,怎么解决,星期一就要评审了
-
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;
using System.Collections;namespace project_word_game
{
public partial class Form1 : Form
{
public int mark = 0;//计算用户击中字母个数
public int fault = 0;//计算用户未击中字母个数
public int intoal = 0;//计算产生的字母总数
public Form1()
{
InitializeComponent();
this.Activate();
}private void button1_Click(object sender, EventArgs e) { timer2.Enabled = true; timer1.Enabled = true; } private void Create_lable()//通过自定义label,生成label,使其出现的坐标和颜色随机生成 { this.KeyPreview = true; Random ra = new Random(DateTime.Now.Millisecond); Label lb = new Label(); lb.Location = new Point(ra.Next(10,340),0); lb.Text= ((char)ra.Next(65,90)).ToString(); lb.Font = new Font("宋体", 20); lb.AutoSize = true; lb.Height = 30; lb.Width = 30; int c1 = ra.Next(0, 256); int c2 = ra.Next(0, 256); int c3 = ra.Next(0, 256); lb.BackColor = Color.FromArgb(c1, c2, c3); panel1.Controls.Add(lb); } private void Move_label()//控制label的下降,通过自增top来实现 { //Random ra = new Random(DateTime.Now.Millisecond); foreach (Label l in panel1.Controls) { l.Top += 8; } } private void timer2_Tick(object sender, EventArgs e)//控制label的移动 { Move_label(); foreach (Label l in this.panel1.Controls) { if ((l.Top + l.Height) >= this.panel1.Height && l.Visible==true)//如果用户未击中,并且label已经触及panel底部 { l.Visible = false; fault += 1;//失误数加一 label7.Text = fault.ToString(); if (fault == 20)//游戏的结束点,当用户失误达到20,便提示游戏结束。 { timer2.Enabled = false; timer1.Enabled = false; DialogResult rt= MessageBox.Show("游戏结束,是否重新开始?","提示",MessageBoxButtons.OKCancel); if (rt == DialogResult.Cancel)//通过消息框,如果用户选取消,则结束程序 { MessageBox.Show("谢谢您的使用!", "再见"); this.Close(); } else//如果用户选确定,则表示用户愿意再玩一次游戏,则游戏界面初始化,等待用户点开始 { mark = 0; fault = 0; intoal = 0; label6.Text = "0"; label7.Text = "0"; label11.Text = "0"; foreach (Label la in panel1.Controls) { la.Visible = false; } } } } } } private void button2_Click(object sender, EventArgs e) { timer2.Enabled = false; timer1.Enabled = false; } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { comboBox1.Text = "初级"; } private void Form1_KeyDown(object sender, KeyEventArgs e)//用户输入和字母进行比较 { string str = e.KeyCode.ToString().ToUpper(); foreach (Label l in this.panel1.Controls) { if (l.Text == str) { l.Visible = false; mark += 1; label6.Text = mark.ToString(); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Text == "初级") { timer1.Interval = 1500; timer2.Interval = 400; } if (comboBox1.Text == "中级") { timer1.Interval = 1200; timer2.Interval = 300; } if (comboBox1.Text == "高级") { timer1.Interval = 900; timer2.Interval = 200; } } private void button3_Click(object sender, EventArgs e) { timer1.Enabled = false; timer2.Enabled = false; label6.Text = "0"; label7.Text = "0"; mark = 0; fault = 0; MessageBox.Show("谢谢您的使用!","再见"); this.Close(); } private void timer1_Tick(object sender, EventArgs e)//控制label生产 { Create_lable(); intoal += 1; label11.Text = intoal.ToString(); } private void Form1_MouseEnter(object sender, EventArgs e) { } }
}
解决方案
label6.Text = mark.ToString();后面加上e.Handled = true;
时间: 2024-11-05 16:36:44