![](/img/trans.png)
[英]how can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?
[英]Is there a way and how can i make the program increment the obstacleSpeed by one every time the score increments by one?
有沒有辦法,我怎樣才能讓程序在每次分數增加 1 時將障礙速度增加 1? (包含所有 if 語句的部分)。 我認為它一定是某種 for 循環,但也許我都錯了。 希望有人可以提供幫助。
我是編碼新手,所以如果你能像孩子一樣解釋它會很棒
我的程序:
namespace Arnold
{
public partial class Form1 : Form
{
bool jumping = false;
int jumpSpeed;
int force = 15;
int score = 0;
int obstacleSpeed = 15;
Random rand = new Random();
int posisition;
bool isGameOver = false;
public Form1()
{
InitializeComponent();
GameReset();
}
private void mainGameTimerEvent(object sender, EventArgs e)
{
player.Top += jumpSpeed;
txtScore.Text = "score: " + score;
if (jumping == true && force < 0)
{
jumping = false;
}
if (jumping == true)
{
jumpSpeed = -25;
force -= 1;
}
else
{
jumpSpeed = 25;
}
if (player.Top > 364 && jumping == false)
{
force = 15;
player.Top = 365;
jumpSpeed = 0;
}
foreach (Control x in this.Controls)
{
if (x is PictureBox && (string)x.Tag == "Heli")
{
x.Left -= obstacleSpeed;
if (x.Left < -100)
{
x.Left = this.ClientSize.Width + rand.Next(200, 500) + (x.Width * 15);
}
if (player.Bounds.IntersectsWith(x.Bounds))
{
gameTimer.Stop();
player.Image = Properties.Resources.Explosion_2_0;
txtScore.Text += " Press R to restart the game! Or press Q to exit!";
isGameOver = true;
}
}
if (x is PictureBox && (string)x.Tag == "obstacle")
{
x.Left -= obstacleSpeed;
if (x.Left < -100)
{
x.Left = this.ClientSize.Width + rand.Next(200, 500) + (x.Width * 15);
score++;
}
if (player.Bounds.IntersectsWith(x.Bounds))
{
gameTimer.Stop();
player.Image = Properties.Resources.Explosion_2_0;
txtScore.Text += " Press R to restart the game! Or press Q to exit!";
isGameOver = true;
}
}
}
if (score > 5)
{
obstacleSpeed = 20;
}
if(score > 10)
{
obstacleSpeed = 25;
}
if (score > 15)
{
obstacleSpeed = 30;
}
if (score > 20)
{
obstacleSpeed = 35;
}
if (score > 25)
{
obstacleSpeed = 40;
}
if (score > 30)
{
obstacleSpeed = 45;
}
if (score > 35)
{
obstacleSpeed = 50;
}
if (score > 40)
{
obstacleSpeed = 55;
}
if (score > 45)
{
obstacleSpeed = 60;
}
if (score > 50)
{
obstacleSpeed = 65;
}
if (score > 55)
{
obstacleSpeed = 70;
}
if (score > 60)
{
obstacleSpeed = 75;
}
if (score > 65)
{
obstacleSpeed = 80;
}
if (score > 70)
{
obstacleSpeed = 85;
}
if (score > 75)
{
obstacleSpeed = 90;
}
}
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space && jumping == false)
{
jumping = true;
}
}
private void keyisup(object sender, KeyEventArgs e)
{
if (jumping == true)
{
jumping = false;
}
if (e.KeyCode == Keys.R && isGameOver == true)
{
GameReset();
}
if (e.KeyCode == Keys.Q && isGameOver == true)
{
txtScore.Text = "Are you sure you want to exit the program? (Y/N)";
}
if (e.KeyCode == Keys.Y)
{
Environment.Exit(0);
}
if (e.KeyCode == Keys.N)
{
GameReset();
}
}
private void GameReset()
{
force = 15;
jumpSpeed = 0;
jumping = false;
score = 0;
obstacleSpeed = 15;
txtScore.Text = "Score" + score;
player.Image = Properties.Resources.figur_færdig;
isGameOver = false;
player.Top = 365;
foreach (Control x in this.Controls)
{
if (x is PictureBox && (string)x.Tag == "obstacle")
{
posisition = this.ClientSize.Width + rand.Next(500, 800) + (x.Width * 10);
x.Left = posisition;
}
}
gameTimer.Start();
}
}
}
它告訴我要添加更多詳細信息,因此下一部分只是垃圾郵件。 對不起。
這是實現這一點的方法。 它只使用創建一個引用我命名為OnScoreChanged()
的方法的get-set property
。
int _score;
int speed;
private int score
{
get
{
return _score;
}
set
{
_score = value;
OnScoreChanged();
}
}
並且您的 OnScoreChanged() 方法可以支持速度的變化。
private void OnScoreChanged()
{
speed++;
}
請注意,只要您更改score
的值,就會調用此事件。 為了避免在score
下降時提高speed
,您可以執行以下操作:
private int score
{
get
{
return _score;
}
set
{
if(_score < value) // When the value of score increases
{
OnScoreIncreased();
}
else if(_score > value) // When the value of score decreases
{
OnScoreDecreased();
}
else
{
_score = value;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.