pub fn is_key_pressed(key: KeyCode) -> boolExpand description
Was this key pressed this frame?
Examples found in repository?
examples/soko.rs (line 89)
88 fn update(&mut self) {
89 if is_key_pressed(KeyCode::R) {
90 *self = Self::new();
91 return;
92 }
93
94 let mut movement = Vec2::ZERO;
95 if is_key_pressed(KeyCode::A) || is_key_pressed(KeyCode::Left) {
96 movement.x -= 1.;
97 }
98 if is_key_pressed(KeyCode::D) || is_key_pressed(KeyCode::Right) {
99 movement.x += 1.;
100 }
101 if is_key_pressed(KeyCode::W) || is_key_pressed(KeyCode::Up) {
102 movement.y -= 1.;
103 }
104 if is_key_pressed(KeyCode::S) || is_key_pressed(KeyCode::Down) {
105 movement.y += 1.;
106 }
107
108 let mut collided = false;
109 let position = self.player.position + movement;
110 if self.walls.iter().any(|e| e.position == position) {
111 collided = true;
112 } else if let Some(obj) = self.objects.iter().position(|e| e.position == position) {
113 let position = position + movement;
114 if self.solids().any(|e| e.position == position) {
115 collided = true;
116 } else {
117 self.objects[obj].position = position;
118 }
119 }
120
121 if !collided {
122 self.player.position = position;
123 }
124
125 if !self.won
126 && self
127 .objects
128 .iter()
129 .all(|object| self.targets.iter().any(|e| e.position == object.position))
130 {
131 self.won = true;
132 // you are free!
133 self.walls.clear();
134 }
135
136 self.draw();
137 }