Guest User

Untitled

a guest
Feb 25th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12. using System.Timers;
  13.  
  14. namespace Snakes_on_a_Game
  15. {
  16. class Snake
  17. {
  18. static Rectangle BLOCK_SIZE = new Rectangle(0, 0, 40, 40);
  19. List<Vector2> SnakePositions = new List<Vector2>();
  20. Vector2 snakeVelocity;
  21. int snakeLength = 5;
  22. Texture2D snaketexture;
  23. private Game1 game1;
  24.  
  25. public Snake(Game1 game1)
  26. {
  27. // TODO: Complete member initialization
  28. snaketexture = game1.Content.Load<Texture2D>(@"Square");
  29. this.game1 = game1;
  30. snakeVelocity = new Vector2(1, 0);
  31. for(int i = 0; i < snakeLength; i++)
  32. SnakePositions.Add(new Vector2(i, i)*snakeVelocity);
  33. }
  34.  
  35. public void Draw(GameTime gameTime)
  36. {
  37. for (int i = 0; i < SnakePositions.Count; i++)
  38. {
  39. Vector2 v = SnakePositions[i];
  40. Rectangle pos = new Rectangle((int)(BLOCK_SIZE.Width*v.X), (int)(BLOCK_SIZE.Height*v.Y),
  41. (int)(BLOCK_SIZE.Width), (int)(BLOCK_SIZE.Height));
  42. game1.spriteBatch.Draw(snaketexture, pos, Color.Green);
  43. }
  44. }
  45.  
  46. const double UPDATE_TIME = 500;
  47. double lastMoveTime;
  48. public void Update(GameTime gameTime)
  49. {
  50.  
  51.  
  52. KeyboardState keystate = Keyboard.GetState();
  53. if (keystate.IsKeyDown(Keys.Up))
  54. {
  55. snakeVelocity = new Vector2(0, 1);
  56. }
  57. else if (keystate.IsKeyDown(Keys.Down))
  58. {
  59. snakeVelocity = new Vector2(0, -1);
  60. }
  61. else if (keystate.IsKeyDown(Keys.Left))
  62. {
  63. snakeVelocity = new Vector2(-1, 0);
  64. }
  65. else if (keystate.IsKeyDown(Keys.Right))
  66. {
  67. snakeVelocity = new Vector2(-1, 0);
  68. }
  69.  
  70. double l = gameTime.TotalGameTime.TotalMilliseconds;
  71.  
  72. //gotta check if 500ms has elapsed since lastMoveTime
  73. if (l - lastMoveTime < UPDATE_TIME)
  74. return;
  75. lastMoveTime = l;
  76. //You also need to use some time check otherwise it's gonna be extremely fast
  77. //you'll want to remove the latest vector of the list and then place a new one in front
  78. Vector2 newPos = SnakePositions[SnakePositions.Count-1] + snakeVelocity;
  79. SnakePositions.RemoveAt(0);
  80. if (SnakePositions.Contains(newPos))
  81. {
  82. //game over because we hit ourself
  83. } else
  84. SnakePositions.Add(newPos);
  85.  
  86. //check if you hit a food or w/e they are called (or possibly do this in Game1 class.. dunno)
  87. //but thats it i guess
  88. }
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment