2013-11-03 53 views
0

我在C#中製作遊戲時出現問題# 它是這樣的:我做了一個picturebox,彈出計時器,然後我想要做的事情是當我點擊它時,標籤會去「Points: 「到」分數:1「,但它會像」分數:「到」分數:162「一樣。用計時器C進行計數器點擊#

我認爲這是因爲區間,我無法弄清楚如何解決。

- 我們點擊圖片 -points加1 * 沒有完成尚未 - 圖像(圖片框)desapear -add另一個圖像(圖片框)隨機

我想有一個點,但櫃檯隨着計時器的使用,就是這樣。 任何幫助將被折衷。

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

+1

您需要發佈的代碼Timer對象 –

+0

僅訂閱Click事件處理程序*一次*。沒有一遍又一遍的每個油漆,這讓事件處理程序運行多次。而不是在窗體構造函數中。也不要使用Paint來移動控件,而是在Tick事件處理程序中設置Location屬性。不再需要自定義繪圖事件處理程序。 –

+0

它正在工作!非常感謝你! :DD –

回答

0

你似乎訂閱點擊事件,每次Form1_Paint被稱爲

pictureBox1.Click += pictureBox1_Click; 

嘗試將它移動到構造函數或初始化方法..

+0

我將該行移動到Form1_Load和pictureBox1.Location = new Point(x,y);到timer1_Tick,它的工作!謝謝! –

0

代碼是不完整的。請指定從哪個甚至是,您正在觸發導致調用「timer1_Tick」方法的Timer事件。

看問題和代碼,我認爲每個計時器「單擊」導致重新繪製窗口。更好的方法是僅在點擊圖片時重新繪製窗口。此外,似乎在每個Time_Click的相同順序中,您還調用Picture_Click(代碼不在發佈代碼中,但根據行爲進行猜測),從而增加點數。所以,一旦這個結果有可能有162個蜱,這就是爲什麼你看到162點。