You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在类太空侵略者游戏中实现3分钟倒计时功能?

实现3分钟限时计分机制的解决方案

嘿,看起来你已经把游戏核心玩法搞定了,只差倒计时这一步!咱们用WinForms的Timer控件就能轻松实现3分钟的时间限制,下面一步步来:

步骤1:添加倒计时相关变量和UI元素

首先在你的Form类里加两个关键内容:一个存储剩余秒数(3分钟=180秒),还有一个用来显示倒计时的Label(你可以在设计器里拖个Label到窗体上,命名为lblTimer)。

在类顶部添加变量:

int remainingSeconds = 180; // 3分钟换算成秒数

步骤2:配置倒计时计时器

在设计器里拖一个新的Timer控件(命名为timer3),把它的Interval属性设为1000——这表示每秒触发一次Tick事件。

步骤3:初始化并启动倒计时

在Form的构造函数里初始化倒计时显示并启动计时器:

public Form1()
{
    InitializeComponent();
    UpdateTimerDisplay(); // 初始化显示剩余时间
    timer3.Start(); // 启动倒计时
}

// 辅助方法:格式化并更新倒计时Label的显示
private void UpdateTimerDisplay()
{
    int minutes = remainingSeconds / 60;
    int seconds = remainingSeconds % 60;
    lblTimer.Text = $"剩余时间:{minutes:D2}:{seconds:D2}"; // 显示成MM:SS格式
}

步骤4:编写倒计时的核心逻辑

双击timer3进入Tick事件,添加以下代码:

private void timer3_Tick(object sender, EventArgs e)
{
    remainingSeconds--;
    UpdateTimerDisplay();

    // 检查时间是否耗尽
    if (remainingSeconds <= 0)
    {
        // 停止所有游戏相关计时器
        timer1.Stop();
        timer2.Stop();
        timer3.Stop();

        // 弹出最终得分提示
        MessageBox.Show($"时间到!你的最终得分是:{score}", "游戏结束");
    }
}

步骤5:处理中途失败的情况

别忘了在你现有“撞到外星人失败”的代码里,也停止倒计时计时器,避免时间继续走:

if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
{
    timer2.Enabled = false;
    timer3.Stop(); // 新增:失败时停止倒计时
    MessageBox.Show("You Lose!");
    return;
}

完整修改后的代码

把所有改动整合到你的代码里,最终版本如下:

public partial class Form1 : Form 
{ 
    private List<Invader> invaders = new List<Invader>(); 
    private List<Laser> lasers = new List<Laser>(); 
    int invaderNumber = 0; 
    int score = 0; 
    int remainingSeconds = 180; // 新增:3分钟倒计时变量

    public Form1() 
    { 
        InitializeComponent(); 
        UpdateTimerDisplay();
        timer3.Start(); // 新增:启动倒计时计时器
    } 

    // 新增:更新倒计时显示的方法
    private void UpdateTimerDisplay()
    {
        int minutes = remainingSeconds / 60;
        int seconds = remainingSeconds % 60;
        lblTimer.Text = $"剩余时间:{minutes:D2}:{seconds:D2}";
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
        if (e.KeyCode.Equals(Keys.W)) 
        { 
            if (SpaceFighter.Top > 0) 
            { 
                SpaceFighter.Top = SpaceFighter.Top - 30; 
            } 
        } 
        if (e.KeyCode.Equals(Keys.A)) 
        { 
            if (SpaceFighter.Left > 0) 
            { 
                SpaceFighter.Left = SpaceFighter.Left - 10; 
            } 
        } 
        if (e.KeyCode.Equals(Keys.D)) 
        { 
            if (SpaceFighter.Right < this.Width) 
            { 
                SpaceFighter.Left = SpaceFighter.Left + 10; 
            } 
        } 
        if (e.KeyCode.Equals(Keys.S)) 
        { 
            if (SpaceFighter.Bottom < this.Height - 10) 
            { 
                SpaceFighter.Top = SpaceFighter.Top + 10; 
            } 
        } 
        if (e.KeyCode.Equals(Keys.Space)) 
        { 
            this.lasers.Add(new Laser(this, SpaceFighter)); 
        } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
        if (invaderNumber > 4) 
        { 
            timer1.Enabled = false; 
            timer2.Enabled = true; 
        } 
        else 
        { 
            invaders.Add(new Invader(this)); 
            invaderNumber++; 
        } 
    } 

    private void timer2_Tick(object sender, EventArgs e) 
    { 
        invaders.RemoveAll(ship => ship.isDisposed); 
        foreach(Invader ship in invaders) 
        { 
            ship.MoveInvader(this); 
            if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds)) 
            { 
                timer2.Enabled = false; 
                timer3.Stop(); // 新增:失败时停止倒计时
                MessageBox.Show("You Lose!"); 
                return; 
            } 
        } 
        lasers.RemoveAll(laser => laser.isDisposed); 
        foreach (Laser laser in lasers) 
        { 
            laser.MoveLaser(this); 
            foreach (Invader ship in invaders) 
            { 
                if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds)) 
                { 
                    laser.isDisposed = true; 
                    laser.laser.Dispose(); 
                    ship.isDisposed = true; 
                    ship.ship.Dispose(); 
                    score = score + 2; 
                    lblScore.Text = score.ToString(); 
                } 
            } 
        } 
    }

    // 新增:倒计时计时器的Tick事件
    private void timer3_Tick(object sender, EventArgs e)
    {
        remainingSeconds--;
        UpdateTimerDisplay();

        if (remainingSeconds <= 0)
        {
            timer1.Stop();
            timer2.Stop();
            timer3.Stop();
            MessageBox.Show($"时间到!你的最终得分是:{score}", "游戏结束");
        }
    }
}

这样游戏启动后就会开始3分钟倒计时,时间到了自动停止所有游戏逻辑并弹出最终得分;如果中途撞到外星人失败,倒计时也会同步停止,不会继续走啦。

内容的提问来源于stack exchange,提问作者adrian rowland

火山引擎 最新活动