现在我正在读一本C#Programming for Absolute Beginner 在第7章,部分训练,它把它。我的问题是 计时器的Interval属性以毫秒为单位,而不是刻度。 现在,我如何根据每个刻度
更改变量(dy)的值修改Lunar Lander游戏的难度级别。有几种方法可以 调整代码。也许你可以通过修改dy中的变化来改变引力 计时器的每个滴答。您还可以调整每个键中dx和dy的变化量 通过修改计时器的间隔来按或时间进度。另一个容易改变 将修改着陆垫或着陆器的大小。
public partial class theForm : Form
{
private double x, y; //will show new position of lander
private double dx, dy; //diffrence in x and y
private int fuel = 100; //how much fuel is left
private int ships = 3; // number of ships player has
private int score = 0; //the player's current score
private int level = 1;
public theForm()
{
InitializeComponent();
initGame();
}
private void timer1_Tick(object sender, EventArgs e)
{
//account for gravity
dy += 0.5;
//increment score for being alive
score += 100;
//show ordinary (no flames) lander
picLander.Image = myPics.Images[0];
//call helper methods to handle details
moveShip();
checkLanding();
showStats();
}//end timer tick
private void showStats()
{
//display the statistics
lblDx.Text = "dx: " + dx;
lblDy.Text = "dy: " + dy;
lblFuel.Text = "fuel: " + fuel;
lblShips.Text = "ships: " + ships;
lblScore.Text = "score: " + score;
}//end showStats
private void checkLanding()
{
//get rectangle from the objects
Rectangle rLander = picLander.Bounds;
Rectangle rPlatform = picPlatform.Bounds;
//look for an interesection
if (rLander.IntersectsWith(rPlatform))
{
//it's either a crash or a landing
//turn off the timer for a moment
timer1.Enabled = false;
if (Math.Abs(dx) < 3)
{
//horizontal speed ok
if (Math.Abs(dy) < 5)
{
//vertical speed ok
if (Math.Abs(rLander.Bottom - rPlatform.Top) < 3)
{
//landing on top of platform
MessageBox.Show("Good Landing!");
lblLevel.Text=Convert.ToString(level++);
fuel += 30;
score += 1000;
}
else
{
//not on top of platform
MessageBox.Show("You have to land on top.");
killShip();
}//end vertical if
}
else
{
//dy too large
MessageBox.Show("Too much vertical velocity!");
killShip();
} // end vertical if
}
else
{
//dx too large
MessageBox.Show("too much horizontal velocity");
killShip();
}//end horiz if
//reset the lander
initGame();
}//end if
}//end checkLanding
private void initGame()
{
//re-initializes the game
Random roller = new Random();
int platX, platY;
//find random dx,dy values for lander
dx = Convert.ToDouble(roller.Next(5) - 2);
dy = Convert.ToDouble(roller.Next(5) - 2);
//place lander randomly on form
x = Convert.ToDouble(roller.Next(this.ClientSize.Width));
y = Convert.ToDouble(roller.Next(this.ClientSize.Height));
//place platform randomly on form (but make sure it appears)
platX = roller.Next(this.ClientSize.Width - picPlatform.Width);
platY = roller.Next(this.ClientSize.Height - picPlatform.Height);
picPlatform.Location = new Point(platX, platY);
//turn on timer
timer1.Enabled = true;
}//end initGame
private void killShip()
{
//kill off a ship,check for end of game
DialogResult answer;
ships--;
if (ships <= 0)
{
//game is over
answer = MessageBox.Show("Play Again?", "Game Over", MessageBoxButtons.YesNo);
if (answer == DialogResult.Yes)
{
ships = 3;
fuel = 100;
initGame();
}
else
{
Application.Exit();
}//end if
}//end 'no ships' if
}//end killShip
private void theForm_KeyDown(object sender, KeyEventArgs e)
{
fuel--;
//check to see if user is out of gas
if (fuel < 0)
{
timer1.Enabled = false;
MessageBox.Show("Out of Fuel!!!");
fuel += 20;
killShip();
initGame();
}//end if
//look for arrow keys
switch (e.KeyData)
{
case Keys.Up:
picLander.Image = myPics.Images[1];
dy -= 2;
break;
case Keys.Left:
picLander.Image = myPics.Images[2];
dx++;
break;
case Keys.Right:
picLander.Image = myPics.Images[3];
dx--;
break;
default:
//do nothing
break;
}//end switch
if(e.KeyCode==Keys.Up && level==2)
{
dy -= 3;
}else if(e.KeyCode == Keys.Left && level== 2)
{
dx += 2;
}else if(e.KeyCode == Keys.Right && level == 2)
{
dx -= 2;
}
}//end keydown
private void moveShip()
{
//change x and check for boundaries
x += dx;
if (x > this.Width - picLander.Width)
{
x = 0;
}
if (x < 0)
{
x = Convert.ToDouble(this.Width - picLander.Width);
}//end if
//change y and check for boundaries
y += dy;
if (y < 0)
{
y = Convert.ToDouble(this.Height - picLander.Height);
}//end if
if (y > this.Height - picLander.Height)
{
y = 0;
}//end if
//move picLander to new location
picLander.Location = new Point(Convert.ToInt32(x), Convert.ToInt32(y));
}//end MoveShip
private void theForm_Load(object sender, EventArgs e)
{
}
}
答案 0 :(得分:0)
您可以使用计时器的“Tick”事件。
public Form1()
{
InitializeComponent();
Timer timer = new Timer();
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
//Your code goes here
}