@@ -12,7 +12,6 @@ public class Snake {
1212 public const double DeathAnimDuration = 1500 ;
1313
1414 public readonly Vector2 [ ] CenterCoords ;
15- readonly Dictionary < ( int Col , int Row ) , int > _validCells = [ ] ;
1615
1716 public List < SnakeBodyPart > Parts = [ ] ;
1817 public SnakeBodyPart Head => Parts [ 0 ] ;
@@ -33,7 +32,10 @@ public class Snake {
3332 public double DeathTimer ;
3433 public List < DeathSegment > DeathSegments = [ ] ;
3534 public bool ShowDeathScreen ;
36- public Queue < int > KeyQueue = new ( ) ;
35+ public Queue < int > KeyQueue = new ( 3 ) ;
36+
37+ readonly double _eatDistSq ;
38+ readonly Dictionary < ( int Col , int Row ) , int > _validCells ;
3739
3840 static readonly string [ ] EatColors = [ "#ffe080" , "#ffb840" , "#fff4c0" , "#ff9420" , Neon . TextAccent ] ;
3941 static readonly string [ ] DeathColors = [ Neon . Danger , "#ff6a9d" , Neon . TextAccent , "#8b5dff" , "#ffc2ff" ] ;
@@ -46,6 +48,9 @@ public Snake(int cellSize, int visibleCols, int visibleRows) {
4648 Rows = visibleRows * 5 ;
4749 WorldW = Cols * CellSize ;
4850 WorldH = Rows * CellSize ;
51+ _eatDistSq = CellSize * ( double ) CellSize ;
52+ _validCells = new ( Rows * Cols ) ;
53+
4954 CenterCoords = new Vector2 [ Cols * Rows ] ;
5055 float centerOffset = CellSize * 0.5f ;
5156 for ( int y = 0 ; y < Rows ; y ++ )
@@ -67,8 +72,9 @@ public Snake(int cellSize, int visibleCols, int visibleRows) {
6772 }
6873
6974 int numObstacles = ( int ) ( Cols * Rows * 0.02 ) ;
75+ const int numFood = 50 ;
7076 for ( int i = 0 ; i < numObstacles ; i ++ ) SpawnObstacle ( ) ;
71- for ( int i = 0 ; i < 50 ; i ++ ) SpawnFood ( ) ;
77+ for ( int i = 0 ; i < numFood ; i ++ ) SpawnFood ( ) ;
7278 }
7379
7480 public int GetIndex ( int col , int row ) {
@@ -130,7 +136,7 @@ bool EggBlocked(int x, int y, Egg skipEgg) {
130136 }
131137
132138 void StepEgg ( Egg egg ) {
133- Span < int > order = stackalloc int [ 4 ] { 0 , 1 , 2 , 3 } ;
139+ Span < int > order = [ 0 , 1 , 2 , 3 ] ;
134140 for ( int i = order . Length - 1 ; i > 0 ; i -- ) {
135141 int j = Random . Shared . Next ( i + 1 ) ;
136142 ( order [ i ] , order [ j ] ) = ( order [ j ] , order [ i ] ) ;
@@ -236,6 +242,11 @@ public void Update(double deltaTime, double screenW, double screenH) {
236242 }
237243
238244 if ( Dead ) {
245+ if ( DeathTimer >= DeathAnimDuration ) {
246+ ShowDeathScreen = true ;
247+ return ;
248+ }
249+
239250 DeathTimer += deltaTime ;
240251 double progress = Math . Min ( 1 , DeathTimer / DeathAnimDuration ) ;
241252 for ( int si = 0 ; si < DeathSegments . Count ; si ++ ) {
@@ -246,7 +257,6 @@ public void Update(double deltaTime, double screenW, double screenH) {
246257 seg . Alpha = Math . Max ( 0 , 1 - progress * 0.8 ) ;
247258 DeathSegments [ si ] = seg ;
248259 }
249- if ( DeathTimer >= DeathAnimDuration ) ShowDeathScreen = true ;
250260 return ;
251261 }
252262
@@ -300,11 +310,11 @@ public void Update(double deltaTime, double screenW, double screenH) {
300310 }
301311
302312 void CheckFoodCollision ( ) {
303- double eatDistSq = CellSize * ( double ) CellSize ;
313+
304314 for ( int fi = Food . Count - 1 ; fi >= 0 ; fi -- ) {
305315 var egg = Food [ fi ] ;
306316 double dx = Head . CurrentScreenPos . X - egg . ScreenX , dy = Head . CurrentScreenPos . Y - egg . ScreenY ;
307- if ( dx * dx + dy * dy >= eatDistSq ) continue ;
317+ if ( dx * dx + dy * dy >= _eatDistSq ) continue ;
308318 Score ++ ;
309319 for ( int p = 0 ; p < 8 ; p ++ ) {
310320 double angle = ( p / 8.0 ) * Math . PI * 2 + Random . Shared . NextDouble ( ) * 0.5 ;
@@ -336,6 +346,10 @@ void CheckFoodCollision() {
336346 }
337347 }
338348
349+ public void AddCommand ( int key ) {
350+ if ( KeyQueue . Count < 3 ) KeyQueue . Enqueue ( key ) ;
351+ }
352+
339353 void ProcessInput ( ) {
340354 bool processed = false ;
341355 var dir = Head . Direction ;
0 commit comments