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
相关推荐
Rauser Mack28 分钟前
Vibe coding游戏实战:零代码编程五子棋小游戏
人工智能·python·游戏·html·prompt
想做后端的前端11 小时前
游戏里的水面是怎么做的
游戏
leoZ23114 小时前
Claude 全面解析:从基础原理到实战应用指南
人工智能·游戏
2501_9437823517 小时前
【共创季稿事节】猜数字游戏:二分法思维与交互式反馈
前端·游戏·microsoft·harmonyos·鸿蒙·鸿蒙系统
SmalBox20 小时前
【节点】[IrisOffset节点]原理解析与实际应用
unity3d·游戏开发·图形学
星空露珠21 小时前
迷你世界UGc3.0脚本Wiki[剧情动画模块管理接口 Timeline]
开发语言·数据结构·算法·游戏·lua
yangmu32031 天前
《星露谷物语》MOD配置与实战安装综合指南
游戏·游戏引擎·游戏程序
xcLeigh1 天前
Unity基础:Game视图详解——游戏预览、分辨率模拟与性能显示
游戏·unity·游戏引擎·音频·视频·game·play模式
yyuuuzz1 天前
2026独立站运营的几个技术细节问题
运维·服务器·网络·人工智能·游戏
Johnstons2 天前
游戏网络测试怎么做?从延迟到丢包,一套完整的游戏弱网测试方案
网络·游戏·php