Snake: Difference between revisions
Content deleted Content added
m added up and down arrow keys, removed unused G variable comment |
m →{{header|Rust}}: cosmetic |
||
Line 4,605: | Line 4,605: | ||
<syntaxhighlight lang="rust">/* add to file Cargo.toml: |
<syntaxhighlight lang="rust">/* add to file Cargo.toml: |
||
[dependencies] |
[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" |
rand = "0.8" |
||
derive-new = "0.5" |
derive-new = "0.5" |
||
Line 4,627: | Line 4,627: | ||
#[derive(new)] |
#[derive(new)] |
||
struct Context { |
struct Context { |
||
wnd: gui::WindowMain, |
wnd: gui::WindowMain, |
||
#[new(default) ] snake: Vec<i32>, // [rect_ids] where rect_id = y * TW + x (where x, y: nSTEPs) |
#[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) |
#[new(value = "[ID0; 6]")] r: [i32; 6], // ID 6 rect to color in next frame (bg, tail, turn, body, food, head) |
||
Line 4,646: | Line 4,646: | ||
let wnd = gui::WindowMain::new(gui::WindowMainOpts { |
let wnd = gui::WindowMain::new(gui::WindowMainOpts { |
||
title: "Snake - Start: Space, then press W-A-S-D".to_string(), |
title: "Snake - Start: Space, then press W-A-S-D".to_string(), |
||
size: |
size: SIZE::new(FW * GCW * STEP, FW * GCW * STEP), |
||
class_bg_brush: brushes[bg], |
class_bg_brush: brushes[bg], |
||
..Default::default() |
..Default::default() |
||
Line 4,660: | Line 4,660: | ||
let hdc = ctx.wnd.hwnd().BeginPaint(&mut ps)?; |
let hdc = ctx.wnd.hwnd().BeginPaint(&mut ps)?; |
||
hdc.SelectObjectPen(HPEN::CreatePen(co::PS::NULL, 0, COLORREF::new(0, 0, 0))?)?; |
hdc.SelectObjectPen(HPEN::CreatePen(co::PS::NULL, 0, COLORREF::new(0, 0, 0))?)?; |
||
for (& |
for (&id, &brush) in ctx.r.iter().zip(&brushes[bg..=head]) { |
||
⚫ | |||
⚫ | |||
let left = rect_id % TW * STEP - (STEP * GCW + SNAKE_W) / 2; |
|||
⚫ | |||
let rect = RECT { left, top, right: left + SNAKE_W, bottom: top + SNAKE_W }; |
let rect = RECT { left, top, right: left + SNAKE_W, bottom: top + SNAKE_W }; |
||
⚫ | |||
hdc.RoundRect(rect, SIZE::new(SNAKE_W / 2, SNAKE_W / 2))?; |
hdc.RoundRect(rect, SIZE::new(SNAKE_W / 2, SNAKE_W / 2))?; |
||
} |
} |