diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-01-27 09:44:06 +0300 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-01-27 09:44:06 +0300 |
| commit | abecec2d432d836561b6af8f19c94d57df658697 (patch) | |
| tree | 2dae02ae6c019464c5cc56d201f7b6f9e3a982f7 | |
| parent | 57920a063e889fc024cee8e8f58a476c3dfc1b93 (diff) | |
Some code quality improvements
| -rw-r--r-- | DungeonSlime/Game1.cs | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/DungeonSlime/Game1.cs b/DungeonSlime/Game1.cs index 88bd262..d4529e2 100644 --- a/DungeonSlime/Game1.cs +++ b/DungeonSlime/Game1.cs @@ -49,7 +49,11 @@ struct Segment { class Snake { - public static readonly Point SegmentSize = new Point(50, 50); + //Constants + public static readonly Point SegmentSize = new Point(100, 100); + public static readonly Color HeadColor = Color.Green; + public static readonly Color SegmentColor = Color.White; + public Direction NextHeading { get; set; } List<Segment> tail = []; Segment head; @@ -58,7 +62,7 @@ class Snake Texture2D whitePixel; public float Speed { get; set; } - public Snake(Point postionOnGrid, Direction heading, Texture2D whitePixel, float startSpeed) + public Snake(Point postionOnGrid, int segments, Direction heading, Texture2D whitePixel, float startSpeed) { this.NextHeading = heading; @@ -69,19 +73,15 @@ class Snake }; head.targetPosition = head.pastPosition + GetHeadTargetOffset(); - tail.Add(new Segment + for (var i = 0; i < segments; i++) { - pastPosition = (postionOnGrid * SegmentSize).ToVector2(), - heading = heading, - targetPosition = head.pastPosition - }); - - tail.Add(new Segment - { - pastPosition = (postionOnGrid * SegmentSize).ToVector2(), - heading = heading, - targetPosition = head.pastPosition - }); + tail.Add(new Segment + { + pastPosition = (postionOnGrid * SegmentSize).ToVector2(), + heading = heading, + targetPosition = head.pastPosition + }); + } this.whitePixel = whitePixel; @@ -102,13 +102,7 @@ class Snake tail[i] = tail[i-1]; } - Segment tailSegment = tail[0]; - - tailSegment.pastPosition = head.pastPosition; - tailSegment.targetPosition = head.targetPosition; - tailSegment.heading = head.heading; - - tail[0] = tailSegment; + tail[0] = head; } //Start moving head to next grid position @@ -166,7 +160,7 @@ class Snake i.GetPosition(portionTraveled).ToPoint(), SegmentSize ), - Color.White + SegmentColor ); Rectangle trail = new Rectangle( @@ -195,7 +189,7 @@ class Snake spriteBatch.Draw( whitePixel, trailTrimmed, - Color.White + SegmentColor ); pastSegment = i; @@ -207,7 +201,7 @@ class Snake head.GetPosition(portionTraveled).ToPoint(), SegmentSize ), - Color.Red + HeadColor ); } } @@ -218,7 +212,7 @@ public class Game1 : Core Texture2D whitePixel; Snake snake; - public Game1() : base("Smooth Snake Movement", 1280, 720, false) + public Game1() : base("Smooth Snake Movement", 800, 600, false) { } @@ -230,7 +224,7 @@ public class Game1 : Core whitePixel = new Texture2D(GraphicsDevice, 1, 1); whitePixel.SetData<Color>([Color.White]); - snake = new Snake(new Point(2, 2), Direction.Right, whitePixel, StartSpeed); + snake = new Snake(new Point(2, 2), 10, Direction.Right, whitePixel, StartSpeed); } protected override void LoadContent() @@ -260,7 +254,7 @@ public class Game1 : Core protected override void Draw(GameTime gameTime) { - GraphicsDevice.Clear(Color.CornflowerBlue); + GraphicsDevice.Clear(Color.Black); SpriteBatch.Begin(); snake.Draw(SpriteBatch); |
