fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12.  
  13. namespace XNASnake
  14. {
  15. /// <summary>
  16. /// This is the main type for your game
  17. /// </summary>
  18. public class Game1 : Microsoft.Xna.Framework.Game
  19. {
  20. GraphicsDeviceManager graphics;
  21. SpriteBatch spriteBatch;
  22. KeyboardState oldState = Keyboard.GetState();
  23. Snake.WhereToGo keyState;
  24. int time;
  25. Apple apple;
  26. private int appleTime;
  27. private bool drawApple;
  28. Character Tail;
  29. private Snake Head;
  30. private int ileKwadratow;
  31. SpriteFont sf;
  32.  
  33. public Game1()
  34. {
  35. graphics = new GraphicsDeviceManager(this);
  36. graphics.PreferredBackBufferHeight = 300;
  37. graphics.PreferredBackBufferWidth = 300;
  38. Content.RootDirectory = "Content";
  39. }
  40.  
  41. protected override void Initialize()
  42. {
  43.  
  44. base.Initialize();
  45. }
  46.  
  47. protected override void LoadContent()
  48. {
  49. ileKwadratow = 0;
  50. Tail = new Character();
  51. time = 0;
  52. spriteBatch = new SpriteBatch(GraphicsDevice);
  53. apple = new Apple();
  54. apple.texture = Content.Load<Texture2D>("Snake/Apple");
  55. Head = new Snake(Content.Load<Texture2D>("Snake/SnakeRectangle"));
  56. Tail.wezykList.Add(Head);
  57. sf = Content.Load<SpriteFont>("SpriteFont1");
  58. Head.position = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
  59. Head.velocity = new Vector2(5, 0);
  60. }
  61.  
  62. protected override void UnloadContent()
  63. {
  64.  
  65. }
  66.  
  67. public void SnakeUpdate()
  68. {
  69. Head.position += Head.velocity;
  70. }
  71.  
  72.  
  73.  
  74. protected override void Update(GameTime gameTime)
  75. {
  76. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  77. this.Exit();
  78.  
  79. KeyAdapter();
  80. CheckApple();
  81.  
  82. time += gameTime.ElapsedGameTime.Milliseconds;
  83. appleTime += gameTime.ElapsedGameTime.Milliseconds;
  84. SnakeUpdate();
  85.  
  86. foreach (Snake s in Tail.wezykList)
  87. {
  88. s.position = Head.position;
  89. }
  90.  
  91.  
  92. if (appleTime >= 6000 || apple.isDead == true)
  93. {
  94. drawApple = true;
  95. apple.X = new Random().Next(0, graphics.PreferredBackBufferWidth - apple.texture.Width);
  96. apple.Y = 200;
  97. appleTime -= 6000;
  98. apple.isDead = false;
  99. }
  100.  
  101. base.Update(gameTime);
  102. }
  103.  
  104. public void KeyAdapter()
  105. {
  106. KeyboardState currentState = Keyboard.GetState();
  107.  
  108. if (currentState.IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up))
  109. {
  110. keyState = Snake.WhereToGo.goUP;
  111. Head.velocity = new Vector2(0, -3);
  112. }
  113.  
  114. if (currentState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down))
  115. {
  116. keyState = Snake.WhereToGo.goDOWN;
  117. Head.velocity = new Vector2(0, 3);
  118. }
  119.  
  120. if (currentState.IsKeyDown(Keys.Left) && oldState.IsKeyUp(Keys.Left))
  121. {
  122. keyState = Snake.WhereToGo.goLEFT;
  123. Head.velocity = new Vector2(-3, 0);
  124. }
  125.  
  126. if (currentState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right))
  127. {
  128. keyState = Snake.WhereToGo.goRIGHT;
  129. Head.velocity = new Vector2(3, 0);
  130. }
  131. }
  132.  
  133. public void CheckApple()
  134. {
  135. if (apple.Rectanglee().Intersects(Tail.wezykList[0].Rectanglee()))
  136. {
  137. Snake dodatek = new Snake(Content.Load<Texture2D>("Snake/SnakeRectangle"));
  138.  
  139. if (keyState == Snake.WhereToGo.goDOWN)
  140. {
  141. dodatek.position.X = Tail.wezykList[ileKwadratow].position.X;
  142. dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y - 10;
  143. }
  144. if (keyState == Snake.WhereToGo.goUP)
  145. {
  146. dodatek.position.X = Tail.wezykList[ileKwadratow].position.X;
  147. dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y + 10;
  148. }
  149. if (keyState == Snake.WhereToGo.goLEFT)
  150. {
  151. dodatek.position.X = Tail.wezykList[ileKwadratow].position.X + 10;
  152. dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y;
  153. }
  154. if (keyState == Snake.WhereToGo.goRIGHT)
  155. {
  156. dodatek.position.X = Tail.wezykList[ileKwadratow].position.X - 10;
  157. dodatek.position.Y = Tail.wezykList[ileKwadratow].position.Y;
  158. }
  159.  
  160.  
  161. apple.isDead = true;
  162.  
  163.  
  164. ileKwadratow++;
  165. Tail.wezykList.Add(dodatek);
  166.  
  167. }
  168. }
  169.  
  170. protected override void Draw(GameTime gameTime)
  171. {
  172. GraphicsDevice.Clear(Color.CornflowerBlue);
  173. spriteBatch.Begin();
  174.  
  175. //spriteBatch.Draw(sticzeq.wezykList[0].texture, sticzeq.wezykList[0].Rectanglee(), Color.Aqua);
  176. // rysowanie OK
  177.  
  178. foreach (Snake s in Tail.wezykList)
  179. {
  180. spriteBatch.Draw(s.texture, s.Rectanglee(), Color.Aqua);
  181. }
  182.  
  183. if (drawApple == true)
  184. {
  185. spriteBatch.Draw(apple.texture, apple.Rectanglee(), Color.Red);
  186. }
  187.  
  188. spriteBatch.DrawString(sf,Convert.ToString(keyState),new Vector2(100,100),Color.Black);
  189. spriteBatch.DrawString(sf, Convert.ToString(Tail.wezykList.Count), new Vector2(100, 130), Color.Black);
  190.  
  191.  
  192. spriteBatch.End();
  193.  
  194. base.Draw(gameTime);
  195. }
  196. }
  197. }
  198.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(18,36): error CS0234: The type or namespace name `Xna' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
stdout

Standard output is empty