1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267:
| using System; using System.Collections.Generic; using System.Linq; 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 Microsoft.Xna.Framework.Storage; using System.Collections; using System.Media;
namespace Snake { public class Game1 : Microsoft.Xna.Framework.Game { bool verloren = false; GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont font;
ArrayList elementX = new ArrayList(); ArrayList elementY = new ArrayList();
ArrayList foodX = new ArrayList(); ArrayList foodY = new ArrayList();
int updates = 0; int speed = 5; int hightY; int wightX; int punkte = 0;
Texture2D bg, snake_body, snake_head,texture_food; string direction;
public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }
protected override void Initialize() { graphics.PreferredBackBufferHeight = 700; graphics.PreferredBackBufferWidth = 1000; graphics.ApplyChanges();
hightY = graphics.PreferredBackBufferHeight / 10; wightX = graphics.PreferredBackBufferWidth / 10;
elementY.Add(0); elementX.Add(0);
elementY.Add(1); elementX.Add(0);
elementY.Add(2); elementX.Add(0);
Random z = new Random(); foodX.Add(z.Next(0, graphics.PreferredBackBufferWidth/10)); foodY.Add(z.Next(0, graphics.PreferredBackBufferHeight/10));
foodX.Add(z.Next(0, graphics.PreferredBackBufferWidth/10)); foodY.Add(z.Next(0, graphics.PreferredBackBufferHeight/10));
base.Initialize(); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); snake_body = Content.Load<Texture2D>("snake_body"); snake_head = Content.Load<Texture2D>("snake_head"); texture_food = Content.Load<Texture2D>("food"); bg = Content.Load<Texture2D>("bg"); font= Content.Load<SpriteFont>("SpriteFont1"); MediaPlayer.IsRepeating = true; MediaPlayer.Play(Content.Load<Song>("media"));
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
int i = 1; while (i < elementX.Count) { if (elementX[i].ToString() == elementX[0].ToString() && elementY[i].ToString() == elementY[0].ToString()) { verloren = true; } i++; } if (verloren==false) {
KeyboardState ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.Up)) { if (direction != "d") { direction = "u"; } } if (ks.IsKeyDown(Keys.Down)) { if (direction != "u") { direction = "d"; } } if (ks.IsKeyDown(Keys.Left)) { if (direction != "r") { direction = "l"; } } if (ks.IsKeyDown(Keys.Right)) { if (direction != "l") { direction = "r"; } } if (ks.IsKeyDown(Keys.Escape)) { Exit(); } if (updates == speed) { if (direction == "d") { elementX.Insert(0, elementX[0]); elementX.RemoveAt(elementX.Count - 1);
elementY.Insert(0, Convert.ToUInt16(elementY[0]) + 1); elementY.RemoveAt(elementY.Count - 1);
} if (direction == "u") { elementX.Insert(0, elementX[0]); elementX.RemoveAt(elementX.Count - 1);
elementY.Insert(0, Convert.ToUInt16(elementY[0]) - 1); elementY.RemoveAt(elementY.Count - 1);
} if (direction == "l") { elementX.Insert(0, Convert.ToUInt16(elementX[0]) - 1); elementX.RemoveAt(elementX.Count - 1); elementY.Insert(0, elementY[0]); elementY.RemoveAt(elementY.Count - 1);
} if (direction == "r") { elementX.Insert(0, Convert.ToUInt16(elementX[0]) + 1); elementX.RemoveAt(elementX.Count - 1); elementY.Insert(0, elementY[0]); elementY.RemoveAt(elementY.Count - 1);
} updates = 0; } else { updates++; } i = 0; while (i < foodX.Count) { if (foodX[i].ToString() == elementX[0].ToString() && foodY[i].ToString() == elementY[0].ToString()) { foodY.RemoveAt(i); foodX.RemoveAt(i);
elementX.Add(-1); elementY.Add(-1);
punkte++; if (punkte == 17) { } Random z = new Random(); foodX.Add(z.Next(0, graphics.PreferredBackBufferWidth / 10)); foodY.Add(z.Next(0, graphics.PreferredBackBufferHeight / 10)); } i++; } if (Convert.ToInt16(elementX[0]) < 0) { elementX[0] = graphics.PreferredBackBufferWidth / 10; } if (Convert.ToInt16(elementX[0]) > graphics.PreferredBackBufferWidth / 10) { elementX[0] = 0; } if (Convert.ToInt16(elementY[0]) < 0) { elementY[0] = graphics.PreferredBackBufferHeight / 10; } if (Convert.ToInt16(elementY[0]) > graphics.PreferredBackBufferHeight / 10) { elementY[0] = 0; } } base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(bg, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
int i = 0; while (i < elementX.Count) { spriteBatch.Draw(snake_body,new Rectangle(Convert.ToInt16(elementX[i])*10,Convert.ToInt16(elementY[i])*10,10,10),Color.White); i++; } spriteBatch.Draw(snake_head, new Rectangle(Convert.ToInt16(elementX[0])*10, Convert.ToInt16(elementY[0])*10, 10, 10), Color.White); i = 0; while (i < foodX.Count) { spriteBatch.Draw(texture_food, new Rectangle(Convert.ToInt16(foodX[i])*10, Convert.ToInt16(foodY[i])*10, 10, 10), Color.White); i++; } if (verloren) {spriteBatch.DrawString(font,"G a m e O v e r!!",new Vector2(50,graphics.PreferredBackBufferHeight/2),Color.White); } spriteBatch.DrawString(font, "Punkte: "+punkte.ToString(), new Vector2(10,10), Color.White); spriteBatch.End();
base.Draw(gameTime); } } } |