🦀

chip1. RustとBevyで"Hello, world!"

2023/07/26に公開

はじめに

2023/07/26時点の内容です。

  • rustc 1.71.0
  • bevy 0.11.0
    bevyは開発初期段階のOSSで、まだまだ破壊的なアップデートが入ります。
    でも、面白いですよ。

Cargo.toml

普通にcargo new XXXでプロジェクトを作成し、cargo add bevyしてください。

Cargo.toml
[package]
name = "chip_1"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = "0.11.0"

a. 単純な"Hello, world!"

これだとさすがにつまらない。

chip1a
use bevy::prelude::*;

fn main()
{   App::new()
        .add_systems( Update, system )
        .run();
}

fn system()
{   println!( "Hello, world!" );
}

b. 少し”らしい” "Hello, world!"

もう少しそれっぽく。

chip1b
use bevy::prelude::*;

fn main()
{   App::new()
        .add_plugins( DefaultPlugins )   //ウインドウやスケジュールの面倒を見てもらう
        .add_systems
        (   Startup, 
            (   spawn_camera2d,          //カメラを作る
                spawn_text2d_helloworld, //2Dテキストを作る
            )
        )
        .add_systems
        (   Update,
            (   bevy::window::close_on_esc, //[ESC]キーで終了
            )
        )
        .run();
}

//カメラを作る
fn spawn_camera2d( mut cmds: Commands )
{   cmds.spawn( Camera2dBundle::default() );
}

//2Dテキストを作る
fn spawn_text2d_helloworld( mut cmds: Commands )
{   let textstyle = TextStyle { font_size: 100.0, ..default() };
    let text = Text::from_section( "Hello, world!", textstyle );
    cmds.spawn( Text2dBundle { text, ..default() } );
}

chip1bchip1b

Discussion