计数器点击计时器C#

时间:2013-11-03 13:24:02

标签: c# counter picturebox

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

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

- 我们点击图片 -points加1 * 尚未完成 -image(picturebox)desapear - 随机添加另一张图片(图片框)

我想有一个点计数器,但是使用计时器,就是它。 任何帮助都会被贬低。

 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:我是葡萄牙人,对不起我的英语

2 个答案:

答案 0 :(得分:0)

代码不完整。请指定哪个甚至,你正在触发Timer事件,这导致调用“timer1_Tick”方法。

查看问题和代码,我认为每个Timer“Click”都会导致重绘窗口。更好的方法是仅在Click of Picture上重新绘制窗口。此外,似乎与每个Time_Click在相同的序列中,您也调用Picture_Click(代码不在发布的代码中......但根据行为猜测),这会导致增加点数。所以,一旦这个结束,可能有162个蜱,这就是为什么你看到162分。

答案 1 :(得分:0)

每次调用Form1_Paint

时,您似乎都在订阅点击事件
 pictureBox1.Click += pictureBox1_Click;

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