Rust game engine Piston触りかけ
Pistonはイイぞと聞いたので、少し触って見た。cargoによるビルドがとてもおされ。
extern crate piston_window; use piston_window::*; pub struct WinSize{pub width:u32,pub height:u32,} pub struct CubeSize{pub width:f64,pub height:f64,} pub struct Cube{pub x:f64,pub y:f64,} fn main(){ const BLUE:[f32;4]=[0.0,0.5,0.8,0.7]; const GRAY:[f32;4]=[0.15,0.15,0.15,1.0]; let init_window_size=WinSize{width:640,height:640}; let cube_size=CubeSize{width:90.0,height:90.0}; let mut cube_ptr=Cube{x:0.0,y:0.0}; let mut cube_ptr1=Cube{x:(init_window_size.width as f64) - cube_size.width,y:0.0}; let mut window:PistonWindow=WindowSettings::new("Test Window",[init_window_size.width,init_window_size.height]) .exit_on_esc(true) .build() .unwrap_or_else(|e|{panic!("Failed :{}",e)}); let window_size:Size=window.size(); let mut arrive:bool=true; let mut arrive1:bool=true; while let Some(e)=window.next(){ window.draw_2d(&e, |c,g|{ clear(GRAY,g); rectangle(BLUE,[cube_ptr.x,cube_ptr.y,cube_size.width,cube_size.height],c.transform,g); if arrive && (cube_ptr.x != (window_size.width as f64) - cube_size.width || cube_ptr.y != (window_size.height as f64) - cube_size.height) { cube_ptr.x+=1.0; cube_ptr.y+=1.0; if cube_ptr.x == (window_size.width as f64) - cube_size.width || cube_ptr.y == (window_size.height as f64) - cube_size.height { arrive=false; } }else{ cube_ptr.x-=1.0; cube_ptr.y-=1.0; if cube_ptr.x == 0.0 || cube_ptr.y == 0.0 { arrive=true; } } }); window.draw_2d(&e, |c,g|{ rectangle(BLUE,[cube_ptr1.x,cube_ptr1.y,cube_size.width,cube_size.height],c.transform,g); if arrive1 && (cube_ptr1.x != 0.0 || cube_ptr1.y != (window_size.height as f64) - cube_size.height) { cube_ptr1.x-=1.0; cube_ptr1.y+=1.0; if cube_ptr1.x == 0.0 || cube_ptr.y == (window_size.height as f64) - cube_size.height { arrive1=false; } }else{ cube_ptr1.x+=1.0; cube_ptr1.y-=1.0; if cube_ptr1.x == (window_size.width as f64) - cube_size.width || cube_ptr1.y == 0.0 { arrive1=true; } } }); } }
何か久しぶりにPistonを使ってゲームでも作るのも良いかもしれない。asm.jsを使ってブラウザで動かしたりもできる様子。とてもオサレ。