From 14bc87ae57ecbb054f7e4aa26162bdf45070c769 Mon Sep 17 00:00:00 2001 From: t10blue <2653110209@qq.com> Date: Sat, 20 Jul 2024 21:04:20 +0800 Subject: [PATCH 1/2] test pull request, add food type, different food type represent different color and different socres --- src/lib.rs | 23 ++++++++++++++++++----- www/package.json | 4 ++-- www/src/game-manager.js | 2 +- www/src/view.js | 18 ++++++++++++++++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 36a2d9c..1ecfb27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,12 @@ impl Vector { self.x * other.x + self.y * other.y } } +#[derive(Copy, Clone)] +#[wasm_bindgen] +pub struct Food{ + pub coord: Vector, + pub food_type: i8, +} pub struct Segment<'a> { pub start: &'a Vector, @@ -97,6 +103,12 @@ impl<'a> Segment<'a> { are_equal(self.length(), first.length() + second.length()) } + pub fn is_point_inside_food(&self, food: &Food) -> bool { + let first = Segment::new(self.start, &food.coord); + let second = Segment::new(&food.coord, self.end); + are_equal(self.length(), first.length() + second.length()) + } + pub fn get_projected_point(&self, point: &Vector) -> Vector { let vector = self.get_vector(); let diff = point.subtract(&self.start); @@ -113,7 +125,7 @@ fn get_segments_from_vectors(vectors: &[Vector]) -> Vec { .collect::>() } -fn get_food(width: i32, height: i32, snake: &[Vector]) -> Vector { +fn get_food(width: i32, height: i32, snake: &[Vector]) -> Food { let segments = get_segments_from_vectors(snake); let mut free_positions: Vec = Vec::new(); for x in 0..width { @@ -125,7 +137,7 @@ fn get_food(width: i32, height: i32, snake: &[Vector]) -> Vector { } } let index = rand::thread_rng().gen_range(0, free_positions.len()); - free_positions[index] + Food{coord: free_positions[index], food_type: rand::thread_rng().gen_range(0, 6) % 4} } #[wasm_bindgen] @@ -143,7 +155,7 @@ pub struct Game { pub speed: f64, snake: Vec, pub direction: Vector, - pub food: Vector, + pub food: Food, pub score: i32, } @@ -268,14 +280,15 @@ impl Game { let snake_len = self.snake.len(); let head_segment = Segment::new(&self.snake[snake_len - 2], &self.snake[snake_len - 1]); - if head_segment.is_point_inside(&self.food) { + if head_segment.is_point_inside_food(&self.food) { let tail_end = &self.snake[0]; let before_tail_end = &self.snake[1]; let tail_segment = Segment::new(before_tail_end, &tail_end); let new_tail_end = tail_end.add(&tail_segment.get_vector().normalize()); self.snake[0] = new_tail_end; + self.score += self.food.food_type as i32 + 1; self.food = get_food(self.width, self.height, &self.snake); - self.score += 1; + } } diff --git a/www/package.json b/www/package.json index 28ada13..88874cc 100644 --- a/www/package.json +++ b/www/package.json @@ -7,8 +7,8 @@ "create-wasm-app": ".bin/create-wasm-app.js" }, "scripts": { - "build": "webpack --config webpack.config.js", - "start": "webpack-dev-server", + "build": "SET NODE_OPTIONS=--openssl-legacy-provider && webpack --config webpack.config.js", + "start": "SET NODE_OPTIONS=--openssl-legacy-provider && webpack-dev-server", "deploy": "gh-pages -d dist" }, "repository": { diff --git a/www/src/game-manager.js b/www/src/game-manager.js index ec5afa5..70673e4 100644 --- a/www/src/game-manager.js +++ b/www/src/game-manager.js @@ -1,4 +1,4 @@ -import { Game, Vector } from 'wasm-snake-game' +import { Game, Vector, Food} from 'wasm-snake-game' import CONFIG from './config' import { View } from './view' diff --git a/www/src/view.js b/www/src/view.js index 3c0a440..5eca1c4 100644 --- a/www/src/view.js +++ b/www/src/view.js @@ -58,7 +58,8 @@ export class View { ) this.context.globalAlpha = 1 - const projectedFood = this.projectPosition(food) + const projectedFood = this.projectPosition(food.coord) + this.context.beginPath() this.context.arc( projectedFood.x, @@ -67,7 +68,20 @@ export class View { 0, 2 * Math.PI ) - this.context.fillStyle = '#e74c3c' + switch(food.food_type){ + case 0: + this.context.fillStyle = '#e74c3c'; + break; + case 1: + this.context.fillStyle = '#green'; + break; + case 2: + this.context.fillStyle = '#white'; + break; + case 3: + this.context.fillStyle = 'yellow'; + break; + } this.context.fill() this.context.lineWidth = this.unitOnScreen From 1fbeee459c80870ee222a8306a2727a4d01923cb Mon Sep 17 00:00:00 2001 From: t10blue <2653110209@qq.com> Date: Sat, 20 Jul 2024 21:24:39 +0800 Subject: [PATCH 2/2] fix bugs --- www/src/view.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/src/view.js b/www/src/view.js index 5eca1c4..7b266f4 100644 --- a/www/src/view.js +++ b/www/src/view.js @@ -73,10 +73,10 @@ export class View { this.context.fillStyle = '#e74c3c'; break; case 1: - this.context.fillStyle = '#green'; + this.context.fillStyle = 'green'; break; case 2: - this.context.fillStyle = '#white'; + this.context.fillStyle = 'white'; break; case 3: this.context.fillStyle = 'yellow';