Feedback

Tilemap aus Datei entnehmen (Laden)

Sprache: C#

Hey! Hier ist ein recht einfaches Beispiel wie Ihr eine Tilemap, bestehend aus Zahlen, laden und zeichnen könnt! Die Seite wird geupdatet um neue Funktionen zu veranschaulichen. [b][u]"Materialien"[/u][/b] Texturen(Wand/Boden) Eine Tilemap, zB: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (Tile1.map) [b][u]Code[/u][/b]
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
//Zunächst implementieren wir die IO-Namespace.
using System.IO;
public class Form1
{
	//Deklaration und das Laden!
	private string MapPath = "Tile1.map";
	//Einen StreamReader der den MapPath als Konstruktorfülle inne ist.
	private StreamReader ReadFile_Length = new StreamReader(MapPath);
	private string Map;
	//Ein Array um den "Pointer" eine Zeile dazu zu addieren
	private string[] StringBuffer_Y = File.ReadAllLines(MapPath);
	//Mapinformationen
	//---Tile data--- 
	private int MapX;
	private int MapY;

	private string Map_Y_LineInString;
	//Rectangles

	//---Tiles---
	private Rectangle Wand;

	private Rectangle Boden;
	//1 = Wand
	//0 = Boden

	//---TileTexturen---
	private Bitmap WandTexture = new Bitmap("Textures\wand.png");

	private Bitmap BodenTexture = new Bitmap("Textures\boden.png");


	private void Form1_Load(object sender, EventArgs e)
	{
		DoubleBuffered = true;
		//Und die Map in den String laden!
		Map = ReadFile_Length.ReadToEnd();
	}

	//das Zeichnen

	private void DrawTile(object sender, PaintEventArgs e)
	{
		try {
			//Alle Zeilen durchlaufen
			for (y = 0; y <= Map.Length; y++) {
				Map_Y_LineInString = StringBuffer_Y[y];
				for (x = 0; x <= Map_Y_LineInString.Length / 2; x++) {
					//Jedes Zeichen durchlaufen
					//Und bei " " splitten!
					string[] ConvertMap = Map_Y_LineInString.Split(' ');
					//Stück für Stück,Zeichen für Zeichen, ein Rectangle setzen.
					switch (ConvertMap[Convert.ToInt32(x)]) {

						case "0":
							//Die Koordinaten auslesen.
							MapX = Convert.ToInt32(x * 20);
							MapY = y * 20;
							Boden = new Rectangle(MapX, MapY, 20, 20);
							e.Graphics.DrawImage(BodenTexture, Boden);
							break;
						case "1":
							MapX = Convert.ToInt32(x * 20);
							MapY = y * 20;
							Wand = new Rectangle(MapX, MapY, 20, 20);
							e.Graphics.DrawImage(WandTexture, Wand);
							break;
					}

				}
			}
		//Fertig!
		} catch {
		}
	}
	public Form1()
	{
		
	Load += Form1_Load;
        Paint += DrawTile;
	}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
//Zunächst implementieren wir die IO-Namespace.
using System.IO;
public class Form1
{
	//Deklaration und das Laden!
	private string MapPath = "Tile1.map";
	//Einen StreamReader der den MapPath als Konstruktorfülle inne ist.
	private StreamReader ReadFile_Length = new StreamReader(MapPath);
	private string Map;
	//Ein Array um den "Pointer" eine Zeile dazu zu addieren
	private string[] StringBuffer_Y = File.ReadAllLines(MapPath);
	//Mapinformationen
	//---Tile data--- 
	private int MapX;
	private int MapY;

	private string Map_Y_LineInString;
	//Rectangles

	//---Tiles---
	private Rectangle Wand;

	private Rectangle Boden;
	//1 = Wand
	//0 = Boden

	//---TileTexturen---
	private Bitmap WandTexture = new Bitmap("Textures\wand.png");

	private Bitmap BodenTexture = new Bitmap("Textures\boden.png");


	private void Form1_Load(object sender, EventArgs e)
	{
		DoubleBuffered = true;
		//Und die Map in den String laden!
		Map = ReadFile_Length.ReadToEnd();
	}

	//das Zeichnen

	private void DrawTile(object sender, PaintEventArgs e)
	{
		try {
			//Alle Zeilen durchlaufen
			for (y = 0; y <= Map.Length; y++) {
				Map_Y_LineInString = StringBuffer_Y[y];
				for (x = 0; x <= Map_Y_LineInString.Length / 2; x++) {
					//Jedes Zeichen durchlaufen
					//Und bei " " splitten!
					string[] ConvertMap = Map_Y_LineInString.Split(' ');
					//Stück für Stück,Zeichen für Zeichen, ein Rectangle setzen.
					switch (ConvertMap[Convert.ToInt32(x)]) {

						case "0":
							//Die Koordinaten auslesen.
							MapX = Convert.ToInt32(x * 20);
							MapY = y * 20;
							Boden = new Rectangle(MapX, MapY, 20, 20);
							e.Graphics.DrawImage(BodenTexture, Boden);
							break;
						case "1":
							MapX = Convert.ToInt32(x * 20);
							MapY = y * 20;
							Wand = new Rectangle(MapX, MapY, 20, 20);
							e.Graphics.DrawImage(WandTexture, Wand);
							break;
					}

				}
			}
		//Fertig!
		} catch {
		}
	}
	public Form1()
	{
		
	Load += Form1_Load;
        Paint += DrawTile;
	}
}