Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Otter; namespace LabyrhintMaker { class Map : Entity { Tilemap tm; Random rnd; List<Vector2> path; List<Direction> directions; Direction lastDirection; public Map() { rnd = new Random(); lastDirection = Direction.Left; tm = new Tilemap(20 * 32, 14 * 32, 32, 32); AddGraphic(tm); path = new List<Vector2>(); directions = new List<Direction>(); Black(); DrawStart(); } private void Black() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 14; j++) { tm.SetTile(i, j, Color.Black); } } } private void DrawStart() { int x = rnd.Next(0, 21); int y = rnd.Next(0, 14); tm.SetTile(x, y, Color.Red); path.Add(new Vector2(x, y)); } public override void Update() { // 1. Check passability if (tm.GetTile((int)path[path.Count - 1].X - 2, (int)path[path.Count - 1].Y).Color.R == 0f) { directions.Add(Direction.Left); } if (tm.GetTile((int)path[path.Count - 1].X + 2, (int)path[path.Count - 1].Y).Color.R == 0f) { directions.Add(Direction.Right); } if (tm.GetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 2).Color.R == 0f) { directions.Add(Direction.Up); } if (tm.GetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 2).Color.R == 0f) { directions.Add(Direction.Down); } // 2. Pick random direction int dir = rnd.Next(0, directions.Count); // 2.5 Check while (directions[dir] == lastDirection) { dir = rnd.Next(0, directions.Count); } // 3. Draw if (directions[dir] == Direction.Left) { lastDirection = Direction.Left; tm.SetTile((int)path[path.Count - 1].X - 1, (int)path[path.Count - 1].Y, Color.Red); tm.SetTile((int)path[path.Count - 1].X - 2, (int)path[path.Count - 1].Y, Color.Red); float x = path[path.Count - 1].X - 2; float y = path[path.Count - 1].Y; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Right) { lastDirection = Direction.Right; tm.SetTile((int)path[path.Count - 1].X + 1, (int)path[path.Count - 1].Y, Color.Red); tm.SetTile((int)path[path.Count - 1].X + 2, (int)path[path.Count - 1].Y, Color.Red); float x = path[path.Count - 1].X + 2; float y = path[path.Count - 1].Y; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Up) { lastDirection = Direction.Up; tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 1, Color.Red); tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 2, Color.Red); float x = path[path.Count - 1].X; float y = path[path.Count - 1].Y - 2; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Down) { lastDirection = Direction.Down; tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 1, Color.Red); tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 2, Color.Red); float x = path[path.Count - 1].X; float y = path[path.Count - 1].Y + 2; path.Add(new Vector2(x, y)); } base.Update(); } } } |
C#-Quelltext |
|
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Otter; namespace LabyrhintMaker { class Map : Entity { Tilemap tm; Random rnd; List<Vector2> path; List<Direction> directions; Direction lastDirection; bool run; public Map() { rnd = new Random(); run = true; lastDirection = Direction.Left; tm = new Tilemap(20 * 32, 14 * 32, 32, 32); AddGraphic(tm); path = new List<Vector2>(); directions = new List<Direction>(); Black(); DrawStart(); } private void Black() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 14; j++) { tm.SetTile(i, j, Color.Black); } } } private void DrawStart() { int x = rnd.Next(0, 21); int y = rnd.Next(0, 14); tm.SetTile(x, y, Color.Red); path.Add(new Vector2(11, 7)); } public override void Update() { if (run) { // 1. Check passability if (tm.GetTile((int)path[path.Count - 1].X - 2, (int)path[path.Count - 1].Y).Color.R == 0f) { directions.Add(Direction.Left); } if (tm.GetTile((int)path[path.Count - 1].X + 2, (int)path[path.Count - 1].Y).Color.R == 0f) { directions.Add(Direction.Right); } if (tm.GetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 2).Color.R == 0f) { directions.Add(Direction.Up); } if (tm.GetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 2).Color.R == 0f) { directions.Add(Direction.Down); } if (directions.Count == 1) run = false; // 2. Pick random direction int dir = rnd.Next(0, directions.Count); // 2.5 Check try { while (directions[dir] == lastDirection) { dir = rnd.Next(0, directions.Count); } } catch (Exception ex) { } // 3. Draw if (directions[dir] == Direction.Left) { lastDirection = Direction.Left; tm.SetTile((int)path[path.Count - 1].X - 1, (int)path[path.Count - 1].Y, Color.Red); tm.SetTile((int)path[path.Count - 1].X - 2, (int)path[path.Count - 1].Y, Color.Red); float x = path[path.Count - 1].X - 2; float y = path[path.Count - 1].Y; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Right) { lastDirection = Direction.Right; tm.SetTile((int)path[path.Count - 1].X + 1, (int)path[path.Count - 1].Y, Color.Red); tm.SetTile((int)path[path.Count - 1].X + 2, (int)path[path.Count - 1].Y, Color.Red); float x = path[path.Count - 1].X + 2; float y = path[path.Count - 1].Y; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Up) { lastDirection = Direction.Up; tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 1, Color.Red); tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y - 2, Color.Red); float x = path[path.Count - 1].X; float y = path[path.Count - 1].Y - 2; path.Add(new Vector2(x, y)); } else if (directions[dir] == Direction.Down) { lastDirection = Direction.Down; tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 1, Color.Red); tm.SetTile((int)path[path.Count - 1].X, (int)path[path.Count - 1].Y + 2, Color.Red); float x = path[path.Count - 1].X; float y = path[path.Count - 1].Y + 2; path.Add(new Vector2(x, y)); } directions.Clear(); } base.Update(); } } } |
Werbeanzeige