首页 > 其他分享 >0239-RLTK-分割不同文件

0239-RLTK-分割不同文件

时间:2024-08-16 16:26:43浏览次数:14  
标签:RLTK 0239 map mut 分割 pub pos let world

环境

  • Time 2022-11-30
  • WSL-Ubuntu 22.04
  • RLTK 0.8.7

前言

说明

参考:https://bfnightly.bracketproductions.com/rustbook

目标

基于前一节的内容,随着 main.rs 文件中的内容越来越多,将其进行分割。

comp.rs

use rltk::RGB;
use specs::prelude::*;
use specs_derive::Component;

#[derive(Component)]
pub struct Position {
    pub x: i32,
    pub y: i32,
}

#[derive(Component)]
pub struct Renderable {
    pub glyph: rltk::FontCharType,
    pub fg: RGB,
    pub bg: RGB,
}

#[derive(Component)]
pub struct LeftWalker {}

#[derive(Component, Debug)]
pub struct Player {}

impl<'a> System<'a> for LeftWalker {
    type SystemData = (ReadStorage<'a, LeftWalker>, WriteStorage<'a, Position>);

    fn run(&mut self, (lefty, mut pos): Self::SystemData) {
        for (_lefty, pos) in (&lefty, &mut pos).join() {
            pos.x -= 1;
            if pos.x < 0 {
                pos.x = 79;
            }
        }
    }
}

player.rs

use rltk::prelude::*;
use specs::{Join, World, WorldExt};
use std::cmp::{max, min};

use crate::comp::{Player, Position};
use crate::map::{self, TileType};
use crate::State;

pub fn move_player(delta_x: i32, delta_y: i32, world: &mut World) {
    let mut positions = world.write_storage::<Position>();
    let mut players = world.write_storage::<Player>();
    let map = world.fetch::<Vec<TileType>>();

    for (_player, pos) in (&mut players, &mut positions).join() {
        let destination_idx = map::index(pos.x + delta_x, pos.y + delta_y);
        if map[destination_idx] != TileType::Wall {
            pos.x = min(79, max(0, pos.x + delta_x));
            pos.y = min(49, max(0, pos.y + delta_y));
        }
    }
}

pub fn player_input(state: &mut State, context: &mut Rltk) {
    if let Some(key) = context.key {
        match key {
            VirtualKeyCode::Left => move_player(-1, 0, &mut state.world),
            VirtualKeyCode::Right => move_player(1, 0, &mut state.world),
            VirtualKeyCode::Up => move_player(0, -1, &mut state.world),
            VirtualKeyCode::Down => move_player(0, 1, &mut state.world),
            _ => {}
        }
    }
}

map.rs

use rltk::prelude::*;

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum TileType {
    Wall,
    Floor,
}

pub fn index(x: i32, y: i32) -> usize {
    (y as usize * 80) + x as usize
}

pub fn new_map() -> Vec<TileType> {
    let mut map = vec![TileType::Floor; 80 * 50];

    for x in 0..80 {
        map[index(x, 0)] = TileType::Wall;
        map[index(x, 49)] = TileType::Wall;
    }
    for y in 0..50 {
        map[index(0, y)] = TileType::Wall;
        map[index(79, y)] = TileType::Wall;
    }

    let mut rng = rltk::RandomNumberGenerator::new();

    for _i in 0..400 {
        let x = rng.roll_dice(1, 79);
        let y = rng.roll_dice(1, 49);
        let idx = index(x, y);
        if idx != index(40, 25) {
            map[idx] = TileType::Wall;
        }
    }

    map
}

pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
    let mut y = 0;
    let mut x = 0;
    for tile in map.iter() {
        match tile {
            TileType::Floor => {
                ctx.set(
                    x,
                    y,
                    RGB::from_f32(0.5, 0.5, 0.5),
                    RGB::from_f32(0., 0., 0.),
                    rltk::to_cp437('.'),
                );
            }
            TileType::Wall => {
                ctx.set(
                    x,
                    y,
                    RGB::from_f32(0.0, 1.0, 0.0),
                    RGB::from_f32(0., 0., 0.),
                    rltk::to_cp437('#'),
                );
            }
        }

        x += 1;
        if x > 79 {
            x = 0;
            y += 1;
        }
    }
}

main.rs

use comp::{LeftWalker, Player, Position, Renderable};
use map::TileType;
use rltk::prelude::*;
use specs::prelude::*;

mod comp;
mod map;
mod player;

pub struct State {
    world: World,
}
impl GameState for State {
    fn tick(&mut self, context: &mut Rltk) {
        context.cls();

        player::player_input(self, context);
        self.run_systems();

        let map1 = self.world.fetch::<Vec<TileType>>();
        map::draw_map(&map1, context);

        let positions = self.world.read_storage::<Position>();
        let renderables = self.world.read_storage::<Renderable>();
        for (pos, render) in (&positions, &renderables).join() {
            context.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
        }
    }
}

impl State {
    fn run_systems(&mut self) {
        let mut lw = LeftWalker {};
        lw.run_now(&self.world);
        self.world.maintain();
    }
}

fn main() -> rltk::BError {
    let context = rltk::RltkBuilder::simple80x50()
        .with_title("冒险游戏")
        .build()?;

    let mut state = State {
        world: World::new(),
    };
    state.world.register::<Position>();
    state.world.register::<Renderable>();
    state.world.register::<LeftWalker>();
    state.world.register::<Player>();

    state.world.insert(map::new_map());

    state
        .world
        .create_entity()
        .with(Position { x: 40, y: 25 })
        .with(Renderable {
            glyph: rltk::to_cp437('@'),
            fg: RGB::named(rltk::YELLOW),
            bg: RGB::named(rltk::BLACK),
        })
        .with(Player {})
        .build();

    rltk::main_loop(context, state)
}

效果

生成地图

总结

将功能划分到了不同的文件,并且保持功能不变。

附录

标签:RLTK,0239,map,mut,分割,pub,pos,let,world
From: https://www.cnblogs.com/jiangbo4444/p/18363138

相关文章

  • 0240-RLTK-生成小房间
    环境Time2022-11-30WSL-Ubuntu22.04RLTK0.8.7前言说明参考:https://bfnightly.bracketproductions.com/rustbook目标基于前一节的内容,生成两个小房间,只修改map.rs文件。生成房间函数fnapply_room_to_map(room:&Rect,map:&mut[TileType]){foryinroom......
  • 分割模型的数据集由json转为txt
    点击查看代码#-*-coding:utf-8-*-importjsonimportosimportargparsefromtqdmimporttqdmimportglobimportcv2importnumpyasnpdefconvert_label_json(json_dir,save_dir,classes):json_paths=os.listdir(json_dir)classes=classes.spli......
  • 0234-RLTK-创建主程序窗口
    环境Time2022-11-29WSL-Ubuntu22.04RLTK0.8.7前言说明参考:https://bfnightly.bracketproductions.com/rustbook/chapter_1.html目标RLTK表示RoguelikeToolkit,随机游戏工具包,使用这个包创建一个主窗口。Cargo.toml[package]edition="2021"name="game"vers......
  • 0235-RLTK-渲染静态字符
    环境Time2022-11-29WSL-Ubuntu22.04RLTK0.8.7前言说明参考:https://bfnightly.bracketproductions.com/rustbook/目标渲染一个主窗口,并且在窗口上渲染一些静态的字符。Cargo.toml[package]edition="2021"name="game"version="0.1.0"[dependencies]rl......
  • python之numpy (5 分割和复制)
    分割分割指将矩阵分割为几个小部分,以便于后续的计算需要。splitimportnumpyasnpm=np.random.random((3,3))print(m)sp=np.split(m,3,axis=0)ssp=np.split(m,3,axis=1)print(sp,ssp,sep='\n')[[0.373247510.933194940.18961048][0.814330810.377225750.00708......
  • 使用Mask R-CNN实现图像分割
    使用MaskR-CNN实现分割步骤1.导入依赖项import osimport torchimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imagefrom torch.utils.data import Dataset, DataLoaderfrom torchvision.transforms import Compose, ToTe......
  • 图像分割算法
    5.1阈值分割(Thresholding)介绍阈值分割是一种简单而有效的图像分割方法,通过设置一个或多个阈值,将图像分割为前景和背景区域。常见的阈值分割方法包括全局阈值、自适应阈值和Otsu阈值。原理阈值分割通过比较像素值与设定的阈值,将像素分类为前景或背景。公式在阈值分割......
  • AI在医学领域:nnSynergyNet3D高精度分割肝硬化肝脏体MRI图像
    关键词:肝硬化肝脏分割、协同深度学习模型、跨模态泛化    肝硬化是慢性肝病(CLD)的最后阶段,是一个重大的全球性健康问题。2019年,它是全球死亡原因的第11位,占全球死亡人数的2.4%。尽管病毒性肝炎仍然是终末期肝病的主要原因,但与代谢功能障碍相关的脂肪肝病(MASLD)预计将由于......
  • Skeleton Recall Loss 分割领域的新突破:极大的减少了资源消耗,还能提高性能
    精确分割在当今众多领域都是一项关键需求比如说自动驾驶汽车的训练、医学图像识别系统,以及通过卫星图像进行监测。在许多其他领域,当感兴趣的对象微小但至关重要时,例如研究血管流动、手术规划、检测建筑结构中的裂缝或优化路线规划,需要更高的精度。此前已经做了大量工作来解决这种......
  • CLAM实现指定区域分割
    CLAMhttps://github.com/mahmoodlab/CLAM    CLAM(Clustering-constrainedAttentionMultipleInstanceLearning)。旨在用于数据高效的弱监督计算病理学,特别是使用切片级标签对全切片图像(WSI)进行分类,无需提取感兴趣区域 (ROI)或进行切片级别的标注。     先......