muh
No notes
Syntax:
C#
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.Net; using Microsoft.Xna.Framework.Storage; namespace ASternTest1 { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; int gridHeight = 5; int gridWidth = 7; Node[,] grid; Texture2D pixel; KeyboardState previousKeyboardState; Node startNode; Node endNode; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here grid = new Node[gridWidth,gridHeight]; for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { grid[i, j] = new Node(); grid[i, j].xPos = i; grid[i, j].yPos = j; } } grid[3, 1].walkable = false; grid[3, 2].walkable = false; grid[3, 3].walkable = false; previousKeyboardState = Keyboard.GetState(PlayerIndex.One); startNode = grid[1,2]; endNode = grid[5,2]; base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); pixel = Content.Load<Texture2D>("pixel"); // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); if (previousKeyboardState.IsKeyUp(Keys.Enter) && Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter)) { ASternSuche(startNode, endNode); } previousKeyboardState = Keyboard.GetState(PlayerIndex.One); // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here DrawGrid(); base.Draw(gameTime); } public void DrawGrid() { for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { Rectangle position = new Rectangle(50 + (60 * i), 50 + (60 * j), 50, 50); spriteBatch.Begin(SpriteBlendMode.AlphaBlend); if (grid[i, j].walkable) { spriteBatch.Draw(pixel, position, null, Color.White); } else { spriteBatch.Draw(pixel, position, null, Color.Blue); } spriteBatch.End(); } } } public void ASternSuche(Node start, Node end) { Node actual = start; Node helpNode = null; start.openListed = true; int zugKosten = 0; while (!(end.closedListed)) { foreach (Node node in grid) { if ((node.openListed && (node.fValue < actual.fValue)) || node.openListed && actual.closedListed) { actual = node; } } actual.openListed = false; actual.closedListed = true; for (int i = 1; i < 9; i++) { helpNode = grid[3, 1]; switch (i) { case 1: if (actual.xPos > 0 && actual.yPos > 0) { helpNode = grid[actual.xPos - 1, actual.yPos - 1]; zugKosten = 14; } break; case 2: if (actual.yPos > 0) { helpNode = grid[actual.xPos, actual.yPos - 1]; zugKosten = 10; } break; case 3: if (actual.xPos < gridWidth - 1 && actual.yPos > 0) { helpNode = grid[actual.xPos + 1, actual.yPos - 1]; zugKosten = 14; } break; case 4: if (actual.xPos > 0) { helpNode = grid[actual.xPos - 1, actual.yPos]; zugKosten = 10; } break; case 5: if (actual.xPos < gridWidth - 1) { helpNode = grid[actual.xPos + 1, actual.yPos]; zugKosten = 10; } break; case 6: if (actual.xPos > 0 && actual.yPos < gridHeight - 1) { helpNode = grid[actual.xPos - 1, actual.yPos + 1]; zugKosten = 14; } break; case 7: if (actual.yPos < gridHeight - 1) { helpNode = grid[actual.xPos, actual.yPos + 1]; zugKosten = 10; } break; case 8: if (actual.xPos < gridWidth - 1 && actual.yPos < gridHeight - 1) { helpNode = grid[actual.xPos + 1, actual.yPos + 1]; zugKosten = 14; } break; } if (helpNode.walkable && !helpNode.closedListed) { if (!helpNode.openListed) { helpNode.openListed = true; helpNode.parent = actual; helpNode.gValue = actual.gValue + zugKosten; helpNode.hValue = (int)(Math.Abs(helpNode.xPos - end.xPos) + Math.Abs(helpNode.yPos - end.yPos)); helpNode.fValue = helpNode.gValue + helpNode.hValue; } else { if ((actual.gValue + zugKosten) < helpNode.gValue) { helpNode.parent = actual; helpNode.gValue = actual.gValue + zugKosten; helpNode.fValue = helpNode.gValue + helpNode.hValue; } } } } } while (!(end.Equals(start))) { end.walkable = false; end = end.parent; } end.walkable = false; } } }