繁体   English   中英

用 C# 构建打字游戏

[英]Building a typing game in C#

几天前,我开始阅读 Head first 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 事件的某个地方在此处输入图片说明

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

timer1.Start();

在 InitializeComponent() 之后;

问题很简单 - 您的输入焦点在列表框上。 编写示例的人可能没有对其进行太多测试,或者您没有足够精确地遵循该程序:)

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM