0

我在用 C# 制作游戏时遇到问题,它是这样的:我制作了一个用计时器弹跳的图片框,然后我想做的是,当我点击它时,标签会从“Points:”变为“Points:1”,但它就像“积分:”到“积分:162”。

我认为这是因为间隔,我无法弄清楚如何解决。

- 我们点击图片 - 点添加 1 *尚未完成 - 图像(图片框)删除 - 随机添加另一张图片(图片框)

我想要一个点数计数器,但使用计时器,就是这样。任何帮助将不胜感激。

 int dx;
    int dy;
    int x;
    int y;
    int pts = 0;

    private void Form1_Load(object sender, EventArgs e)
    {
        Random rnd = new Random();
        dx = rnd.Next(2, 5);
        dy = rnd.Next(2, 5);
        x = rnd.Next(0, this.ClientSize.Width - 1 );
        y = rnd.Next(0, this.ClientSize.Height - 1);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        pictureBox1.Location = new Point(x, y);
        pictureBox1.Click += pictureBox1_Click;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        x += dx;
        if (x < 0)
        {
            dx = -dx;
        }
        else if (x + 50 > this.ClientSize.Width)
        {
            dx = -dx;
        }

        y += dy;
        if (y < 100)
        {
            dy = -dy;
        }
        else if (y + 50 > this.ClientSize.Height)
        {
            dy = -dy;
        }
        this.Invalidate();
    } 

    void pictureBox1_Click(object sender, EventArgs e)
    {  
       pts++;
       label1.Text = "Pontos: " + pts;

       pictureBox1.Location = new Point(x,y); 
    }



 this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.panel1 = new System.Windows.Forms.Panel();
        this.label1 = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Interval = 10;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // pictureBox1
        // 
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.Location = new System.Drawing.Point(146, 243);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(50, 50);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        // 
        // panel1
        // 
        this.panel1.BackColor = System.Drawing.Color.Teal;
        this.panel1.Controls.Add(this.label1);
        this.panel1.Cursor = System.Windows.Forms.Cursors.Arrow;
        this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
        this.panel1.Location = new System.Drawing.Point(0, 0);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(534, 100);
        this.panel1.TabIndex = 1;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label1.ForeColor = System.Drawing.SystemColors.ButtonFace;
        this.label1.Location = new System.Drawing.Point(26, 27);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(144, 39);
        this.label1.TabIndex = 0;
        this.label1.Text = "Pontos: ";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.Control;
        this.ClientSize = new System.Drawing.Size(534, 562);
        this.Controls.Add(this.panel1);
        this.Controls.Add(this.pictureBox1);
        this.Cursor = System.Windows.Forms.Cursors.Cross;
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.panel1.ResumeLayout(false);
        this.panel1.PerformLayout();
        this.ResumeLayout(false);

ps:我是葡萄牙人,对不起我的英语

4

2 回答 2

0

代码不完整。请指定您从哪个事件触发 Timer 事件,这会导致调用“timer1_Tick”方法。

查看问题和代码,我认为每个计时器“单击”都会导致窗口重绘。更好的方法是仅在单击图片时重新绘制窗口。此外,似乎在每个 Time_Click 的相同顺序中,您也在调用 Picture_Click(代码不在发布的代码中......但根据行为猜测)这会导致积分增加。所以,一旦结束,可能会有 162 个滴答声,这就是为什么你会看到 162 个点。

于 2013-11-03T13:53:37.143 回答
0

您似乎每次都在订阅点击事件 Form1_Paint被调用

 pictureBox1.Click += pictureBox1_Click;

尝试将其移至构造函数或初始化方法..

于 2013-11-03T14:00:33.150 回答