Snake: Difference between revisions
Content deleted Content added
Basicgames (talk | contribs) |
m →{{header|Rust}}: mini refactoring |
||
Line 4,227: | Line 4,227: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Implemented smooth (per-pixel) animation on Win32 API (tested on Windows 7) |
Implemented smooth (per-pixel) animation on Win32 API (tested on Windows 7 and Windows 11) |
||
Used winsafe - a safe rust bindings library for Win32 GUI: young but very handy, with links to docs.microsoft.com from doc and src for all Win32 entities involved. |
Used winsafe - a safe rust bindings library for Win32 GUI: young but very handy, with links to docs.microsoft.com from doc and src for all Win32 entities involved. |
||
Line 4,271: | Line 4,271: | ||
wnd: gui::WindowMain, |
wnd: gui::WindowMain, |
||
snake: Vec<i32>, // [ids_rect] where id_rect = y * TW + x (where x, y: nSTEPs) |
snake: Vec<i32>, // [ids_rect] where id_rect = y * TW + x (where x, y: nSTEPs) |
||
id_r: [i32; 6], |
id_r: [i32; 6], // ID 6 rectangles to color in next frame (bg, tail, turn, body, food, head) |
||
gap: i32, |
gap: i32, // gap in STEPs between animation and logic cell (negative - remove tail) |
||
dir: Direction, |
dir: Direction, |
||
ordered_dir: Direction, |
ordered_dir: Direction, |
||
Line 4,343: | Line 4,343: | ||
ctx.gap -= ctx.gap.signum(); |
ctx.gap -= ctx.gap.signum(); |
||
if ctx.gap == 0 { |
if ctx.gap == 0 { |
||
⚫ | |||
let hw = ctx.wnd.hwnd(); |
let hw = ctx.wnd.hwnd(); |
||
let eat = new_h == ctx.id_r[food]; |
let eat = new_h == ctx.id_r[food]; |
||
let mut snk_cells: Vec<_> = ctx.snake.iter().step_by(RATIO as usize).collect(); |
|||
if !eat && (cells.binary_search(&new_h).is_err() || snk_cells.contains(&&new_h)) { |
|||
hw.KillTimer(1)?; |
hw.KillTimer(1)?; |
||
hw.SetWindowText(&(hw.GetWindowText()? + " Restart: F2 (with save - Space)"))?; |
hw.SetWindowText(&(hw.GetWindowText()? + " Restart: F2 (with save - Space)"))?; |
||
ctx.dir = Start; |
ctx.dir = Start; |
||
return Ok(()); |
return Ok(()); |
||
} else if eat || ctx.id_r[food] == |
} else if eat || ctx.id_r[food] == START_CELL && ctx.id_r[tail] != START_CELL { |
||
if eat { |
|||
hw.SetWindowText(&format!("Snake - Eaten: {}.", snk_cells.len()))?; |
|||
⚫ | |||
⚫ | |||
hw.SetWindowText(&format!("Snake - Eaten: {}.", snk_cells.len()))? |
|||
} |
} |
||
if |
if eat && snk_cells.len() == cells.len() - 2 { |
||
⚫ | |||
ctx.id_r[food] = 0; // hide food if not all of the saved snake has come out or everything is eaten |
|||
ctx.id_r[food] = 0; // hide food: all eaten |
|||
⚫ | |||
snk_cells.sort(); |
snk_cells.sort(); |
||
ctx.id_r[food] = *(cells.iter()) |
ctx.id_r[food] = *(cells.iter()) |
||
Line 4,368: | Line 4,366: | ||
} |
} |
||
} |
} |
||
⚫ | |||
ctx.gap = if eat { RATIO } else { -RATIO } |
ctx.gap = if eat { RATIO } else { -RATIO } |
||
} |
} |