(&mut self, random: u8)
| 37 | } |
| 38 | } |
| 39 | pub fn tick(&mut self, random: u8) { |
| 40 | if self.game_over { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | let (x, y) = self.head; |
| 45 | let oldhead = self.head; |
| 46 | self.head = match self.direction { |
| 47 | // (0, 0) is at the top right corner |
| 48 | HeadDirection::Right => (x - 1, y), |
| 49 | HeadDirection::Left => (x + 1, y), |
| 50 | HeadDirection::Down => (x, y + 1), |
| 51 | HeadDirection::Up => (x, y - 1), |
| 52 | }; |
| 53 | let (x, y) = self.head; |
| 54 | let width = WIDTH as i8; |
| 55 | let height = HEIGHT as i8; |
| 56 | |
| 57 | if self.body.contains(&self.head) { |
| 58 | // Ran into itself |
| 59 | self.game_over = true |
| 60 | } else if x >= width || x < 0 || y >= height || y < 0 { |
| 61 | // Hit an edge |
| 62 | if WRAP_ENABLE { |
| 63 | self.head = if x >= width { |
| 64 | (0, y) |
| 65 | } else if x < 0 { |
| 66 | (width - 1, y) |
| 67 | } else if y >= height { |
| 68 | (x, 0) |
| 69 | } else if y < 0 { |
| 70 | (x, height - 1) |
| 71 | } else { |
| 72 | (x, y) |
| 73 | }; |
| 74 | } else { |
| 75 | self.game_over = true |
| 76 | } |
| 77 | } else if self.head == self.food { |
| 78 | // Eating food and growing |
| 79 | self.body.insert(0, oldhead).unwrap(); |
| 80 | self.food = place_food(random); |
| 81 | } else if !self.body.is_empty() { |
| 82 | // Move body along |
| 83 | self.body.pop(); |
| 84 | self.body.insert(0, oldhead).unwrap(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn handle_control(&mut self, arg: &GameControlArg) { |
| 89 | match arg { |
no test coverage detected