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
相关推荐
魔法阵维护师4 小时前
从零开发游戏需要学习的c#模块,第二十章(2D 敌人与战斗触发)
学习·游戏·c#
相信神话20215 小时前
第四章:创建《酒魂》项目与场景结构
游戏·游戏引擎·godot·2d游戏开发
hhcgchpspk6 小时前
easyx按键游戏
c++·stm32·单片机·游戏·easyx
魔法阵维护师6 小时前
从零开发游戏需要学习的c#模块,第二十一章(精灵动画 —— 让角色走起来)
学习·游戏·c#
好想写代码7 小时前
Java:猜数字游戏
游戏
2501_940041747 小时前
挖掘前端交互潜力的五款创意游戏原型
前端·游戏
Kurisu5757 小时前
博德之门3 2026最新免费下载 一键转存 永久更新 (看到速转存 资源随时走丢)
游戏·游戏程序·动画·游戏美术·友善·爱国
^—app5668668 小时前
多款游戏vivo小游戏Oppo小游戏开发定制游戏流量主广告联盟成品搭建部署
游戏
richard_yuu20 小时前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
魔法阵维护师20 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#