Snake: Difference between revisions

49 bytes added ,  11 months ago
m
m (added up and down arrow keys, removed unused G variable comment)
m (→‎{{header|Rust}}: cosmetic)
Line 4,605:
<syntaxhighlight lang="rust">/* add to file Cargo.toml:
[dependencies]
winsafe = "0.0.8" # IMHO: before the appearance of winsafe="0.1" it is not worth raising the version here
winsafe = "0.0.8"
rand = "0.8"
derive-new = "0.5"
Line 4,627:
#[derive(new)]
struct Context {
wnd: gui::WindowMain,
#[new(default) ] snake: Vec<i32>, // [rect_ids] where rect_id = y * TW + x (where x, y: nSTEPs)
#[new(value = "[ID0; 6]")] r: [i32; 6], // ID 6 rect to color in next frame (bg, tail, turn, body, food, head)
Line 4,646:
let wnd = gui::WindowMain::new(gui::WindowMainOpts {
title: "Snake - Start: Space, then press W-A-S-D".to_string(),
size: winsafe::SIZE::new(FW * GCW * STEP, FW * GCW * STEP),
class_bg_brush: brushes[bg],
..Default::default()
Line 4,660:
let hdc = ctx.wnd.hwnd().BeginPaint(&mut ps)?;
hdc.SelectObjectPen(HPEN::CreatePen(co::PS::NULL, 0, COLORREF::new(0, 0, 0))?)?;
for (&rect_idid, &brush) in ctx.r.iter().zip(&brushes[bg..=head]) {
let [left, top] = rect_id[id % TW, id / TW].map(|i| i * STEP - (STEP * GCW + SNAKE_W) / 2);
hdc.SelectObjectBrush(brush)?;
let left = rect_id % TW * STEP - (STEP * GCW + SNAKE_W) / 2;
let top = rect_id / TW * STEP - (STEP * GCW + SNAKE_W) / 2;
let rect = RECT { left, top, right: left + SNAKE_W, bottom: top + SNAKE_W };
hdc.SelectObjectBrush(brush)?;
hdc.RoundRect(rect, SIZE::new(SNAKE_W / 2, SNAKE_W / 2))?;
}
106

edits