Browse Source

add source code and tiny tweaks

ok i swear i'll stop poking at this now
main
cynthia 2 years ago
parent
commit
5024b2ce23
  1. 9
      cool-game/Cargo.toml
  2. 53
      cool-game/src/main.rs
  3. 2
      html/index.html
  4. 5
      readme.md

9
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"

53
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,
);
}
}

2
html/index.html

@ -91,7 +91,7 @@
<p>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!</p>
<h2 id="getting-started">Getting started</h2>
<p><strong>Where do I write Rust?</strong> Rust has two language servers that attach to your editor to analyze your code while you write it, <code>rls</code> and <code>rust-analyzer</code> (most people use the second). If you’re not already attached to a text editor, VS Code’s <code>rust-analyzer</code> extension is really good!</p>
<p><strong>Where do I find documentation?</strong> Documentation for Rust’s standard library is at <a href="https://doc.rust-lang.org/std/index.html" class="uri">https://doc.rust-lang.org/std/index.html</a>. Documentation for crates like macroquad is at <a href="https://docs.rs/crate_name" class="uri">https://docs.rs/crate_name</a>.</p>
<p><strong>Where do I find documentation?</strong> Documentation for Rust’s standard library is at <a href="https://doc.rust-lang.org/std/index.html" class="uri">https://doc.rust-lang.org/std/index.html</a>. Documentation for crates like macroquad is at <a href="#" class="uri">https://docs.rs/crate_name</a>.</p>
<p><strong>What’s cargo?</strong> Cargo is Rust’s package manager and build system. It downloads crates from <a href="https://crates.io" class="uri">https://crates.io</a>, 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.</p>
<h2 id="an-example">An example</h2>
<p>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:</p>

5
readme.md

@ -2,6 +2,7 @@ this is a rust zine that provides an introduction to the language if you don't h
- html: <https://licynthiax.gitlab.io/rust-zine>, 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 <https://jywarren.github.io/bookletize.js/>)
- 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 <https://jywarren.github.io/bookletize.js>)
- 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.
Loading…
Cancel
Save