Snake: Difference between revisions
→{{header|Rust}}: minimization and renaming + screenshot
(Integer BASIC) |
(→{{header|Rust}}: minimization and renaming + screenshot) |
||
Line 4,602:
=={{header|Rust}}==
Implemented smooth (per-pixel) animation on Win32 API (tested on Windows 7 and Windows 11)
<syntaxhighlight lang="rust">/* add to file Cargo.toml:
[dependencies]
winsafe = "0.0.8"
rand = "0.8
derive-new = "0.5"
*/
Line 4,620 ⟶ 4,614:
use rand::Rng;
use std::{cell::RefCell, rc::Rc};
use winsafe::{co, gui, prelude::*, COLORREF, HBRUSH, HPEN,
const STEP: i32 = 3; // px, motion per frame
const
const FIELD_W: i32 = 20; // width of the square field in CELLs▼
const SNAKE_W: i32 = 20; // px
const
▲/// total field width (with overlap for collisions) in STEPs
#[rustfmt::skip]
#[derive(new)]
struct Context {
#[new(default) ] snake: Vec<i32>, // [
#[new(value = "[
#[new(default) ] incr: i32, // 0 | -1 | 1 | -TW | TW - increment r[head] in next STEP
#[new(value = "TW") ] next_incr: i32, // `incr` in the next grid cell
#[new(default) ] gap: i32, //
}
pub fn main() {
let context = Rc::new(RefCell::new(Context::new(HWND::NULL, Vec::new())));▼
let grid: Vec<_> = (1..=
▲ (1..=FIELD_W).flat_map(|y| (1..=FIELD_W).map(move |x| (y * TW + x) * RATIO)).collect();
▲ let [bg, tail, turn, body, food, head] = [0usize, 1, 2, 3, 4, 5];
let mut colors = [(0x00, 0xF0, 0xA0); 6]; // color tail, turn, body
colors[bg] = (0x00, 0x50, 0x90);
Line 4,672 ⟶ 4,645:
let wnd = gui::WindowMain::new(gui::WindowMainOpts {
title: "Snake - Start: Space, then press W-A-S-D".to_string(),
size: winsafe::SIZE::new(
class_bg_brush: brushes[bg],
..Default::default()
});
wnd.on().wm_paint({
▲ let wnd = wnd.clone(); // WindowMain is based on Arc, so wnd.clone() is a shallow copy
let context = Rc::clone(&context);
move || {
let
ctx.hwnd = wnd.hwnd();▼
let mut ps = winsafe::PAINTSTRUCT::default();
let hdc = ctx.wnd.hwnd().BeginPaint(&mut ps)?;
hdc.SelectObjectPen(HPEN::CreatePen(co::PS::NULL, 0, COLORREF::new(0, 0, 0))?)?;
for (&
hdc.SelectObjectBrush(brush)?;
let left =
let top =
let rect = RECT { left, top, right: left + SNAKE_W, bottom: top + SNAKE_W };
hdc.RoundRect(rect,
}
Ok(ctx.wnd.hwnd().EndPaint(&ps)
}
});
Line 4,703 ⟶ 4,672:
wnd.on().wm_key_down({
let context = Rc::clone(&context);
move |
let mut ctx = context.borrow_mut();
Ok(match (ctx.
(
(TW, bt @ (b'A' | b'D')) => ctx.next_incr = if bt ==
(1, bt @ (b'S' | b'W'))
ctx.hwnd.SetTimer(1, 1000 / FPS, None)?;▼
_ => (),
})
}
});
Line 4,723 ⟶ 4,685:
wnd.on().wm_timer(1, move || {
let mut ctx = context.borrow_mut();
let new_h = ctx.
(ctx.
if ctx.gap < 0 {
ctx.
ctx.
ctx.
}
ctx.gap -= ctx.gap.signum();
if ctx.gap == 0 {
let
if !eat && (cells.binary_search(&new_h).is_err() || snk_cells.contains(&&new_h)) {▼
▲
ctx.wnd.hwnd().SetWindowText(&(title + ". Restart: Space"))?;
return Ok(());
▲ hw.SetWindowText(&format!("Snake - Eaten: {}.", snk_cells.len()))?;
▲ hw.SetWindowText(&(hw.GetWindowText()? + " ALL !!!"))?;
▲ snk_cells.sort();
▲ .nth(rand::thread_rng().gen_range(0..cells.len() - snk_cells.len() - 1))
}
ctx.
}
ctx.snake.push(new_h);
ctx.wnd.hwnd().InvalidateRect(None,
Ok(())
});
}</syntaxhighlight>
{{out}}
[[File:Snake rust.png|thumb]]
=={{header|Sidef}}==
|