Unity的C#编程教程_23_if 条件语句挑战3

  • 设计一个程序,用于统计得分
  • 每次按下空格键加十分
  • 分数达到 100 分以上,弹出消息:Great!(限制为仅仅弹出一次)
  • 提示:使用 bool 变量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddPoints : MonoBehaviour
{
    public int points = 0;
    // 设置个变量存储分数
    private bool _getPoint;
    // 表示之前没有达到过,默认为 false

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) // 判断按下空格键
        {
            points += 10; // 加十分
            Debug.Log("Points: " + points); // 显示分数
        }

        if (points >= 100 && !_getPoint) // 分数达到 100分 并且之前没有达到过
        {
            _getPoint = true; // 把状态切换到:已经到达过
            Debug.Log("Great!"); // 弹出该消息
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值