用C#构建打字游戏

时间:2017-01-18 10:34:50

标签: c# winforms visual-studio-2015

几天前我开始阅读Head#C#,我使用visual c#2015来构建代码和学习C#但是这本书是基于visual studio 2010的,到目前为止我从未遇到任何问题,直到我遇到这个练习当我必须构建一个打字游戏时,我按照书中提到的所有程序进行了操作并且没有错误地构建它。但最后当我运行代码时,键盘上的按键应初始化游戏,但似乎没有任何东西可以启动游戏,甚至没有鼠标点击甚至是虚拟键盘。

以下是代码

namespace WindowsFormsApplication15{
public partial class Form1 : Form
{
    Random random = new Random();
    Stats stats = new Stats();
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Add((Keys)random.Next(65, 70));
        if (listBox1.Items.Count > 7)
        {
            listBox1.Items.Clear();
            listBox1.Items.Add("Game over");
            timer1.Stop();
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if  (listBox1.Items.Contains(e.KeyCode))
        {
            listBox1.Items.Remove(e.KeyCode);
            listBox1.Refresh();
            if (timer1.Interval > 400)
                timer1.Interval -= 10;
            if (timer1.Interval > 250)
                timer1.Interval -= 7;
            if (timer1.Interval > 100)
                timer1.Interval -= 2;
            difficultyProgressBar.Value = 800 - timer1.Interval;

            stats.Update(true);           
        }
        else
        {
            stats.Update(false);
        }
        correctLabel.Text = "Correct:" + stats.Correct;
        missedLabel.Text = "Missed:" + stats.Missed;
        totalLabel.Text = "Total:" + stats.Total;
        accuracyLabel.Text = "Accuracy:" + stats.Accuracy + "%";
    }
}

}

代码类

namespace WindowsFormsApplication15{
class Stats
{
    public int Total = 0;
    public int Missed = 0;
    public int Correct = 0;
    public int Accuracy = 0;

    public void Update(bool correctKey)
    {
        Total++;
        if (!correctKey)
        {
            Missed++;
        }
        else
        {
            Correct++;
        }
        Accuracy = 100 * Correct / (Missed + Correct);
    }
}

}

表单设计器代码

namespace WindowsFormsApplication15{
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.listBox1 = new System.Windows.Forms.ListBox();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.statusStrip1 = new System.Windows.Forms.StatusStrip();
        this.correctLabel = new System.Windows.Forms.ToolStripStatusLabel();
        this.missedLabel = new System.Windows.Forms.ToolStripStatusLabel();
        this.totalLabel = new System.Windows.Forms.ToolStripStatusLabel();
        this.accuracyLabel = new System.Windows.Forms.ToolStripStatusLabel();
        this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
        this.difficultyProgressBar = new System.Windows.Forms.ToolStripProgressBar();
        this.statusStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 80.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.listBox1.FormattingEnabled = true;
        this.listBox1.ItemHeight = 120;
        this.listBox1.Location = new System.Drawing.Point(0, 0);
        this.listBox1.MultiColumn = true;
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(887, 261);
        this.listBox1.TabIndex = 0;
        // 
        // timer1
        // 
        this.timer1.Interval = 800;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // statusStrip1
        // 
        this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.correctLabel,
        this.missedLabel,
        this.totalLabel,
        this.accuracyLabel,
        this.toolStripStatusLabel1,
        this.difficultyProgressBar});
        this.statusStrip1.Location = new System.Drawing.Point(0, 239);
        this.statusStrip1.Name = "statusStrip1";
        this.statusStrip1.Size = new System.Drawing.Size(887, 22);
        this.statusStrip1.SizingGrip = false;
        this.statusStrip1.TabIndex = 1;
        this.statusStrip1.Text = "statusStrip1";
        // 
        // correctLabel
        // 
        this.correctLabel.Name = "correctLabel";
        this.correctLabel.Size = new System.Drawing.Size(58, 17);
        this.correctLabel.Text = "Correct: 0";
        // 
        // missedLabel
        // 
        this.missedLabel.Name = "missedLabel";
        this.missedLabel.Size = new System.Drawing.Size(56, 17);
        this.missedLabel.Text = "Missed: 0";
        // 
        // totalLabel
        // 
        this.totalLabel.Name = "totalLabel";
        this.totalLabel.Size = new System.Drawing.Size(45, 17);
        this.totalLabel.Text = "Total: 0";
        // 
        // accuracyLabel
        // 
        this.accuracyLabel.Name = "accuracyLabel";
        this.accuracyLabel.Size = new System.Drawing.Size(78, 17);
        this.accuracyLabel.Text = "Accuracy: 0%";
        // 
        // toolStripStatusLabel1
        // 
        this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
        this.toolStripStatusLabel1.Size = new System.Drawing.Size(533, 17);
        this.toolStripStatusLabel1.Spring = true;
        this.toolStripStatusLabel1.Text = "Difficulty";
        this.toolStripStatusLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // difficultyProgressBar
        // 
        this.difficultyProgressBar.Name = "difficultyProgressBar";
        this.difficultyProgressBar.Size = new System.Drawing.Size(100, 16);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(887, 261);
        this.Controls.Add(this.statusStrip1);
        this.Controls.Add(this.listBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.Text = "Form1";
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        this.statusStrip1.ResumeLayout(false);
        this.statusStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.StatusStrip statusStrip1;
    private System.Windows.Forms.ToolStripStatusLabel correctLabel;
    private System.Windows.Forms.ToolStripStatusLabel missedLabel;
    private System.Windows.Forms.ToolStripStatusLabel totalLabel;
    private System.Windows.Forms.ToolStripStatusLabel accuracyLabel;
    private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
    private System.Windows.Forms.ToolStripProgressBar difficultyProgressBar;
}

}

我刚开始学习C#并在本月与visual studio合作,我对编程知之甚少 我的猜测是问题在于KeyDown事件的某些地方 enter image description here

4 个答案:

答案 0 :(得分:2)

在所有代码中,我都看不到timer1的启动位置 - 尝试添加

timer1.Start();

在InitializeComponent();

之后

答案 1 :(得分:1)

问题很简单 - 你的输入焦点在列表框上。编写样本的人可能没有那么多测试,或者你没有完全按照程序进行测试:)

要确保表单在子控件上接收按键,您需要将表单的KeyPreview属性设置为true

另外,正如PaulF所说,你永远不会启动计时器。最简单的解决方案是将Enabled设置为true。

答案 2 :(得分:0)

我迟到了,但这就是我所得到的。我正在读同一本书,遇到了同样的问题,即说明要求您将列表框字体设置为72pt大小。这会使字母过大而无法在窗体的列表框中正确显示,并且Dock Fill属性(或某些其他属性,我在C#中是新手)导致ListBox(及其内容)在运行时不会显示在窗体上

解决方法是将ListBox1的字体大小减小到下一个大小,即48pt。

答案 3 :(得分:0)

我遇到了同样的问题,必须将计时器设置为启动或启用,并将字体大小设置为48pt。 在Form1.cs中的InitializeComponent()下面添加这些行之一。

this.timer1.Enabled = true; 
this.timer1.Start();