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 小时前
godot-rust(gdext)2D游戏之旅【flappy-bird】 - 3
游戏·游戏开发
wanhengidc5 小时前
服务器租用的好处
大数据·运维·服务器·游戏·智能手机
LcGero5 小时前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb
LcGero1 天前
TypeScript 快速上手:前言
typescript·cocos creator·游戏开发
AI_零食1 天前
Flutter 框架跨平台鸿蒙开发 - 社交断舍离应用
运维·服务器·学习·flutter·游戏·开源·harmonyos
雷焰财经1 天前
首都在线MaaS平台:打造企业级AI中枢,驱动游戏产业智变
大数据·人工智能·游戏
wanhengidc1 天前
云手机 热血传奇游戏挂机
服务器·网络·安全·游戏·智能手机
黑客说1 天前
AI 游戏:从固定剧本到无限宇宙
人工智能·游戏
Setsuna_F_Seiei2 天前
CocosCreator 游戏开发 - 多维度状态机架构设计与实现
前端·cocos creator·游戏开发