1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use opengl_graphics::GlGraphics;
use piston::input::RenderArgs;

use super::snake::Snake;

pub struct Food {
    pub x: u32,
    pub y: u32,
}

impl Food {
    // Return true if snake ate food this update
    pub fn update(&mut self, s: &Snake) -> bool {
        let front = s.snake_parts.front().unwrap();
        front.0 == self.x && front.1 == self.y
    }

    pub fn render(&mut self, gl: &mut GlGraphics, args: &RenderArgs, width: u32) {
        const RED: [f32; 4] = [0.92, 0.10, 0.14, 1.0];

        let x = self.x * width;
        let y = self.y * width;

        let square = graphics::rectangle::square(x as f64, y as f64, width as f64);

        gl.draw(args.viewport(), |c, gl| {
            let transform = c.transform;

            graphics::rectangle(RED, square, transform, gl)
        });
    }
}