godot-rust(gdext)2D游戏之旅【flappy-bird】 - 1

简介

本文以Make Flappy Bird Game in Godot Engine | gameidea为蓝本撰写。如果你对rust、godot等内容比较生疏或感到疑惑,可以先阅读godot-rust入门文档

本文搭建在godot-rust(gdext)创建项目的基础之上,如果你对该部分内容感到生疏,可以先阅读这部分内容,为接下来的操作做好准备。

注意,本文使用的godot版本为4.6.2,godot版本的变化可能会导致一些内容失效

本文所有操作均在Windows11环境下进行。

正文

项目准备

项目=>项目设置=>常规=>显示=>窗口

  • 视口宽度:540
  • 视口高度:720

你需要的一些游戏素材从这里下载,将他们放入assets文件夹下。

此时你的项目看上去应该像这样

项目设置中找到输入映射,增加一个动作flap并映射到空格键(Space)

创建Bird

在rust中创建Bird节点,记得不要忘记在你的lib.rs文件中添加mod bird;

rust 复制代码
// bird.rs
use godot::prelude::*;

#[derive(GodotClass)]
#[class(init, base=CharacterBody2D)]
struct Bird {}

执行cargo build

在godot中新建你刚刚创建的Bird节点。

右键点击Bird,点击添加子节点 ,为你的Bird节点添加一个AnimatedSprite2D子节点,用这个节点来表示Bird的视觉效果。选中AnimatedSprite2D后在检查器 面板中的AnimatedSprite2D > Sprite Frames 中,新建SpriteFrames ,点击你刚刚创建的SpriteFrames,在编辑器的底部会出现动画面板。

AnimatedSprite2D添加两个动画fallflap,通过拖动图片为两个动画添加内容:

  • fall:bird-1.png
  • flap:bird-2.png

检查器 > AnimatedSprite2D > Transform > Scale,设置:

  • x:0.1
  • y:0.1

用同样的方法为Bird添加CollisionShape2D子节点,用这个节点来表示Bird的物理效果。选中CollisionShape2D子节点,在godot编辑器中的检查器 > Shape ,选择CircleShape2D ,手动调节蓝色圆形大小以尽可能表示Bird物理形状。

现在来丰富我们rust中Bird代码

rust 复制代码
// bird.rs
use godot::{
    classes::{AnimatedSprite2D, CharacterBody2D, ICharacterBody2D, Input},
    prelude::*,
};

#[derive(GodotClass)]
#[class(init, base=CharacterBody2D)]
pub struct Bird {
    #[init(node = "AnimatedSprite2D")]
    animated_sprite: OnReady<Gd<AnimatedSprite2D>>,

    #[init(val = OnReady::manual())]
    tree: OnReady<Gd<SceneTree>>,

    base: Base<CharacterBody2D>,
}

#[godot_api]
impl Bird {
    const GRAVITY: real = 1372.0; // 980.0 * 1.4
    const FLAP_FORCE: real = -425.0;

    #[signal]
    pub fn hit();

    pub fn stop(&mut self) {
        self.base_mut().set_physics_process(false);
    }

    pub fn reset(&mut self) {
        self.base_mut().set_position(Vector2 { x: 270.0, y: 360.0 });
    }

    pub fn start(&mut self) {
        self.base_mut().set_physics_process(true);
    }

    fn play(&mut self, animation_name: &str) {
        self.animated_sprite.set_animation(animation_name);
        self.animated_sprite.play();
    }
}

#[godot_api]
impl ICharacterBody2D for Bird {
    fn ready(&mut self) {
        self.tree.init(self.base().get_tree());

        self.play("fall");
    }

    fn physics_process(&mut self, delta: f32) {
        let input = Input::singleton();
        let mut velocity = self.base().get_velocity();

        if !self.base().is_on_floor() {
            velocity.y += Bird::GRAVITY * delta;
        }

        if input.is_action_just_pressed("flap") {
            velocity.y = Bird::FLAP_FORCE;
            self.play("flap");

            self.tree
                .create_timer(0.25)
                .signals()
                .timeout()
                .connect_other(&*self, |bird| {
                    bird.play("fall");
                });
        }

        self.base_mut().set_velocity(velocity);
        self.base_mut().move_and_slide();

        if self.base().get_slide_collision_count() > 1 {
            self.signals().hit().emit();
        }
    }
}

编译你的Bird

复制代码
cargo build

回到godot编辑器,在编辑器右上角点击运行场景

你会看到小鸟发生自由落体,而在你按下空格键后,它会向上飞一小段距离。

参考

  1. godot-rust(gdext)创建项目 - 掘金
  2. godot - Rust
  3. Make Flappy Bird Game in Godot Engine | gameidea
相关推荐
SmalBox5 小时前
YooAsset-全篇导览
unity3d·游戏开发
Jgch225 小时前
出海Facebook广告账户怎么选?
游戏·facebook
长脖鹿Johnny9 小时前
游戏输入系统框架设计(三):网络输入权威与可验证性
网络·游戏·游戏开发·架构设计·输入系统·网络同步
谈资一本本10 小时前
电脑全能修复大师 Gilisoft Total Repair,智能检测修复系统、应用、游戏问题!DLL、DirectX、.NET运行库损坏修复+缺失自动补齐!
游戏·电脑·.net
程序员-Benothing18 小时前
H3 Max开放世界RPG:AI原生游戏,从“能生成“到“能玩“差了多远
游戏·ai-native
2601_9620781918 小时前
python怎么用
游戏开发·web开发·数据科学·自动化脚本·网页爬取
CoderYanger1 天前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
无别0521 天前
【做游戏】做完游戏Demo后没地方发布?
游戏·玩游戏
lilian2331 天前
HarmonyOS 7 新特性(二十九)|游戏伴随服务:悬浮协作与质量降级
游戏·语音识别·harmonyos
人民广场吃泡面1 天前
DLSS 5 深度解析:当 AI 学会“创造”画面,实时游戏渲染迎来“GPT 时刻”
人工智能·gpt·游戏