use piston_window::*; use findfolder; trait State{ fn update(&mut…
- User
use piston_window::*; use find_folder;
trait State{ fn update(&mut self,args:&UpdateArgs); fn input(&mut self,button:Button)->Option; fn render(&mut self,c:Context,g:&mut G2d,glyphs:Glyphs); } enum StateCommand{ Push(Box), Pop, None, } struct Mainpage{ circle_x:f64, direction:f64, fps_timer:f64, fps_count:u32, fps:u32, } impl Mainpage { fn new()->Self{ Self { circle_x: 320.0 , direction: 100.0 , fps_timer: 0.0 , fps_count: 0 , fps: 0 } } } struct StateStack{ stack:Vec<Box> }
impl StateStack { fn new(initial_state:Box)->Self{ Self { stack: vec![initial_state] } } fn push(&mut self,state:Box){ self.stack.push(state); } fn pop(&mut self){ self.stack.is_empty(){ return; } self.stack.pop(); if self.stack.is_empty(){ println!("堆疊結束"); } }
fn is_empty(&self)->bool{ self.stack.is_empty() } fn update(&mut self,args:&UpdateArgs){ if let Some(top)=self.stack.last_mut(){ top.update(args); } } fn render(&mut self,c:Context,g:&mut G2d,glyphs:&mut Glyphs){ for state in &mut self.stack{ state.render(c, g, glyphs); } } fn input(&mut self,button:Button)->Option<StateCommand>{ if let Some(top)=self.stack.last_mut(){ if let Some(cmd)=top.input(button){ match cmd { StateCommand::Push(state)=>self.push(state), StateCommand::Pop=>self.pop(), StateCommand::None=>{} } } } }}
struct ModalWindow{ count:usize } impl ModalWindow{ fn new()->Self{ Self { count: 0 } } } impl State for ModalWindow { fn input(&mut self,button:Button)->Option { if let Button::Keyboard(Key)=button{ match Key { Key::Escape=>return Some(StateCommand::Pop), Key::Space=>{ self.count +=1; println!("{}",self.count); return Some(StateCommand::None); } _=>{} } } } fn render(&mut self,c:Context,g:&mut G2d,glyphs:Glyphs) { graphics::rectangle([0.0,0.0,0.0,0.5], [0.0,0.0,640.0,480.0], c.transform, g); let rect =[120.0,140.0,400.0,240.0]; graphics::rectangle([1.0;4],rect,c.transform,g); let text_obj=graphics::text::Text::new_color([0.0,0.0,0.0,1.0], 32); text_obj.draw("這是彈出視窗", glyphs, &graphics::DrawState::default(), c.transform.trans(180.0,200.0), g,).unwrap(); text_obj.draw("按esc,關閉", glyphs, &graphics::DrawState::default(), c.transform.trans(120.0, 240.0), g).unwrap(); let conter_str=format!("按下空白鍵{}",self.count); text_obj.draw(&conter_str, glyphs, graphics::DrawState::default(), c.transform.trans(180.0, 280.0), g).unwrap(); } }
impl State for Mainpage { fn update(&mut self,args:&UpdateArgs) { self.circle_x += self.direction *args.dt; if self.circle_x > 660.0{ self.circle_x=660.0; self.direction= -self.direction; }else if self.circle_x <40.0 { self.circle_x=40.0; self.direction=-self.direction; }; self.fps_timer +=args.dt; self.fps_count +=1; if self.fps_timer >= 1.0 { self.fps=self.fps_count; self.fps_timer -=1.0; self.fps_count =0; }
} fn input(&mut self,button:Button)->Option<StateCommand> { if let Some(Button::Keyboard(Key))=button{ if Key==Key::M{return Some(StateCommand::Push(Box::new(ModalWindow::new())));} } None } fn render(&mut self,c:Context,g:&mut G2d,glyphs:Glyphs) { clear([0.8,0.8,1.0,1.0], g); draw_text_centered("text_str", c, g, glyphs, 240.0); graphics::ellipse([1.0,0.0,0.0,1.0],[self.circle_x-20.0,300.0-20.0,40.0,40.0],c.transform,g,); let fps_text=format!("FPS:{}",self.fps); let text_obj=graphics::text::Text::new_color([0,0,0.0,0.0,1.0], 24); text_obj.draw(&fps_text, glyphs, &graphics::DrawState::default(), c.transform.trans(10.0,460.0), g).unwrap(); } - Assistant
- User
完整程式碼
- Assistant
- User
rror[E0659]:
graphicsis ambiguous --> examples\1_01_super_simple.rs:225:9 | 225 | use graphics::Transformed; | ^^^^^^^^ ambiguous name | = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution = note:graphicscould refer to a crate passed with--extern= help: use::graphicsto refer to this crate unambiguously note:graphicscould also refer to the crate imported here - Assistant
這個錯誤是因為
graphics名稱在你的程式中同時被不同來源引入,產生重複定義造成不明確(ambiguous)。在 piston_window 裡
graphics其實是 re-export 了graphicscrate,所以會衝突。
解決方案:
將
use graphics::Transformed;換成use ::graphics::Transformed;這樣改寫可明確告訴 Rust 你引用的是 root namespace 的外部 crate
graphics,避免與本地模組命名衝突。fn draw_text_centered(text_str: &str, c: Context, g: &mut G2d, glyphs: &mut Glyphs, y: f64) { use ::graphics::Transformed; // ← 在這裡加上 :: 就沒問題 let text_width = text_str.len() as f64 * 24.0; // 粗估字寬 let x = 320.0 - text_width / 2.0; let transform = c.transform.trans(x, y); let text_obj = ::graphics::text::Text::new_color([1.0; 4], 32); text_obj.draw(text_str, glyphs, &::graphics::DrawState::default(), transform, g).unwrap(); }