现在更新了代码,只剩下两个问题,直到我很高兴认为我已经完成它。
当玩家击中墙壁时,计数器会在每次击中时下降2次。
当游戏开始时,我发出的声音(beep.wav)应该只在玩家撞墙时发出声音,每次开始游戏时都会发出声音,而整个游戏中应播放的声音(onestop.wav)不是一点都不玩。
public partial class Form1 : Form
{
// This SoundPlayer plays a song when the game begins.
System.Media.SoundPlayer onestop = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\onestop.wav");
// This SoundPlayer plays a sound whenever the player hits a wall.
System.Media.SoundPlayer beepSound = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\beep.wav");
// This SoundPlayer plays a sound when the player finishes the game.
System.Media.SoundPlayer clapSound = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\winningApplause.wav");
public Form1()
{
InitializeComponent();
timer1.Interval = (1000) * (1);
timer1.Enabled = true;
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
begin();
}
private void begin()
{
onestop.Play();
livesTextBox.Text = lives.ToString();
Point startingPoint = panel1.Location;
startingPoint.Offset(10, 10);
Cursor.Position = PointToScreen(startingPoint);
}
int lives = 5;
private void MoveToStart()
{
if ( lives > 0)
{
lives--;
}
if (lives == 0)
{
MessageBox.Show("You Lose!!");
Close();
}
else if (lives < 0)
{
MessageBox.Show("You Lose!!");
Close();
}
}
private void wall_MouseEnter(object sender, EventArgs e)
{
// When the mouse pointer hits a wall or enters the panel,
// call the MoveToStart() method.
beepSound.Play();
MoveToStart();
lives--;
livesTextBox.Text = lives.ToString();
}
private void finishLabel_MouseEnter(object sender, EventArgs e)
{
// Play a sound, show a congratulatory MessageBox, then close the form.
clapSound.Play();
MessageBox.Show("Congratulations! You've beaten the maze!");
Close();
}
int gameElapsed = 0;
private void timer1_Tick(object sender, EventArgs e)
{
gameElapsed++;
textBox1.Text = "" + gameElapsed.ToString();
}
}
答案 0 :(得分:1)
您没有非常清楚地发布代码,但我会尽力帮助。
不确定您的声音问题。但是:要“打电话给你的计时器”,我猜你想在文本框中输入一个数字?你在中途。 Timer
对象每秒只调用tick事件,因此您需要执行显示部分。
将文本框放在表单上,假设它名为textBox1
。
放在begin()
方法之前:
Stopwatch stopWatch = new Stopwatch();
放入begin()
方法
stopWatch.Start();
在timer1_Tick
方法中:
TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = String.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
对于您的“生命”位,您需要在begin()
方法之前放置
int lives = 0;
在begin()
内,放
int lives = 5;
livesTextBox.Text = lives.toString();
并在wall_MouseEnter()
内,放
lives--;
livesTextBox.Text = lives.toString();
(假设您在表单上绘制了livesTextBox
)
答案 1 :(得分:1)
只需在表单上创建一个间隔为1000的计时器,然后启用它,然后将game_elpased定义为类中私有字段的整数,并在timer_tick中写入:
void Timer1_Tick(object Sender,EventArgs e)
{
game_elapsed++;
textBox1.Text = "Elapsed Seconds : " + game_elapsed.ToString();
}
对于直播时间,当玩家失败时,您需要像生命一样控制公共变量:
if(fails && lives>0){
lives--;
}
else if (lives<0)
{
MessageBox.Show("You are a looser ...");
}
答案 2 :(得分:0)
生命可以是玩家阶层的财产。
public class Player
{
int lives = 5;
public bool Kill()
{
this.lives--;
return this.lives <= 0;
}
public void run()
{
Player player = new Player();
// do stuff
// Check whether the player needs to die
if ("player fails".Contains("fail"))
{
if (player.Kill())
{
// restart level.
}
else
{
// Game over.
}
}
}
}