From 5024b2ce239d6d632a9b4a69546fa5c6e76f7004 Mon Sep 17 00:00:00 2001 From: cynthia <7065188-licynthiax@users.noreply.gitlab.com> Date: Fri, 12 Nov 2021 16:36:36 -0800 Subject: [PATCH] add source code and tiny tweaks ok i swear i'll stop poking at this now --- cool-game/Cargo.toml | 9 ++++++++ cool-game/src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ html/index.html | 2 +- readme.md | 5 ++-- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 cool-game/Cargo.toml create mode 100644 cool-game/src/main.rs diff --git a/cool-game/Cargo.toml b/cool-game/Cargo.toml new file mode 100644 index 0000000..5826897 --- /dev/null +++ b/cool-game/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "cool-game" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +macroquad = "0.3.10" diff --git a/cool-game/src/main.rs b/cool-game/src/main.rs new file mode 100644 index 0000000..f844756 --- /dev/null +++ b/cool-game/src/main.rs @@ -0,0 +1,53 @@ +use macroquad::prelude::*; + +#[macroquad::main("cool game")] +async fn main() { + let mut game = Game::new(); + loop { + if is_key_down(KeyCode::Escape) { + break; + } + game.draw(); + game.update(); + next_frame().await; + } +} + +struct Game { + player: Rect, +} + +impl Game { + fn new() -> Self { + Self { + player: Rect::new(50.0, 50.0, 25.0, 25.0), + } + } + + fn update(&mut self) { + if is_key_down(KeyCode::Up) { + self.player.y -= 1.0; + } + if is_key_down(KeyCode::Down) { + self.player.y += 1.0; + } + if is_key_down(KeyCode::Right) { + self.player.x += 1.0; + } + if is_key_down(KeyCode::Left) { + self.player.x -= 1.0; + } + } + + fn draw(&self) { + clear_background(DARKBLUE); + + draw_rectangle( + self.player.x, + self.player.y, + self.player.w, + self.player.h, + PINK, + ); + } +} diff --git a/html/index.html b/html/index.html index 47b60ed..a5923f9 100644 --- a/html/index.html +++ b/html/index.html @@ -91,7 +91,7 @@

Rust also has really good error messages, well-written documentation, and excellent tools that make writing code and debugging easier. I like it because it helps me make sure that my program is doing exactly what I want it to. I hope you like it too!

Getting started

Where do I write Rust? Rust has two language servers that attach to your editor to analyze your code while you write it, rls and rust-analyzer (most people use the second). If you’re not already attached to a text editor, VS Code’s rust-analyzer extension is really good!

-

Where do I find documentation? Documentation for Rust’s standard library is at https://doc.rust-lang.org/std/index.html. Documentation for crates like macroquad is at https://docs.rs/crate_name.

+

Where do I find documentation? Documentation for Rust’s standard library is at https://doc.rust-lang.org/std/index.html. Documentation for crates like macroquad is at https://docs.rs/crate_name.

What’s cargo? Cargo is Rust’s package manager and build system. It downloads crates from https://crates.io, runs projects, runs tests, generates documentation, manages dependencies, and more! You use it from the terminal—if you’re not familiar with it, there’s a tutorial linked on the last page.

An example

I’m going to walk through an example of an extremely minimal 2D game using the library Macroquad. First, you want to make a new crate (a project) with cargo. In the terminal, navigate to the folder where you want to make your project, and run the command:

diff --git a/readme.md b/readme.md index 7a75496..23a74c3 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,7 @@ this is a rust zine that provides an introduction to the language if you don't h - html: , screenreader-accessible version + working links. - pdf: rust-zine.pdf, to download and read. -- pdf (printable flavor): rust-zine-printable.pdf, print (double-sided!), put all the pages on top of each other, fold in half, and staple. (made with ) +- pdf (printable flavor): rust-zine-printable.pdf, print (double-sided!), put all the pages on top of each other, fold in half, and staple. (made with ) +- also some of the example code is in the `cool-game/` folder -please don't share publicly outside of the 5Cs, thanks +download by clicking the name of the pdf, then hit the download button in the top right corner.