public Score() { font = Global.Content.Load<SpriteFont>("gamefont"); gameArea = new GameArea(); ScoreCount = 0; Position = new Vector2(gameArea.RightBound, 0); gameArea = null; }
public void Update(SnakeSection[] snake, GameArea gameArea) { if (!IsAlive) { IsAlive = true; ok = false; while (!ok) { int x = rnd.Next(gameArea.LeftBound / Texture.Width, gameArea.RightBound / Texture.Width) * Texture.Width; int y = rnd.Next(0, Global.GraphicsManager.PreferredBackBufferHeight / Texture.Height) * Texture.Height; base.Rectangle = new Rectangle(x, y, Texture.Width, Texture.Height); ok = true; for (int i = 0; i < snake.Length; i++) { if (snake[i].Rectangle.Intersects(base.Rectangle)) ok = false; } } IsAlive = true; } }
private void Collision(GameTime gameTime, GameArea gameArea, Apple apple, ScreenManager screenManager) { if(snakeSectionArray[0].Rectangle.X < gameArea.LeftBound || snakeSectionArray[0].Rectangle.Y < 0 || snakeSectionArray[0].Rectangle.X + snakeSectionArray[0].Rectangle.Width> gameArea.RightBound || snakeSectionArray[0].Rectangle.Y + snakeSectionArray[0].Rectangle.Height > Global.GraphicsManager.PreferredBackBufferHeight) { GameOver(screenManager); } for (int i = 1; i < snakeSectionArray.Length; i++) { if(snakeSectionArray[0].Rectangle.Intersects(snakeSectionArray[i].Rectangle)) { GameOver(screenManager); } } if(snakeSectionArray[0].Rectangle.Intersects(apple.Rectangle)) { score.IncreaseScore(); apple.IsAlive = false; SnakeSection[] buffer = snakeSectionArray; snakeSize++; snakeSectionArray = new SnakeSection[snakeSize]; for (int i = 0; i < buffer.Length; i++) { snakeSectionArray[i] = buffer[i]; } if(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X - snakeSectionArray[snakeSectionArray.Length - 3].Rectangle.X == 0) { snakeSectionArray[snakeSectionArray.Length - 1] = new SnakeSection(pathToTexture, new Rectangle(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X, snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y + Texture.Height, Texture.Width, Texture.Height)); } else if (snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y - snakeSectionArray[snakeSectionArray.Length - 3].Rectangle.Y == 0) { snakeSectionArray[snakeSectionArray.Length - 1] = new SnakeSection(pathToTexture, new Rectangle(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X, snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y + Texture.Height, Texture.Width, Texture.Height)); } } }
public void Update(GameTime gameTime, ScreenPad screenPad, GameArea gameArea, Apple apple, ScreenManager screenManager) { Movement(gameTime, screenPad); Collision(gameTime, gameArea, apple, screenManager); }
private void LoadContent() { gameArea = new GameArea(); if (Global.TextureWidth == 20) { snake = new Snake("Snake/SmallSize/middle"); apple = new Apple("Snake/SmallSize/apple"); } else { snake = new Snake("Snake/MediumSize/middle"); apple = new Apple("Snake/MediumSize/apple"); } screenPad = new ScreenPad(Global.Content.Load<Texture2D>("Controls/upButton"), Global.Content.Load<Texture2D>("Controls/downButton"), Global.Content.Load<Texture2D>("Controls/leftButton"), Global.Content.Load<Texture2D>("Controls/rightButton"), false); }