AeroOS / kernel /src /apps /snake.rs
Jaswanth-2008's picture
Upload AeroOS complete Rust source code
dcbeeb2 verified
Raw History Blame Contribute Delete
6.26 kB
extern crate alloc;
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;
use pc_keyboard::DecodedKey;
use crate::graphics::color::Color;
use crate::graphics::surface::Surface;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
pub struct SnakeApp {
pub body: Vec<(i32, i32)>,
pub dir: Direction,
pub next_dir: Direction,
pub food: (i32, i32),
pub score: u32,
pub high_score: u32,
pub game_over: bool,
pub frame_counter: u32,
}
impl SnakeApp {
pub const GRID_W: i32 = 22;
pub const GRID_H: i32 = 16;
pub const TILE_SIZE: usize = 16;
pub fn new() -> Self {
Self {
body: vec![(10, 8), (9, 8), (8, 8)],
dir: Direction::Right,
next_dir: Direction::Right,
food: (16, 8),
score: 0,
high_score: 0,
game_over: false,
frame_counter: 0,
}
}
pub fn reset(&mut self) {
if self.score > self.high_score {
self.high_score = self.score;
}
self.body = vec![(10, 8), (9, 8), (8, 8)];
self.dir = Direction::Right;
self.next_dir = Direction::Right;
self.food = (16, 8);
self.score = 0;
self.game_over = false;
self.frame_counter = 0;
}
pub fn handle_key(&mut self, key: DecodedKey) {
match key {
DecodedKey::Unicode('w') | DecodedKey::Unicode('W') => {
if self.dir != Direction::Down {
self.next_dir = Direction::Up;
}
}
DecodedKey::Unicode('s') | DecodedKey::Unicode('S') => {
if self.dir != Direction::Up {
self.next_dir = Direction::Down;
}
}
DecodedKey::Unicode('a') | DecodedKey::Unicode('A') => {
if self.dir != Direction::Right {
self.next_dir = Direction::Left;
}
}
DecodedKey::Unicode('d') | DecodedKey::Unicode('D') => {
if self.dir != Direction::Left {
self.next_dir = Direction::Right;
}
}
DecodedKey::Unicode('r') | DecodedKey::Unicode('R') | DecodedKey::Unicode(' ') => {
if self.game_over {
self.reset();
}
}
_ => {}
}
}
pub fn tick(&mut self) {
if self.game_over {
return;
}
self.frame_counter += 1;
if self.frame_counter % 8 != 0 {
return; // Speed control
}
self.dir = self.next_dir;
let (head_x, head_y) = self.body[0];
let new_head = match self.dir {
Direction::Up => (head_x, head_y - 1),
Direction::Down => (head_x, head_y + 1),
Direction::Left => (head_x - 1, head_y),
Direction::Right => (head_x + 1, head_y),
};
// Collision with walls
if new_head.0 < 0 || new_head.0 >= Self::GRID_W || new_head.1 < 0 || new_head.1 >= Self::GRID_H {
self.game_over = true;
return;
}
// Collision with self
if self.body.contains(&new_head) {
self.game_over = true;
return;
}
self.body.insert(0, new_head);
// Check food eat
if new_head == self.food {
self.score += 10;
// Spawn next food
let next_x = ((self.food.0 * 7 + 11) % (Self::GRID_W - 2)) + 1;
let next_y = ((self.food.1 * 13 + 5) % (Self::GRID_H - 2)) + 1;
self.food = (next_x, next_y);
} else {
self.body.pop();
}
}
pub fn render(&mut self, surface: &mut Surface, x: i32, y: i32, w: usize, h: usize) {
self.tick();
surface.draw_rect(x, y, w, h, Color::rgb(22, 24, 32));
// Score header bar
surface.draw_rect(x, y, w, 28, Color::rgb(30, 33, 44));
let score_text = format!("Score: {} | Best: {}", self.score, self.high_score);
surface.draw_text(&score_text, x + 12, y + 6, Color::WHITE);
surface.draw_text("Controls: [W,A,S,D]", x + w as i32 - 160, y + 6, Color::TEXT_MUTED);
// Arena boundary
let arena_x = x + 10;
let arena_y = y + 36;
let arena_w = (Self::GRID_W as usize) * Self::TILE_SIZE;
let arena_h = (Self::GRID_H as usize) * Self::TILE_SIZE;
surface.draw_rounded_rect(arena_x, arena_y, arena_w, arena_h, 6, Color::rgb(14, 16, 22));
surface.draw_rect_outline(arena_x, arena_y, arena_w, arena_h, Color::rgba(255, 255, 255, 25), 1);
// Draw food (glowing red apple)
let fx = arena_x + (self.food.0 * Self::TILE_SIZE as i32);
let fy = arena_y + (self.food.1 * Self::TILE_SIZE as i32);
surface.draw_rounded_rect(fx + 2, fy + 2, Self::TILE_SIZE - 4, Self::TILE_SIZE - 4, 4, Color::TRAFFIC_CLOSE);
// Draw snake segments
for (i, &(sx, sy)) in self.body.iter().enumerate() {
let px = arena_x + (sx * Self::TILE_SIZE as i32);
let py = arena_y + (sy * Self::TILE_SIZE as i32);
let color = if i == 0 {
Color::rgb(0, 230, 118) // Bright green head
} else {
Color::rgb(46, 125, 50) // Body
};
surface.draw_rounded_rect(px + 1, py + 1, Self::TILE_SIZE - 2, Self::TILE_SIZE - 2, 3, color);
}
// Game Over Banner
if self.game_over {
let box_w = 200;
let box_h = 70;
let bx = arena_x + ((arena_w as i32 - box_w) / 2);
let by = arena_y + ((arena_h as i32 - box_h) / 2);
surface.draw_drop_shadow(bx, by, box_w as usize, box_h as usize, 8, 8);
surface.draw_rounded_rect(bx, by, box_w as usize, box_h as usize, 8, Color::rgba(30, 32, 44, 240));
surface.draw_rect_outline(bx, by, box_w as usize, box_h as usize, Color::TRAFFIC_CLOSE, 1);
surface.draw_text("GAME OVER!", bx + 55, by + 14, Color::TRAFFIC_CLOSE);
surface.draw_text("Press 'R' or Space to Play", bx + 16, by + 40, Color::WHITE);
}
}
}