Hacker News new | ask | show | jobs
by Lerc 3333 days ago
It would be nice to have a minimal gameish program as a example for people learning Rust.

When I teach kids javascript I start with an etch-a-sketch. It's aided by a simple library to hide the mechanics of the HTML canvas element, context, etc. This allows it to be small enough that they can view it all in one go and build upon it.

    print("Draw with the arrow keys");

    var cx=320;
    var cy=240;

    function update() {
      // the arrow keys have key codes 37,38,39 and 40
      if (keyIsDown(38)) {    cy-=1;  } 
      if (keyIsDown(40)) {    cy+=1;  }
      if (keyIsDown(37)) {    cx-=1;  }  
      if (keyIsDown(39)) {    cx+=1;  }
  
      fillCircle(cx,cy,6);
    }

    run(update);


There might be merit in writing one of these in every language (and a companion that uses the mouse), maybe placing it on github . With a really simple program like this you can focus on learning the language while making something. It's a tough job figuring out how to learn a language while simultaneoulsy learning how to write the boilerplate needed to get something onscreen.
2 comments

For simple pixel access and no primitives, https://github.com/emoon/rust_minifb is very compact and neat.

Otherwise rust-sdl2 is batteries included, there may be more ceremony than html5 canvas but it's easy to hide behind a simple interface. It has many simple examples.

But, like with C++, as long as the language doesn't come with a standard graphics library, I doubt the "official" books can really use graphics apps as a learning tool.

Easy to hide behind a simple interface if you already know the programming language. My suggestion is that someone perhaps should do just that so that beginners don't have to learn how to construct a simple interface at the same time as they are learning the language.

The MiniFB code does seem to be a good starting point for this sort of thing.

Yeah, I was just echoing your comment that you provide a small library for HTML5 canvas-based teaching. If I was going to teach someone Rust, I would definitely do that, but of course I (the teacher) would need to know enough to build it in the first place.

I absolutely advocate using graphics and games as the hook to keep people interested in learning. [1] "The immediacy of many 8-bit computers was awesome. Instant boot to a graphics-ready command line and program editor. I was instantly hooked". And that's why, despite the really bad fit, I support the addition of a 2d std library to C++, and would do likewise for Go, Rust, etc.

[1] https://twitter.com/TheJare/status/860094027370291200

It's not a graphical game, but the introductory project in the book has been the "guessing game" for years now https://doc.rust-lang.org/stable/book/guessing-game.html
Yeah, it's the dynamic interactivity that I was looking for. I'm afriad console IO isn't a starter here. I'm sure it's fine as a console IO example, but it's just a completely different class of thing.
I'm actually a huge proponent of learning through text type games in lieu of more complicated graphics related black boxes where someone is essentially letting a library do 98% of the work. A text game is literally as simple as it gets while requiring no modules (well maybe IO or system if your language doesn't include it by default). Guess my number also is seems more mathematical although that is just subjective rubbish on my part ;). I do believe you're correct that certain people would like a graphical introduction more. Maybe you could write a cool intro to Rust with graphics!
I think the issue isn't text versus complicated graphics, it's that the only thing you can do with most standard libraries is buffered line I/O, which doesn't feel like directly controlling anything. If you could just read the actual keyboard and poke some letters into a 2D text array, it would enable a whole different kind of interaction.
Polling the keyboard and using something like ncurses can work. I've implemented console Tetris in an hour using just those tools, and you get to write a game loop, rather than being called back by a framework like most GUI setups.