Last active
November 13, 2019 14:22
-
-
Save lukewilson2002/dbc3e6c95894e9f54cdfd0f2cb40d6b8 to your computer and use it in GitHub Desktop.
Bounce a ball horizontally in your terminal window. This uses the `@cInclude` Zig feature and creates a very weak wrapper over the required ncurses features. You will need ncurses installed, and for Ubuntu-based systems it's as easy as `sudo apt install libncurses-dev`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Builder = @import("std").build.Builder; | |
pub fn build(b: *Builder) void { | |
const mode = b.standardReleaseOptions(); | |
const exe = b.addExecutable("qubit", "src/main.zig"); | |
exe.setBuildMode(mode); | |
exe.linkSystemLibrary("c"); // link with libc | |
exe.linkSystemLibrary("ncurses"); // link with ncurses.a (requires ncurses be installed where program is run) | |
exe.install(); | |
const run_cmd = exe.run(); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.step); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const nc = @import("ncurses.zig"); | |
const delay = 30000000; // ~.3 seconds | |
pub fn main() anyerror!void { | |
var x: i32 = 0; | |
var y: i32 = 0; | |
var x_dir: i32 = 1; | |
var y_dir: i32 = 1; | |
nc.initscr(); | |
nc.noecho(); // suppress automatic echoing of typed characters | |
nc.keypad(true); // capture special keys | |
nc.curs_set(false); // hide cursor | |
var h: i32 = 0; | |
var w: i32 = 0; | |
while (true) { | |
nc.clear(); // clear terminal screen | |
nc.getmaxyx(&h, &w); // get terminal width and height | |
// change ball horizontal direction when hitting left and right walls | |
if (x + x_dir >= w) x_dir = -1 | |
else if (x + x_dir < 0) x_dir = 1; | |
x += x_dir; | |
// change ball vertical direction when hitting ceiling and floor | |
if (y + y_dir >= h) y_dir = -1 | |
else if (y + y_dir < 0) y_dir = 1; | |
y += y_dir; | |
nc.mvprintw(y, x, c"o"); // print "ball" at current xy position | |
nc.refresh(); | |
std.time.sleep(delay); | |
} | |
nc.endwin(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub const c = @cImport({ | |
@cInclude("ncurses.h"); | |
}); | |
pub fn initscr() void { | |
_ = c.initscr(); | |
} | |
pub fn endwin() void { | |
_ = c.endwin(); | |
} | |
pub fn noecho() void { | |
_ = c.noecho(); | |
} | |
pub fn keypad(b: bool) void { | |
_ = c.keypad(c.stdscr, b); | |
} | |
pub fn curs_set(visible: bool) void { | |
if (visible) _ = c.curs_set(1) | |
else _ = c.curs_set(0); | |
} | |
pub fn refresh() void { | |
_ = c.refresh(); | |
} | |
pub fn clear() void { | |
_ = c.clear(); | |
} | |
pub fn getmaxyx(rows: *i32, cols: *i32) void { | |
rows.* = c.getmaxy(c.stdscr); | |
cols.* = c.getmaxx(c.stdscr); | |
} | |
pub fn mvprintw(row: i32, col: i32, fmt: [*]const u8, args: ...) void { | |
_ = c.mvprintw(row, col, fmt, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment