Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using System.Timers;
- namespace Snakes_on_a_Game
- {
- class Snake
- {
- static Rectangle BLOCK_SIZE = new Rectangle(0, 0, 40, 40);
- List<Vector2> SnakePositions = new List<Vector2>();
- Vector2 snakeVelocity;
- int snakeLength = 5;
- Texture2D snaketexture;
- private Game1 game1;
- public Snake(Game1 game1)
- {
- // TODO: Complete member initialization
- snaketexture = game1.Content.Load<Texture2D>(@"Square");
- this.game1 = game1;
- snakeVelocity = new Vector2(1, 0);
- for(int i = 0; i < snakeLength; i++)
- SnakePositions.Add(new Vector2(i, i)*snakeVelocity);
- }
- public void Draw(GameTime gameTime)
- {
- for (int i = 0; i < SnakePositions.Count; i++)
- {
- Vector2 v = SnakePositions[i];
- Rectangle pos = new Rectangle((int)(BLOCK_SIZE.Width*v.X), (int)(BLOCK_SIZE.Height*v.Y),
- (int)(BLOCK_SIZE.Width), (int)(BLOCK_SIZE.Height));
- game1.spriteBatch.Draw(snaketexture, pos, Color.Green);
- }
- }
- const double UPDATE_TIME = 500;
- double lastMoveTime;
- public void Update(GameTime gameTime)
- {
- KeyboardState keystate = Keyboard.GetState();
- if (keystate.IsKeyDown(Keys.Up))
- {
- snakeVelocity = new Vector2(0, 1);
- }
- else if (keystate.IsKeyDown(Keys.Down))
- {
- snakeVelocity = new Vector2(0, -1);
- }
- else if (keystate.IsKeyDown(Keys.Left))
- {
- snakeVelocity = new Vector2(-1, 0);
- }
- else if (keystate.IsKeyDown(Keys.Right))
- {
- snakeVelocity = new Vector2(-1, 0);
- }
- double l = gameTime.TotalGameTime.TotalMilliseconds;
- //gotta check if 500ms has elapsed since lastMoveTime
- if (l - lastMoveTime < UPDATE_TIME)
- return;
- lastMoveTime = l;
- //You also need to use some time check otherwise it's gonna be extremely fast
- //you'll want to remove the latest vector of the list and then place a new one in front
- Vector2 newPos = SnakePositions[SnakePositions.Count-1] + snakeVelocity;
- SnakePositions.RemoveAt(0);
- if (SnakePositions.Contains(newPos))
- {
- //game over because we hit ourself
- } else
- SnakePositions.Add(newPos);
- //check if you hit a food or w/e they are called (or possibly do this in Game1 class.. dunno)
- //but thats it i guess
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment