Skip to content

Commit 253d870

Browse files
committed
change
1 parent 366eba2 commit 253d870

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

src/game.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ pub struct Game<P: PlatformRenderer> {
264264
step_cooldown: f32,
265265
eating_egg: bool,
266266
camera_pos: Vec2<f32>,
267+
eating_timer: f32,
268+
269+
body_start_color: u32,
270+
body_end_color: u32,
267271

268272
snake: Snake,
269273
dead_snake: DeadSnake,
@@ -278,6 +282,7 @@ pub struct Game<P: PlatformRenderer> {
278282
impl<P: PlatformRenderer> Game<P> {
279283
pub fn new(platform_renderer: P) -> Self {
280284
Self {
285+
eating_timer: 0.0,
281286
width: 0,
282287
height: 0,
283288
dir: Direction::Right,
@@ -299,6 +304,8 @@ impl<P: PlatformRenderer> Game<P> {
299304
vels: Vec::new(),
300305
masks: Vec::new(),
301306
},
307+
body_start_color: 0xFF00FF00,
308+
body_end_color: 0xFF0000FF,
302309
}
303310
}
304311

@@ -404,6 +411,14 @@ impl<P: PlatformRenderer> Game<P> {
404411
dt *= self.dt_scale;
405412
}
406413

414+
if self.eating_egg {
415+
self.eating_timer += dt;
416+
if self.eating_timer > 1.0 {
417+
self.eating_egg = false;
418+
self.eating_timer = 0.0;
419+
}
420+
}
421+
407422
match self.state {
408423
State::GamePlay => {
409424
self.step_cooldown -= dt;
@@ -541,6 +556,19 @@ impl<P: PlatformRenderer> Game<P> {
541556
}
542557
}
543558

559+
fn lerp_color(&self, color1: u32, color2: u32, t: f32) -> u32 {
560+
let r1 = (color1 >> 16) & 0xFF;
561+
let g1 = (color1 >> 8) & 0xFF;
562+
let b1 = color1 & 0xFF;
563+
let r2 = (color2 >> 16) & 0xFF;
564+
let g2 = (color2 >> 8) & 0xFF;
565+
let b2 = color2 & 0xFF;
566+
let r = (r1 as f32 + (r2 as f32 - r1 as f32) * t).round() as u32;
567+
let g = (g1 as f32 + (g2 as f32 - g1 as f32) * t).round() as u32;
568+
let b = (b1 as f32 + (b2 as f32 - b1 as f32) * t).round() as u32;
569+
0xFF000000 | (r << 16) | (g << 8) | b
570+
}
571+
544572
fn snake_render(&self) {
545573
let t = self.step_cooldown / STEP_INTERVAL;
546574

@@ -561,19 +589,25 @@ impl<P: PlatformRenderer> Game<P> {
561589
.adjust_2_slide_sides(tail_dir, if self.eating_egg { 1.0 } else { 1.0 - t });
562590

563591
if self.eating_egg {
564-
self.fill_cell(head_cell, EGG_BODY_COLOR, 1.0);
565-
self.fill_cell(
566-
head_cell,
567-
EGG_SPINE_COLOR,
568-
SNAKE_SPINE_THICKNESS_PERCENT * 2.0,
569-
);
592+
// self.fill_cell(head_cell, EGG_BODY_COLOR, 1.0);
593+
// self.fill_cell(
594+
// head_cell,
595+
// EGG_SPINE_COLOR,
596+
// SNAKE_SPINE_THICKNESS_PERCENT * 2.0,
597+
// );
598+
let t = self.eating_timer;
599+
let color = self.lerp_color(EGG_BODY_COLOR, SNAKE_HEAD_COLOR, t.sin()); // 动态颜色
600+
self.fill_cell(head_cell, color, 1.0);
601+
} else {
602+
self.fill_sides(&head_slide_sides, SNAKE_HEAD_COLOR);
570603
}
571604

572-
self.fill_sides(&head_slide_sides, SNAKE_HEAD_COLOR);
573605
self.fill_sides(&tail_slide_sides, SNAKE_TAIL_COLOR);
574606

575607
for i in 1..self.snake.size() - 1 {
576-
self.fill_cell(self.snake.items.get(i).unwrap(), SNAKE_BODY_COLOR, 1.0);
608+
let t = (i - 1) as f32 / (self.snake.size() - 2) as f32;
609+
let color = self.lerp_color(self.body_start_color, self.body_end_color, t);
610+
self.fill_cell(self.snake.items.get(i).unwrap(), color, 1.0);
577611
}
578612

579613
// body spine

0 commit comments

Comments
 (0)