'C# snake game problem with body extension monogame
So I have this problem where my sprites draw to close to the head of my snake and therefore only takes very long time to complete the map. I have tried to increase the speed and lower the frame rate but it is very laggy.
//Comments are in Swedish
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
SpriteFont xcord;
SpriteFont ycord;
//Map settings
int mapX;
int mapY;
//Movement Variabels
//Snake Movement
bool snakemoveup;
bool snakemovedown;
bool snakemoveright;
bool snakemoveleft;
int speedx;
int speedy;
int score;
int timer;
int i;
List<Rectangle> snakelength = new List<Rectangle>();
List<int> positiony = new List<int>() { 300, 300 };
bool snakemoveup2;
bool snakemovedown2;
bool snakemoveright2;
bool snakemoveleft2;
bool snakemoveupMemory;
bool snakemovedownMemory;
bool snakemoverightMemory;
bool snakemoveleftMemory;
//Sprite Variables
//Textures
Texture2D snakehead;
Texture2D gamemap;
Texture2D gamemapborder;
Texture2D Leader_Right;
Texture2D Leader_Left;
Texture2D Leader_Up;
Texture2D Leader_Down;
Texture2D Follower;
Texture2D Candy;
//Sprite Rectangles
Rectangle snakerec;
Rectangle followerrec;
Rectangle candyrec;
//Position Variables
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}enter code here
protected override void Initialize()
{
// TODO: Add your initialization logic here
//MapSize
mapX = 900 + 100;
mapY = 800 + 100;
_graphics.PreferredBackBufferWidth = mapX;
_graphics.PreferredBackBufferHeight = mapY;
_graphics.ApplyChanges();
//Movement
speedx = 0;
speedy = 0;
base.Initialize();
}
protected override void LoadContent()
{
//läger till en svans i listan
snakelength.Add(new Rectangle(snakerec.X, snakerec.Y, 50, 50));
//sänker fps:n (nämnaren = önskade fps:n)
IsFixedTimeStep = true;
TargetElapsedTime = System.TimeSpan.FromSeconds(1d / 60);
xcord = Content.Load<SpriteFont>("File");
ycord = Content.Load<SpriteFont>("File");
_spriteBatch = new SpriteBatch(GraphicsDevice);
Leader_Right = Content.Load<Texture2D>("Leader_Right");
Leader_Left = Content.Load<Texture2D>("Leader_Left");
Leader_Up = Content.Load<Texture2D>("Leader_Up");
Leader_Down = Content.Load<Texture2D>("Leader_Down");
Candy = Content.Load<Texture2D>("Candy");
Follower = Content.Load<Texture2D>("Follower");
gamemap = Content.Load<Texture2D>("Snake_map");
gamemapborder = Content.Load<Texture2D>("Snakemapborder");
snakerec = new Rectangle(500, 450, Leader_Right.Width, Leader_Right.Height);
candyrec = new Rectangle(200,150, Candy.Width, Candy.Height);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
//gameTime.ElapsedGameTime.Milliseconds
//sätt en if innan ditt ökande
snakerec.X += speedx;
snakerec.Y += speedy;
if (Keyboard.GetState().IsKeyDown(Keys.W) && snakemovedown == false)
{
snakemoveup2 = true;
snakemoveupMemory = true;
}
if (Keyboard.GetState().IsKeyDown(Keys.D) && snakemoveleft == false)
{
snakemoveright2 = true;
snakemoverightMemory = true;
}
if (Keyboard.GetState().IsKeyDown(Keys.A) && snakemoveright == false)
{
snakemoveleft2 = true;
snakemoveleftMemory = true;
}
if (Keyboard.GetState().IsKeyDown(Keys.S) && snakemoveup == false)
{
snakemovedown2 = true;
snakemovedownMemory = true;
}
if (snakerec.X % 50 == 0 && snakemoveupMemory == true)
{
speedy = -5;
speedx = 0;
snakemoveup = true;
snakemovedown = false;
snakemoveright = false;
snakemoveleft = false;
snakemoveupMemory = false;
}
if (snakerec.Y % 50 == 0 && snakemoveleftMemory == true)
{
speedy = 0;
speedx = -5;
snakemoveleft = true;
snakemovedown = false;
snakemoveright = false;
snakemoveup = false;
snakemoveleftMemory = false;
}
if (snakerec.X % 50 == 0 && snakemovedownMemory == true )
{
speedy = 5;
speedx = 0;
snakemovedown = true;
snakemoveup = false;
snakemoveright = false;
snakemoveleft = false;
snakemovedownMemory = false;
}
if (snakerec.Y % 50 == 0 && snakemoverightMemory == true)
{
speedy = 0;
speedx = 5;
snakemoveright = true;
snakemovedown = false;
snakemoveup = false;
snakemoveleft = false;
snakemoverightMemory = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.W))
{
snakemoveup2 = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.S))
{
snakemovedown2 = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.A))
{
snakemoveleft2 = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.D))
{
snakemoveright2 = false;
}
//Godis
Random randomx = new Random();
Random randomy = new Random();
List<int> godtalx = new List<int>() { 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900 };
List<int> godtaly = new List<int>() { 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800 };
if (snakerec.Intersects(candyrec))
{
candyrec.X = godtalx[randomx.Next(1, 18)];
candyrec.Y = godtaly[randomx.Next(1, 16)];
score++;
snakelength.Add(new Rectangle(snakerec.X, snakerec.Y, 50, 50));
}
//Follower
if (snakelength.Count != score)
{
for (int i = snakelength.Count - 1; i > 0; i--)
{
snakelength[i] = new Rectangle(snakelength[i - 1].X, snakelength[i - 1].Y, 50, 50);
}
}
snakelength[0] = new Rectangle(snakerec.X, snakerec.Y, 50, 50);
//Infite borders
//X
if (snakerec.X <= 50 || snakerec.X >= 900)
{
if (snakerec.X <= 0)
{
snakerec.X = 995;
}
if (snakerec.X >= 1000)
{
snakerec.X = 5;
}
}
//Y
if (snakerec.Y <= 50 || snakerec.Y >= 800)
{
if (snakerec.Y <= 0)
{
snakerec.Y = 895;
}
if (snakerec.Y >= 900)
{
snakerec.Y = 5;
}
}
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
_spriteBatch.Begin();
_spriteBatch.Draw(gamemap, new Vector2(0, 0), Color.White);
for (int i = 0; i < snakelength.Count; i++)
{
_spriteBatch.Draw(Follower, new Rectangle(snakelength[i].X, snakelength[i].Y, 50, 50), Color.White);
}
//Vilket håll huvudet kollar mot
if (true)
{
if (snakemoveup == true)
{
_spriteBatch.Draw(Leader_Up, snakerec, Color.White);
}
if (snakemoveright == true)
{
_spriteBatch.Draw(Leader_Right, snakerec, Color.White);
}
if (snakemoveleft == true)
{
_spriteBatch.Draw(Leader_Left, snakerec, Color.White);
}
if (snakemovedown == true)
{
_spriteBatch.Draw(Leader_Down, snakerec, Color.White);
}
}
_spriteBatch.Draw(Candy, candyrec, Color.White);
_spriteBatch.Draw(gamemapborder, new Vector2(0, 0), Color.White);
_spriteBatch.DrawString(xcord, (snakelength.Count).ToString(), new Vector2(0, 60), Color.Black);
_spriteBatch.DrawString(xcord, (snakerec.X).ToString(), new Vector2(0, 0), Color.Black);
_spriteBatch.DrawString(ycord, (snakerec.Y).ToString(), new Vector2(0, 30), Color.Black);
_spriteBatch.End();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
