index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff
author y2w8 <yousefalharby416@gmail.com> 2026-03-17 16:02:19 +0300
committer y2w8 <yousefalharby416@gmail.com> 2026-03-17 16:02:19 +0300
commit ad342729cc4588ac8af513ddc87abf0ba3f56d9f [patch]
tree 1c73bd0092a1c621f5c81e9e7ada2fb83d577863
parent f8e8a55003143adb2fd9382a38e68bf130396625
download main.tar.gz

fmt & better names. HEAD main

Diffstat

-rw-r--r-- src/app.rs 16
-rw-r--r-- src/game_context.rs 2
-rw-r--r-- src/main.rs 2
-rw-r--r-- src/renderer.rs 9
-rw-r--r-- src/ui.rs 0
-rw-r--r-- src/ui/button.rs 60
-rw-r--r-- src/ui/mod.rs 55
-rw-r--r-- src/ui/text.rs 44
-rw-r--r-- src/ui_components/button.rs 56
-rw-r--r-- src/ui_components/mod.rs 45
-rw-r--r-- src/ui_components/text.rs 35
 11 files changed, 175 insertions(+), 149 deletions(-)

diff --git a/src/app.rs b/src/app.rs
index b7e1cf3..1447afe 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -70,17 +70,17 @@
                            },
                            _ => {}
                        },
                        GameState::Paused => match keycode {
                            Keycode::Q => break 'running,
                            _ => {}
                        },
                        GameState::GameOver => match keycode {
                            Keycode::R => {
                        GameState::Paused => {
                            if keycode == Keycode::Q {
                                break 'running;
                            }
                        }
                        GameState::GameOver => {
                            if keycode == Keycode::R {
                                self.change_state(GameState::Playing);
                                self.game_context.restart();
                            }
                            _ => {}
                        },
                        }
                    },
                    _ => {}
                }
diff --git a/src/game_context.rs b/src/game_context.rs
index 7109b05..3946fd3 100644
--- a/src/game_context.rs
+++ b/src/game_context.rs
@@ -1,7 +1,7 @@
use rand::Rng;
use std::ops::Add;

use crate::{renderer::Renderer};
use crate::renderer::Renderer;

#[derive(Copy, Clone, PartialEq)]
pub enum GameState {
diff --git a/src/main.rs b/src/main.rs
index d283772..5f288e0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,7 @@
mod app;
mod game_context;
mod renderer;
mod ui_components;
mod ui;

fn main() -> Result<(), String> {
    let mut app: App = App::new()?;
diff --git a/src/renderer.rs b/src/renderer.rs
index 7938ad6..c1df5dc 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,8 +1,8 @@
use sdl2::{pixels::Color, rect::Rect, ttf};

use crate::{
    app::App,
    game_context::{GameContext, GameState, Point}, ui_components::Ui,
    game_context::{GameContext, GameState, Point},
    ui::Ui,
};

pub struct Renderer {
@@ -47,7 +47,10 @@
    }

    pub fn get_center(&mut self) -> (i32, i32) {
        (((Renderer::GRID_X_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32, ((Renderer::GRID_Y_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32)
        (
            ((Renderer::GRID_X_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
            ((Renderer::GRID_Y_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
        )
    }

    pub fn draw(&mut self, game_context: &GameContext) -> Result<(), String> {
diff --git a/src/ui.rs b/src/ui.rs
deleted file mode 100644
index e69de29..0000000 100644
--- a/src/ui.rs
+++ /dev/null
diff --git a/src/ui/button.rs b/src/ui/button.rs
new file mode 100644
index 0000000..13954a3 100644
--- /dev/null
+++ b/src/ui/button.rs
@@ -1,0 +1,60 @@
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, TextureQuery};
use sdl2::ttf;
use sdl2::video::Window;

use crate::game_context::GameEvent;

pub struct Button {
    pub rect: Rect,
    pub color: Color,
    pub text: String,
    pub event: GameEvent,
}

impl Button {
    pub fn new(x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> Self {
        Self {
            rect: Rect::new(x, y, width, height),
            color: Color::RGB(0, 120, 215),
            text: text.to_string(),
            event,
        }
    }

    pub fn draw(
        &mut self,
        canvas: &mut Canvas<Window>,
        font: &ttf::Font<'static, 'static>,
    ) -> Result<(), String> {
        canvas.set_draw_color(self.color);
        canvas.fill_rect(self.rect)?;

        let surface = font
            .render(&self.text)
            .blended(Color::RGB(255, 255, 255))
            .map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())?;

        let TextureQuery { width, height, .. } = texture.query();

        let text_rect = Rect::new(
            self.rect.x() + (self.rect.width() as i32 - width as i32) / 2,
            self.rect.y() + (self.rect.height() as i32 - height as i32) / 2,
            width,
            height,
        );

        canvas.copy(&texture, None, Some(text_rect))?;
        Ok(())
    }

    pub fn is_clicked(&self, mouse_x: i32, mouse_y: i32) -> bool {
        self.rect.contains_point((mouse_x, mouse_y))
    }
}
diff --git a/src/ui/mod.rs b/src/ui/mod.rs
new file mode 100644
index 0000000..474e9f3 100644
--- /dev/null
+++ b/src/ui/mod.rs
@@ -1,0 +1,55 @@
use sdl2::{pixels::Color, rect::Rect};

use crate::{
    game_context::{GameEvent, Point},
    ui::{button::Button, text::Text},
};

pub mod button;
pub mod text;

pub struct Ui {
    pub buttons: Vec<Button>,
    pub texts: Vec<Text>,
}

impl Ui {
    pub fn new() -> Self {
        Self {
            buttons: vec![],
            texts: vec![],
        }
    }

    pub fn button(
        &mut self,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
        text: &str,
        event: GameEvent,
    ) -> &mut Button {
        self.buttons.push(Button {
            rect: Rect::new(x, y, width, height),
            color: Color::RGB(0, 120, 215),
            text: text.to_string(),
            event,
        });
        self.buttons.last_mut().unwrap()
    }

    pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
        self.texts.push(Text {
            text: text.to_string(),
            color,
            point,
        });
        self.texts.last_mut().unwrap()
    }

    pub fn clear(&mut self) {
        self.buttons.clear();
        self.texts.clear();
    }
}
diff --git a/src/ui/text.rs b/src/ui/text.rs
new file mode 100644
index 0000000..39332b9 100644
--- /dev/null
+++ b/src/ui/text.rs
@@ -1,0 +1,44 @@
use sdl2::{pixels::Color, rect::Rect, render::Canvas, ttf, video::Window};

use crate::game_context::Point;

pub struct Text {
    pub text: String,
    pub color: Color,
    pub point: Point,
}

impl Text {
    pub fn new(text: &str, color: Color, point: Point) -> Self {
        Self {
            text: text.to_string(),
            color,
            point,
        }
    }

    pub fn draw(
        &mut self,
        canvas: &mut Canvas<Window>,
        font: &ttf::Font<'static, 'static>,
    ) -> Result<(), String> {
        let surface = font
            .render(&self.text)
            .blended(self.color)
            .map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())?;

        let target = Rect::new(
            self.point.0,
            self.point.1,
            surface.width(),
            surface.height(),
        );
        canvas.copy(&texture, None, Some(target))?;
        Ok(())
    }
}
diff --git a/src/ui_components/button.rs b/src/ui_components/button.rs
deleted file mode 100644
index b755445..0000000 100644
--- a/src/ui_components/button.rs
+++ /dev/null
@@ -1,56 +1,0 @@
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, TextureQuery};
use sdl2::ttf;
use sdl2::video::Window;

use crate::game_context::GameEvent;

pub struct Button {
    pub rect: Rect,
    pub color: Color,
    pub text: String,
    pub event: GameEvent
}

impl Button {
    pub fn new(x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> Self {
        Self {
            rect: Rect::new(x, y, width, height),
            color: Color::RGB(0, 120, 215),
            text: text.to_string(),
            event,
        }
    }

    pub fn draw(&mut self, canvas: &mut Canvas<Window>, font: &ttf::Font<'static, 'static>) -> Result<(), String> {
        canvas.set_draw_color(self.color);
        canvas.fill_rect(self.rect)?;

        let surface = font
            .render(&self.text)
            .blended(Color::RGB(255, 255, 255))
            .map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())?;

        let TextureQuery { width, height, .. } = texture.query();

        let text_rect = Rect::new(
            self.rect.x() + (self.rect.width() as i32 - width as i32) / 2,
            self.rect.y() + (self.rect.height() as i32 - height as i32) / 2,
            width,
            height,
        );

        canvas.copy(&texture, None, Some(text_rect))?;
        Ok(())
    }

    pub fn is_clicked(&self, mouse_x: i32, mouse_y: i32) -> bool {
        self.rect.contains_point((mouse_x, mouse_y))
    }
}
diff --git a/src/ui_components/mod.rs b/src/ui_components/mod.rs
deleted file mode 100644
index 471175d..0000000 100644
--- a/src/ui_components/mod.rs
+++ /dev/null
@@ -1,45 +1,0 @@
use sdl2::{pixels::Color, rect::Rect};

use crate::{game_context::{GameEvent, Point}, ui_components::{button::Button, text::Text}};

pub mod button;
pub mod text;

pub struct Ui {
    pub buttons: Vec<Button>,
    pub texts: Vec<Text>,
}

impl Ui {
    pub fn new() -> Self {
        Self {
            buttons: vec![],
            texts: vec![],
        }
    }

    pub fn button(&mut self, x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> &mut Button {
        self.buttons.push(Button {
            rect: Rect::new(x, y, width, height),
            color: Color::RGB(0, 120, 215),
            text: text.to_string(),
            event
        });
        self.buttons.last_mut().unwrap()
    }

    pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
        self.texts.push(Text {
            text: text.to_string(),
            color,
            point

        });
        self.texts.last_mut().unwrap()
    }

    pub fn clear(&mut self) {
        self.buttons.clear();
        self.texts.clear();
    }
}
diff --git a/src/ui_components/text.rs b/src/ui_components/text.rs
deleted file mode 100644
index 5c63b3d..0000000 100644
--- a/src/ui_components/text.rs
+++ /dev/null
@@ -1,35 +1,0 @@
use sdl2::{pixels::Color, rect::Rect, ttf, video::Window, render::Canvas};

use crate::{game_context::Point};

pub struct Text {
    pub text: String,
    pub color: Color,
    pub point: Point,
}

impl Text {
    pub fn new(text: &str, color: Color, point: Point) -> Self {
        Self {
            text: text.to_string(),
            color,
            point,
        }
    }

    pub fn draw(&mut self, canvas: &mut Canvas<Window>, font: &ttf::Font<'static, 'static>) -> Result<(), String> {
        let surface = font
            .render(&self.text)
            .blended(self.color)
            .map_err(|e| e.to_string())?;

        let texture_creator = canvas.texture_creator();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())?;

        let target = Rect::new(self.point.0, self.point.1, surface.width(), surface.height());
        canvas.copy(&texture, None, Some(target))?;
        Ok(())
    }
}