index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff
snake-rs / src / game_context.rs
-rw-r--r-- blob 95b1f84b14f2b052a4dfe15430ef5779e2d6cc36 3831 bytes (plain)
1
use std::ops::Add;
2
use rand::Rng;
3
4
use crate::{game_context, renderer::Renderer};
5
6
pub enum GameState {
7
    Playing,
8
    Paused,
9
}
10
11
#[derive(Copy, Clone, PartialEq)] // أضف هذي السطور
12
pub enum PlayerDirection {
13
    Up,
14
    Down,
15
    Right,
16
    Left,
17
}
18
19
#[derive(Copy, Clone, PartialEq)]
20
pub struct Point(pub i32, pub i32);
21
22
impl Point {
23
    pub fn random() -> Self {
24
        let mut rng = rand::rng();
25
        Point(
26
            rng.random_range(0..Renderer::GRID_X_SIZE as i32), 
27
            rng.random_range(0..Renderer::GRID_X_SIZE as i32)
28
        )
29
    }
30
}
31
32
impl Add<Point> for Point {
33
    type Output = Point;
34
35
    fn add(self, rhs: Point) -> Self::Output {
36
        Point(self.0 + rhs.0, self.1 + rhs.1)
37
    }
38
}
39
40
pub struct GameContext {
41
    pub player_position: Vec<Point>,
42
    pub player_direction: PlayerDirection,
43
    pub food: Vec<Point>,
44
    pub state: GameState,
45
}
46
47
impl GameContext {
48
    pub fn new() -> Result<Self, String> {
49
        Ok(Self {
50
            player_position: vec![Point(3, 1), Point(2, 1), Point(1, 1)],
51
            player_direction: PlayerDirection::Right,
52
            food: vec![Point(3, 3)],
53
            state: GameState::Playing,
54
        })
55
    }
56
57
    pub fn next_tick(&mut self) {
58
        if let GameState::Paused = self.state {
59
            return;
60
        }
61
62
        let back_position = *self.player_position.last().unwrap();
63
        let next_head_position = self.next_head_position(self.player_direction);
64
65
66
        self.player_position.insert(0, next_head_position);
67
68
        if self.food.contains(&next_head_position) {
69
            self.player_position.push(back_position);
70
71
            self.food.retain(|p| *p != next_head_position);
72
73
            self.generate_food();
74
        } else {
75
            self.player_position.pop();
76
        }
77
    }
78
79
    // Food
80
    fn generate_food(&mut self) {
81
        loop {
82
            let new_food = Point::random();
83
            if !self.player_position.contains(&new_food) {
84
                self.food.push(new_food);
85
                break;
86
            }
87
        }
88
    }
89
90
    // Player
91
    pub fn move_up(&mut self) {
92
        let next_head_position = self.next_head_position(PlayerDirection::Up);
93
        if self.player_position.contains(&next_head_position) {
94
            return;
95
        }
96
97
        self.player_direction = PlayerDirection::Up;
98
    }
99
100
    pub fn move_down(&mut self) {
101
        let next_head_position = self.next_head_position(PlayerDirection::Down);
102
        if self.player_position.contains(&next_head_position) {
103
            return;
104
        }
105
106
        self.player_direction = PlayerDirection::Down;
107
    }
108
109
    pub fn move_right(&mut self) {
110
        let next_head_position = self.next_head_position(PlayerDirection::Right);
111
        if self.player_position.contains(&next_head_position) {
112
            return;
113
        }
114
115
        self.player_direction = PlayerDirection::Right;
116
    }
117
118
    pub fn move_left(&mut self) {
119
        let next_head_position = self.next_head_position(PlayerDirection::Left);
120
        if self.player_position.contains(&next_head_position) {
121
            return;
122
        }
123
124
        self.player_direction = PlayerDirection::Left;
125
    }
126
127
    pub fn next_head_position(&mut self, direction: PlayerDirection) -> Point {
128
        let head_position = self.player_position.first().unwrap();
129
        match direction {
130
            PlayerDirection::Up => *head_position + Point(0, -1),
131
            PlayerDirection::Down => *head_position + Point(0, 1),
132
            PlayerDirection::Right => *head_position + Point(1, 0),
133
            PlayerDirection::Left => *head_position + Point(-1, 0),
134
        }
135
    }
136
137
138
    // Game State
139
    pub fn toggle_pause(&mut self) {
140
        self.state = match self.state {
141
            GameState::Playing => GameState::Paused,
142
            GameState::Paused => GameState::Playing,
143
        }
144
    }
145
}