我在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:我是葡萄牙人,對不起我的英語
您需要發佈的代碼Timer對象 –
僅訂閱Click事件處理程序*一次*。沒有一遍又一遍的每個油漆,這讓事件處理程序運行多次。而不是在窗體構造函數中。也不要使用Paint來移動控件,而是在Tick事件處理程序中設置Location屬性。不再需要自定義繪圖事件處理程序。 –
它正在工作!非常感謝你! :DD –