使用Rust开发小游戏完成过程

 更新时间:2023年11月27日 14:39:36   作者:techdashen  
这篇文章主要介绍了使用Rust开发小游戏的完整过程,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

本文是对 使用 Rust 开发一个微型游戏【已完结】

cargo new flappy

在Cargo.toml的[dependencies]下方增加:

bracket-lib = "~0.8.7"

main.rs中:

use bracket_lib::prelude::*;
struct State {}
impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print(1, 1, "Hello,Bracket Terminal!");
    }
}
fn main() -> BError {
    let context: BTerm = BTermBuilder::simple80x50()
        .with_title("爽哥做游戏--Flappy Dragon")
        .build()?;
    main_loop(context, State {})
}

cargo run后,可以看到

use bracket_lib::prelude::*;
// 游戏3种模式(菜单,游戏中,结束)
enum GameMode {
    Menu,
    Playing,
    End,
}
struct State {
    mode: GameMode,
}
impl State {
    fn new() -> Self {
        State {
            mode: GameMode::Menu,
        }
    }
    fn play(&mut self, ctx: &mut BTerm) {
        //TODO
        self.mode = GameMode::End;
    }
    fn restart(&mut self) {
        self.mode = GameMode::Playing;
    }
    fn main_menu(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "欢迎来到游戏~");
        ctx.print_centered(8, "Press P key to start Game");
        ctx.print_centered(9, "Press Q to quit Game");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
    fn dead(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "你挂了..");
        ctx.print_centered(8, "按P键 再来一局");
        ctx.print_centered(9, "按Q键 退出游戏");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
}
impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        match self.mode {
            GameMode::Menu => self.main_menu(ctx),
            GameMode::End => self.dead(ctx),
            GameMode::Playing => self.play(ctx),
        }
        // ctx.cls();
        // ctx.print(1, 1, "Hello,Bracket Terminal!");
    }
}
fn main() -> BError {
    let context: BTerm = BTermBuilder::simple80x50()
        .with_title("爽哥做游戏--Flappy Dragon")
        .build()?;
    main_loop(context, State::new())
}

增加玩家

use bracket_lib::prelude::*;
// 游戏3种模式(菜单,游戏中,结束)
enum GameMode {
    Menu,
    Playing,
    End,
}
const SCREEN_WIDTH: i32 = 80;
const SCREEN_HEIGHT: i32 = 50;
const FRAME_DURATION: f32 = 75.0;
struct Player {
    x: i32,
    y: i32,
    velocity: f32,
}
impl Player {
    fn new(x: i32, y: i32) -> Self {
        Player {
            x: 0,
            y: 0,
            velocity: 0.0,
        }
    }
    fn render(&mut self, ctx: &mut BTerm) {
        ctx.set(0, self.y, YELLOW, BLACK, to_cp437('@'));
    }
    fn gravity_and_move(&mut self) {
        if self.velocity < 2.0 {
            self.velocity += 0.2;
        }
        self.y += self.velocity as i32;
        self.x += 1;
        if self.y < 0 {
            self.y = 0;
        }
    }
    fn flap(&mut self) {
        self.velocity = -2.0;
    }
}
struct State {
    player: Player,
    frame_time: f32,
    mode: GameMode,
}
impl State {
    fn new() -> Self {
        State {
            player: Player::new(5, 25),
            frame_time: 0.0,
            mode: GameMode::Menu,
        }
    }
    fn play(&mut self, ctx: &mut BTerm) {
        ctx.cls_bg(NAVY);
        self.frame_time += ctx.frame_time_ms;
        if self.frame_time >= FRAME_DURATION {
            self.player.gravity_and_move();
            self.frame_time = 0.0;
        }
        // 按空格
        if let Some(VirtualKeyCode::Space) = ctx.key {
            self.player.flap();
        }
        self.player.render(ctx);
        ctx.print(0, 0, "按空格起飞~");
        if self.player.y > SCREEN_HEIGHT {
            self.mode = GameMode::End;
        }
    }
    fn restart(&mut self) {
        self.player = Player::new(5, 25);
        self.frame_time = 0.0;
        self.mode = GameMode::Playing;
    }
    fn main_menu(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "欢迎来到游戏~");
        ctx.print_centered(8, "Press P key to start Game");
        ctx.print_centered(9, "Press Q to quit Game");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
    fn dead(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "你挂了..");
        ctx.print_centered(8, "按P键 再来一局");
        ctx.print_centered(9, "按Q键 退出游戏");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
}
impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        match self.mode {
            GameMode::Menu => self.main_menu(ctx),
            GameMode::End => self.dead(ctx),
            GameMode::Playing => self.play(ctx),
        }
        // ctx.cls();
        // ctx.print(1, 1, "Hello,Bracket Terminal!");
    }
}
fn main() -> BError {
    let context: BTerm = BTermBuilder::simple80x50()
        .with_title("爽哥做游戏--Flappy Dragon")
        .build()?;
    main_loop(context, State::new())
}

增加障碍

use std::fmt::format;
use bracket_lib::prelude::*;
// 游戏3种模式(菜单,游戏中,结束)
enum GameMode {
    Menu,
    Playing,
    End,
}
const SCREEN_WIDTH: i32 = 80;
const SCREEN_HEIGHT: i32 = 50;
const FRAME_DURATION: f32 = 75.0;
struct Player {
    x: i32,
    y: i32,
    velocity: f32,
}
impl Player {
    fn new(x: i32, y: i32) -> Self {
        Player {
            x: 0,
            y: 0,
            velocity: 0.0,
        }
    }
    fn render(&mut self, ctx: &mut BTerm) {
        ctx.set(0, self.y, YELLOW, BLACK, to_cp437('@'));
    }
    fn gravity_and_move(&mut self) {
        if self.velocity < 2.0 {
            self.velocity += 0.2;
        }
        self.y += self.velocity as i32;
        self.x += 1;
        if self.y < 0 {
            self.y = 0;
        }
    }
    fn flap(&mut self) {
        self.velocity = -2.0;
    }
}
struct State {
    player: Player,
    frame_time: f32,
    mode: GameMode,
    obstacle: Obstacle,
    score: i32,
}
impl State {
    fn new() -> Self {
        State {
            player: Player::new(5, 25),
            frame_time: 0.0,
            mode: GameMode::Menu,
            obstacle: Obstacle::new(SCREEN_WIDTH, 0),
            score: 0,
        }
    }
    fn play(&mut self, ctx: &mut BTerm) {
        ctx.cls_bg(NAVY);
        self.frame_time += ctx.frame_time_ms;
        if self.frame_time >= FRAME_DURATION {
            self.player.gravity_and_move();
            self.frame_time = 0.0;
        }
        // 按空格
        if let Some(VirtualKeyCode::Space) = ctx.key {
            self.player.flap();
        }
        self.player.render(ctx);
        ctx.print(0, 0, "按空格起飞~");
        //  障碍物&积分
        // 实时打印分数
        ctx.print(0, 1, &format!("Score: {}", self.score));
        self.obstacle.render(ctx, self.player.x);
        if self.player.x > self.obstacle.x {
            self.score += 1; // 分数+1
            self.obstacle = Obstacle::new(self.player.x + SCREEN_WIDTH, self.score);
        }
        if self.player.y > SCREEN_HEIGHT || self.obstacle.hit_obstacle(&self.player) {
            self.mode = GameMode::End;
        }
    }
    fn restart(&mut self) {
        self.player = Player::new(5, 25);
        self.frame_time = 0.0;
        self.mode = GameMode::Playing;
        // 重置分数和障碍物
        self.obstacle = Obstacle::new(SCREEN_WIDTH, 0);
        self.score = 0;
    }
    fn main_menu(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "欢迎来到游戏~");
        ctx.print_centered(8, "Press P key to start Game");
        ctx.print_centered(9, "Press Q to quit Game");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
    fn dead(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.print_centered(5, "你挂了..");
        // 挂了后显示一下分数
        ctx.print_centered(6, &format!("本局获得了 {} 分", self.score));
        ctx.print_centered(8, "按P键 再来一局");
        ctx.print_centered(9, "按Q键 退出游戏");
        if let Some(key) = ctx.key {
            match key {
                VirtualKeyCode::P => self.restart(),
                VirtualKeyCode::Q => ctx.quitting = true,
                _ => {}
            }
            {}
        }
    }
}
impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        match self.mode {
            GameMode::Menu => self.main_menu(ctx),
            GameMode::End => self.dead(ctx),
            GameMode::Playing => self.play(ctx),
        }
    }
}
struct Obstacle {
    x: i32,
    gap_y: i32,
    size: i32,
}
impl Obstacle {
    fn new(x: i32, score: i32) -> Self {
        let mut random: RandomNumberGenerator = RandomNumberGenerator::new();
        Obstacle {
            x,
            gap_y: random.range(10, 40),
            size: i32::max(2, 20 - score),
        }
    }
    fn render(&mut self, ctx: &mut BTerm, player_x: i32) {
        let screen_x = self.x - player_x;
        let half_size = self.size / 2;
        for y in 0..self.gap_y - half_size {
            ctx.set(screen_x, y, RED, BLACK, to_cp437('|'));
        }
        for y in self.gap_y + half_size..SCREEN_HEIGHT {
            ctx.set(screen_x, y, RED, BLACK, to_cp437('|'));
        }
    }
    fn hit_obstacle(&self, player: &Player) -> bool {
        let half_size = self.size / 2;
        let does_x_match = player.x == self.x;
        let player_above_gap = player.y < self.gap_y - half_size;
        let player_below_gap = player.y > self.gap_y + half_size;
        does_x_match && (player_above_gap || player_below_gap)
    }
}
fn main() -> BError {
    let context: BTerm = BTermBuilder::simple80x50()
        .with_title("爽哥做游戏--Flappy Dragon")
        .build()?;
    main_loop(context, State::new())
}

参考资料[1]

使用 Rust 开发一个微型游戏【已完结】: https://www.bilibili.com/video/BV1vM411J74S

到此这篇关于使用Rust开发小游戏的文章就介绍到这了,更多相关Rust开发小游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Rust编程中的共享状态并发执行

    详解Rust编程中的共享状态并发执行

    虽然消息传递是一个很好的处理并发的方式,但并不是唯一一个,另一种方式是让多个线程拥有相同的共享数据,本文给大家介绍Rust编程中的共享状态并发执行,感兴趣的朋友一起看看吧
    2023-11-11
  • rust中async/await的使用示例详解

    rust中async/await的使用示例详解

    在Rust中,async/await用于编写异步代码,使得异步操作更易于理解和编写,通过使用await,在async函数或代码块中等待Future完成,而不会阻塞线程,允许同时执行其他Future,这种机制简化了异步编程的复杂性,使代码更加直观
    2024-10-10
  • Go调用Rust方法及外部函数接口前置

    Go调用Rust方法及外部函数接口前置

    这篇文章主要为大家介绍了Go调用Rust方法及外部函数接口前置示例实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Rust中的Copy和Clone对比分析

    Rust中的Copy和Clone对比分析

    这篇文章主要介绍了Rust中的Copy和Clone及区别对比分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Rust 枚举的使用学习笔记

    Rust 枚举的使用学习笔记

    Rust中的枚举是一种用户定义的类型,本文主要介绍了Rust枚举的使用,它们不仅仅用于表示几个固定的值,还可以包含函数和方法,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • rust标准库std::env环境相关的常量

    rust标准库std::env环境相关的常量

    在本章节中, 我们探讨了Rust处理命令行参数的常见的两种方式和处理环境变量的两种常见方式, 抛开Rust的语法, 实际上在命令行参数的处理方式上, 与其它语言大同小异, 可能影响我们习惯的也就只剩下语法,本文介绍rust标准库std::env的相关知识,感兴趣的朋友一起看看吧
    2024-03-03
  • 详解Rust中#[derive]属性怎么使用

    详解Rust中#[derive]属性怎么使用

    在 Rust 中,#[derive] 是一个属性,用于自动为类型生成常见的实现,下面就跟随小编一起来学习一下Rust中derive属性的具体使用吧
    2024-11-11
  • Rust常用特型之ToOwned特型示例详解

    Rust常用特型之ToOwned特型示例详解

    在Rust中,假定某类型实现了Clone特型,如果给你一个对它引用,那我们得到它指向内容的备份的最常见方式是调用其clone()函数,这篇文章主要介绍了Rust常用特型之ToOwned特型,需要的朋友可以参考下
    2024-04-04
  • 深入了解Rust中trait的使用

    深入了解Rust中trait的使用

    先前我们提到过 trait,那么Rust中的trait 是啥呢?本文将通过一些示例为大家详细讲讲Rust中trait的使用,感兴趣的小伙伴可以了解一下
    2022-11-11
  • 从零开始使用Rust编写nginx(TLS证书快过期了)

    从零开始使用Rust编写nginx(TLS证书快过期了)

    wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 负载均衡, 静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,本文给大家介绍从零开始使用Rust编写nginx(TLS证书快过期了),感兴趣的朋友一起看看吧
    2024-03-03

最新评论