Impressum
< Laufendes O - Teil 4 Inhalt Laufendes O - Teil 6>

Das laufende O - Teil 5 (Arrays)

Beschreibung

Zur Vermeidung von unübersichtlichem Code wird nun das Verhalten und das Aussehen des Spielfeldes in Arrays abgelegt.
Das Spielfeld wird in einem 2dimensionalen Array abgelegt, welcher mit Zahlen gefüllt ist. Der Zahlenwert allein bestimmt über das Aussehen (Zeichen, Farbe, ...) und das Verhalten (begehbar, kann gefressen werden, ...).
Somit kann man das Spiel verändert, indem man nur die Variablen im oberen Teil anders belegt. Die Algorithmen im Programm bleiben hierbei gleich.

Youtube Video: Das laufende O Teil 5

Der Quellcode

using System; namespace Laufendes_O_5_Arrays { class Program { static void Main(string[] args) { int[,] feld = { {1,1,1,1,1,1,1,1,1,1,1 }, {1,0,0,1,0,0,0,1,0,0,1 }, {1,4,0,1,0,0,0,1,0,0,1 }, {1,0,0,1,1,0,1,1,1,0,1 }, {1,0,1,1,2,0,0,3,0,0,1 }, {1,0,0,4,2,0,0,1,0,0,1 }, {1,0,0,0,0,0,0,1,2,0,1 }, {1,1,1,1,1,1,1,1,1,1,1 }, }; char[] zeichen = {' ', '#', '*', 'X', '§' }; int[] wirdzu = {0, 1, 0, 4, 4 }; bool[] begehbar = {true, false, true, true, false }; int[] punkte = {0,0,10,1,0 }; for (int x=0; x<feld.GetLength(0); x++) for (int y = 0; y < feld.GetLength(1); y++) { Console.SetCursorPosition(x, y); int wert = feld[x, y]; Console.Write(zeichen[wert]); } char c; int sx=1, sy=1; int spunkte = 0; do { Console.SetCursorPosition(feld.GetLength(0)+2, 3); Console.Write("Punkte: "+spunkte); Console.SetCursorPosition(sx, sy); Console.Write("O"); c = Console.ReadKey(true).KeyChar; Console.SetCursorPosition(sx, sy); Console.Write(zeichen[feld[sx,sy]]); int oldX = sx; int oldY = sy; if (c == 'w') sy--; if (c == 's') sy++; if (c == 'a') sx--; if (c == 'd') sx++; if (begehbar[ feld[sx, sy] ]==false) { sx = oldX; sy = oldY; } spunkte = spunkte + punkte[feld[sx, sy]]; feld[sx, sy] = wirdzu[feld[sx, sy]]; } while (c!='q'); } } }