Godot-游戏显示信息

简介: 本教程介绍如何创建一个游戏主界面和玩家信息显示系统。使用Godot引擎,设置开始场景包含游戏名称、难度选择和主菜单功能,并通过代码实现场景切换与难度设置。接着,创建玩家信息场景,利用SubViewport与UI元素显示对战双方信息、倒计时及当前回合。教程包含完整脚本与节点设置,适合初学者学习游戏界面设计与场景管理。

游戏主界面

创建场景StartScene.tscn作为游戏的开始界面,分别显示游戏名称,游戏难度,开始游戏,设置,退出游戏,点击开始游戏进入游戏主场景,进行游戏。

游戏脚本参考如下:

using Godot;
using Othello;
using System;
public partial class StartScene : Node2D
{
    private OptionButton difficultySelector;
    private Button startButton;
    private Button existButton;
    public override void _Ready()
    {
        difficultySelector = GetNode<OptionButton>("CanvasLayer/Control/CenterContainer/VBoxContainer/OptionButton");
        startButton = GetNode<Button>("CanvasLayer/Control/CenterContainer/VBoxContainer/Start");
        existButton = GetNode<Button>("CanvasLayer/Control/CenterContainer/VBoxContainer/Exist");
        startButton.Pressed += OnStartPressed;
        existButton.Pressed += OnExistPressed;
    }
    private void OnExistPressed()
    {
        GetTree().Quit();
    }
    private void OnStartPressed()
    {
        // 获取选择的难度
        int selected = difficultySelector.Selected;
        int depth = selected switch
        {
            0 => 1, // 简单
            1 => 3, // 普通
            2 => 5, // 困难
            _ => 2,
        };
        // 存入全局变量(推荐用 Autoload)
        GameSettings.AIDepth = depth;
        // 切换到主场景
        GetTree().ChangeSceneToFile("res://Main.tscn");
    }
}

创建游戏玩家信息

创建玩家信息场景PlayerInfo.tscn,使用SubViewPort作为根节点,以便能够嵌套入游戏主场景中。该场景分别显示对手信息,玩家信息,倒计时,当前回合,参考场景设置如下:

在主场景中创建PlayerInfoPanel(MeshInstance3D)用来承载游戏玩家信息显示,创建SubViewport一个实例化场景PlayerInfo。

PlayerInfoPanel→Mesh:PlaneMesh;

Mesh→Material:StandardMaterial3D;

StandardMaterial3D→Resource→Local to Scene 启用;

StandardMaterial3D→Albedo→Texture:ViewportTexture;

ViewportTexture→Viewport Path:PlayerInfo。

在Main.cs中实现游戏信息显示的逻辑,参考代码如下:

private Label opponentNameLabel;
private Label opponentCountLabel;
private Label playerNameLabel;
private Label playerCountLabel;
private Label countdownLabel;
private Label currentPlayerLabel;
private int countdownSeconds = 30;
private Timer countdownTimer;
/// <summary>
/// 初始化玩家信息
/// </summary>
private void InitPlayerInfo()
{
    // 获取 UI 面板节点
    var infoPanel = GetNode<MeshInstance3D>("PlayerInfoPanel");
    var playerInfo = GetNode<SubViewport>("PlayerInfo");
    opponentNameLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelOpponent/VBoxContainerOpponent/OpponentNameLabel");
    opponentCountLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelOpponent/VBoxContainerOpponent/OpponentCountLabel");
    playerNameLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelPlayer/VBoxContainerPlayer/PlayerNameLabel");
    playerCountLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelPlayer/VBoxContainerPlayer/PlayerCountLabel");
    countdownLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelDesc/VBoxContainerDesc/HBoxContainer/CountdownLabel");
    currentPlayerLabel = playerInfo.GetNode<Label>("Control/VBoxContainer/PanelDesc/VBoxContainerDesc/CurrentPlayerLabel");
    countdownTimer = new Timer();
    AddChild(countdownTimer);
    countdownTimer.WaitTime = 1.0f;
    countdownTimer.OneShot = false;
    countdownTimer.Timeout += OnCountdownTimeout;
    countdownTimer.Start();
    UpdatePlayerInfo();
    UpdateCountdownLabel();
}
private void UpdatePlayerInfo()
{
    opponentNameLabel.Text = "对手玩家: AI";
    opponentCountLabel.Text = "黑棋数: 12";
    playerNameLabel.Text = "我方玩家: 玩家";
    playerCountLabel.Text = "白棋数: 10";
    currentPlayerLabel.Text = currentPlayer == Piece.Black ? "黑方回合" : "白方回合";
}
private void UpdateCountdownLabel()
{
    countdownLabel.Text = $"倒计时: {countdownSeconds}s";
}
private void OnCountdownTimeout()
{
    countdownSeconds--;
    if (countdownSeconds <= 0)
    {
        //countdownTimer.Stop();
        countdownLabel.Text = "时间到!";
        // 触发超时逻辑,比如换对方走
        currentPlayer = currentPlayer == Piece.Black ? Piece.White : Piece.Black;
        countdownSeconds = 30;
        UpdatePlayerInfo();
    }
    else
    {
        UpdateCountdownLabel();
    }
}

演示


目录
相关文章
|
6月前
|
人工智能 测试技术 API
5个AI 龙虾🦞员工自动流转!OpenClaw多Agent部署、免费大模型Coding Plan API配置、研发协作与问题排查手册
OpenClaw(原Clawdbot,昵称“小龙虾”)作为2026年主流的开源多智能体框架,其核心优势在于支持多Agent协作——通过创建不同角色的智能体,实现任务自动派发、结果回调与流程闭环。传统研发流程中,一个人需切换需求分析、开发、测试等多个角色,效率低下且易出错;而OpenClaw的多智能体系统可让AI分别扮演这些角色,从需求到代码的完整流程从1-2天缩短至15-30分钟,成本节省80%。
1405 6
|
9月前
|
数据采集 人工智能 搜索推荐
AI 问答占 52%!长沙别墅装修 GEO 突围:30 天引用率暴涨 40%
周有贵,巴黎学院人工智能博士,GGI商学院GEO首席技术专家,专注AI时代数字营销革新。2025年12月1日,长沙著名别墅设计师张主华专程拜访交流,共探GEO技术在装修设计行业中的AI引流逻辑与实操应用。面对生成式AI问答入口占比突破52%的新趋势,传统SEO正被GEO取代——从链接点击到答案呈现,企业需通过构建灯塔内容、E-E-A-T信任链与结构化数据,让品牌信息被AI优先引用。本次对话揭示:未来流量之争,本质是“被AI推荐”的能力之争。
|
前端开发 Java 测试技术
语音app系统软件源码开发搭建新手启蒙篇
在移动互联网时代,语音App已成为生活和工作的重要工具。本文为新手开发者提供语音App系统软件源码开发的启蒙指南,涵盖需求分析、技术选型、界面设计、编码实现、测试部署等关键环节。通过明确需求、选择合适的技术框架、优化用户体验、严格测试及持续维护更新,帮助开发者掌握开发流程,快速搭建功能完善的语音App。
|
前端开发 JavaScript 内存技术
css动画animation绘制向四周扩散的圆圈
css动画animation绘制向四周扩散的圆圈
1787 0
|
数据采集 域名解析 安全
溯源反制技战法
溯源反制技战法
|
监控 Linux KVM
kvm和vmware有什么区别?如何选择?
最终的选择取决于你的组织需求、预算和技术偏好。在做出决策之前,最好进行详细的比较和评估,以确保选择适合你的虚拟化环境。
1888 3
|
前端开发 Linux API
无缝融入,即刻智能[一]:Dify-LLM大模型平台,零编码集成嵌入第三方系统,42K+星标见证专属智能方案
【8月更文挑战第3天】无缝融入,即刻智能[一]:Dify-LLM大模型平台,零编码集成嵌入第三方系统,42K+星标见证专属智能方案
无缝融入,即刻智能[一]:Dify-LLM大模型平台,零编码集成嵌入第三方系统,42K+星标见证专属智能方案
|
消息中间件 存储 大数据
聊一聊几款主流消息队列之间的差异,我们应该如何选择
聊一聊几款主流消息队列之间的差异,我们应该如何选择
1214 2
|
安全 关系型数据库 MySQL
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
2851 1
|
Java Linux API
CentOS下安装JDK的三种方法
方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java [root@localhost ~]# cd /usr/java   2.
2446 0