Hi all,
Some code I would like to share you, give a try and it is a fun =)
In toml :
[package]
name = "animated_eyes"
version = "0.1.0"
edition = "2024"
[dependencies]
macroquad = "0.4"
and here is the code :
use macroquad::prelude::*;
//const WIDTH: f32 = 800.0;
//const HEIGHT: f32 = 600.0;
struct Eye {
x: f32,
y: f32,
radius: f32,
blink: f32,
}
fn draw_eye(eye: &Eye, mx: f32, my: f32) {
let dx = mx - eye.x;
let dy = my - eye.y;
let dist = (dx * dx + dy * dy).sqrt();
let limit = eye.radius - 12.0;
let mut px = 0.0;
let mut py = 0.0;
if dist > 0.0 {
px = dx / dist * limit;
py = dy / dist * limit;
}
// Black eye border
draw_circle(
eye.x,
eye.y,
eye.radius + 3.0,
BLACK,
);
// Blink animation
if eye.blink > 0.0 {
// Closed eyelid
draw_rectangle(
eye.x - eye.radius,
eye.y - eye.radius,
eye.radius * 2.0,
eye.radius * 2.0,
Color::from_rgba(255,220,180,255),
);
draw_line(
eye.x - eye.radius,
eye.y,
eye.x + eye.radius,
eye.y,
2.0,
Color::from_rgba(80,40,20,255),
);
} else {
// White eye
draw_circle(
eye.x,
eye.y,
eye.radius,
WHITE,
);
// Iris
draw_circle(
eye.x + px,
eye.y + py,
18.0,
Color::from_rgba(70,150,255,255),
);
// Pupil
draw_circle(
eye.x + px,
eye.y + py,
10.0,
BLACK,
);
// Reflection
draw_circle(
eye.x + px - 4.0,
eye.y + py - 4.0,
3.0,
WHITE,
);
}
}
#[macroquad::main("Animated Eyes")]
async fn main() {
let mut left_eye = Eye {
x: 300.0,
y: 250.0,
radius: 40.0,
blink: 0.0,
};
let mut right_eye = Eye {
x: 500.0,
y: 250.0,
radius: 40.0,
blink: 0.0,
};
let mut blink_timer: i32 = 0;
let mut blinking = false;
loop {
clear_background(
Color::from_rgba(220,230,255,255)
);
let (mx,my) = mouse_position();
let dist_left =
((mx-left_eye.x).powi(2)
+(my-left_eye.y).powi(2))
.sqrt();
let dist_right =
((mx-right_eye.x).powi(2)
+(my-right_eye.y).powi(2))
.sqrt();
// Start blinking when mouse gets close
if (dist_left < 100.0 || dist_right < 100.0)
&& !blinking
{
blink_timer = 60;
blinking = true;
}
if blinking {
blink_timer -= 1;
if blink_timer < 20 {
left_eye.blink = 1.0;
right_eye.blink = 1.0;
}
if blink_timer <= 0 {
left_eye.blink = 0.0;
right_eye.blink = 0.0;
blinking = false;
}
}
// Face
draw_circle(
400.0,
300.0,
180.0,
Color::from_rgba(255,220,180,255)
);
// Eyebrows
draw_line(
250.0,
190.0,
350.0,
205.0,
3.0,
Color::from_rgba(70,40,20,255),
);
draw_line(
450.0,
205.0,
550.0,
190.0,
3.0,
Color::from_rgba(70,40,20,255),
);
// Nose
draw_line(
400.0,
270.0,
390.0,
330.0,
3.0,
Color::from_rgba(120,80,50,255),
);
draw_line(
390.0,
330.0,
410.0,
330.0,
3.0,
Color::from_rgba(120,80,50,255),
);
// Mouth
draw_circle(
400.0,
390.0,
45.0,
Color::from_rgba(200,50,50,255),
);
draw_rectangle(
355.0,
390.0,
90.0,
50.0,
Color::from_rgba(220,230,255,255),
);
draw_eye(&left_eye,mx,my);
draw_eye(&right_eye,mx,my);
// Mouse crosshair
/*draw_line(
mx-15.0,
my,
mx+15.0,
my,
2.0,
RED
);
draw_line(
mx,
my-15.0,
mx,
my+15.0,
2.0,
RED
);
draw_circle(
mx,
my,
3.0,
YELLOW
);*/
if is_key_pressed(KeyCode::Escape) {
break;
}
next_frame().await;
}
}
Enjoy !
Happy coding !