<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Win32-Console 2D MAP-Game]]></title><description><![CDATA[<p>Hey Leute,<br />
ich habe nach langer Zeit wieder angefangen C++ zu programmieren (ich hatte viel mit der Schule zu tun) und habe ein neues Spiel geschrieben in der eine einfache Map gezeichnet wird, in der ein paar Objekte vorhanden sind und man sich als Spieler frei mit &quot;W A S D&quot; bewegen kann.</p>
<p>Bis jetzt klappt alles super und ich bin noch in der Entwicklung des Spiels, doch bevor ich jetzt anfange dieses Spiel etwas größer und umfangreicher zu gestalten, möchte ich gerne von euch noch wissen wie sauber mein Quelltext ist, da ich hier bisher bekannt bin als der Typ mit einer miserablen Schreibart.</p>
<p>Daher bitte ich euch kurz durch den Quelltext zu schauen, vielleicht das Spiel auszuprobieren (es ist noch in Entwicklung und man kann es noch nicht als ganzes Spiel betrachten) und mir vielleicht Verbesserungsvorschläge als Rückmeldung zu geben. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code>#include &lt;iostream&gt;
#include &lt;Windows.h&gt;
#include &lt;conio.h&gt;

using namespace std;

int main ()
{
	// Variables initialization
	//
    int Points = 0;
    int Key = 0;
    int x_Key = 0;
    int y_Key = 0;
    int x_Map = 40;
    int y_Map = 20;
    int Map[x_Map][y_Map];
    //
    ////

    // Reset MAP. MAP = 0 (Nothing &quot; &quot;)
    for (int y = 0; y &lt; y_Map; y++)
    {
        for (int x = 0; x &lt; x_Map; x++)
        {
            Map[x][y] = 0; 
        }   
    }

    // Initialization of all objects
    //
    // MAP: Player
    Map[0][0] = 1;

    // Map: Wall
    Map[1][0] = 2; Map[1][1] = 2; Map[0][3] = 2; Map[1][3] = 2; Map[2][3] = 2; Map[3][3] = 2;
    Map[3][2] = 2; Map[3][1] = 2; Map[5][0] = 2; Map[5][1] = 2; Map[5][2] = 2; Map[5][3] = 2;

    // MAP: Points
    Map[10][10] = 3;
    //
    ////

    // Game-Loop (MAP-render of all Map-parts &amp; Control)
    while(true)
    {
    	// Reset all Player-Coordinates on Map
    	for (int y = 0; y &lt; y_Map; y++)
        {
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == 1)
                    Map[x][y] = 0;
            } 
        }

    	// Initialize new Player-Coordinates on Map
        Map[x_Key][y_Key] = 1;

        // Reset Key-Input
        Key = 0;

        // Initialize new Key-Input if kbhit()
        if (kbhit())
            Key = getch();

		// Check, Calculate &amp; Set - The Key-Input
		//  
        if (Key == 119 &amp;&amp; Map[x_Key][y_Key-1] != 2) // Key-Input: w
        {
            if (Map[x_Key][y_Key-1] == 3)
                Points += 1;
            Map[x_Key][y_Key--] = 1;
        }
        else if (Key == 97 &amp;&amp; Map[x_Key-1][y_Key] != 2) // Key-Input: a
        {
            if (Map[x_Key-1][y_Key] == 3)
                Points += 1;
            Map[x_Key--][y_Key] = 1;
        }
        else if (Key == 115 &amp;&amp; Map[x_Key][y_Key+1] != 2) // Key-Input: s
        {
            if (Map[x_Key][y_Key+1] == 3)
                Points += 1;
            Map[x_Key][y_Key++] = 1;
        }
        else if (Key == 100 &amp;&amp; Map[x_Key+1][y_Key] != 2) // Key-Input: d
        {
            if (Map[x_Key+1][y_Key] == 3)
                Points += 1;
            Map[x_Key++][y_Key] = 1;
        }
        //
        ////

		// Map-Render (includes all objects)  
		//    
        cout &lt;&lt; &quot;==========================================\n&quot;;

        for (int y = 0; y &lt; y_Map; y++)
        {
            cout &lt;&lt; &quot;|&quot;;
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == 1)
                    cout &lt;&lt; &quot;o&quot;;
                else if (Map[x][y] == 2)
                    cout &lt;&lt; &quot;X&quot;;
                else if (Map[x][y] == 3)
                    cout &lt;&lt; &quot;P&quot;;
                else
                    cout &lt;&lt; &quot; &quot;;
            }
            cout &lt;&lt; &quot;|&quot;;
            cout &lt;&lt; endl;  
        }

        cout &lt;&lt; &quot;==========================================\n&quot;;
        //
        ////

		// Game-Stats   
        cout &lt;&lt; &quot;Key ID: &quot; &lt;&lt; Key;
        cout &lt;&lt; &quot;     Points: &quot; &lt;&lt; Points;

        // Reset Game-Event
        Sleep(50);
        system(&quot;CLS&quot;);
    } 
}
</code></pre>
<p>Ich denke schon allein wegen der Spielart, wäre es das beste für jedes Objekt eine Klasse anzufertigen und eine bessere Lösung für die Initialisierung für die Objekte zu finden. Jedoch weiß ich nicht wie ich das in die Tat umsetzen soll und hoffe hier auf hilfe! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>MfG, Marvin.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326681/win32-console-2d-map-game</link><generator>RSS for Node</generator><lastBuildDate>Tue, 26 May 2026 03:23:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326681.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Jun 2014 23:30:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Mon, 30 Jun 2014 23:30:47 GMT]]></title><description><![CDATA[<p>Hey Leute,<br />
ich habe nach langer Zeit wieder angefangen C++ zu programmieren (ich hatte viel mit der Schule zu tun) und habe ein neues Spiel geschrieben in der eine einfache Map gezeichnet wird, in der ein paar Objekte vorhanden sind und man sich als Spieler frei mit &quot;W A S D&quot; bewegen kann.</p>
<p>Bis jetzt klappt alles super und ich bin noch in der Entwicklung des Spiels, doch bevor ich jetzt anfange dieses Spiel etwas größer und umfangreicher zu gestalten, möchte ich gerne von euch noch wissen wie sauber mein Quelltext ist, da ich hier bisher bekannt bin als der Typ mit einer miserablen Schreibart.</p>
<p>Daher bitte ich euch kurz durch den Quelltext zu schauen, vielleicht das Spiel auszuprobieren (es ist noch in Entwicklung und man kann es noch nicht als ganzes Spiel betrachten) und mir vielleicht Verbesserungsvorschläge als Rückmeldung zu geben. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code>#include &lt;iostream&gt;
#include &lt;Windows.h&gt;
#include &lt;conio.h&gt;

using namespace std;

int main ()
{
	// Variables initialization
	//
    int Points = 0;
    int Key = 0;
    int x_Key = 0;
    int y_Key = 0;
    int x_Map = 40;
    int y_Map = 20;
    int Map[x_Map][y_Map];
    //
    ////

    // Reset MAP. MAP = 0 (Nothing &quot; &quot;)
    for (int y = 0; y &lt; y_Map; y++)
    {
        for (int x = 0; x &lt; x_Map; x++)
        {
            Map[x][y] = 0; 
        }   
    }

    // Initialization of all objects
    //
    // MAP: Player
    Map[0][0] = 1;

    // Map: Wall
    Map[1][0] = 2; Map[1][1] = 2; Map[0][3] = 2; Map[1][3] = 2; Map[2][3] = 2; Map[3][3] = 2;
    Map[3][2] = 2; Map[3][1] = 2; Map[5][0] = 2; Map[5][1] = 2; Map[5][2] = 2; Map[5][3] = 2;

    // MAP: Points
    Map[10][10] = 3;
    //
    ////

    // Game-Loop (MAP-render of all Map-parts &amp; Control)
    while(true)
    {
    	// Reset all Player-Coordinates on Map
    	for (int y = 0; y &lt; y_Map; y++)
        {
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == 1)
                    Map[x][y] = 0;
            } 
        }

    	// Initialize new Player-Coordinates on Map
        Map[x_Key][y_Key] = 1;

        // Reset Key-Input
        Key = 0;

        // Initialize new Key-Input if kbhit()
        if (kbhit())
            Key = getch();

		// Check, Calculate &amp; Set - The Key-Input
		//  
        if (Key == 119 &amp;&amp; Map[x_Key][y_Key-1] != 2) // Key-Input: w
        {
            if (Map[x_Key][y_Key-1] == 3)
                Points += 1;
            Map[x_Key][y_Key--] = 1;
        }
        else if (Key == 97 &amp;&amp; Map[x_Key-1][y_Key] != 2) // Key-Input: a
        {
            if (Map[x_Key-1][y_Key] == 3)
                Points += 1;
            Map[x_Key--][y_Key] = 1;
        }
        else if (Key == 115 &amp;&amp; Map[x_Key][y_Key+1] != 2) // Key-Input: s
        {
            if (Map[x_Key][y_Key+1] == 3)
                Points += 1;
            Map[x_Key][y_Key++] = 1;
        }
        else if (Key == 100 &amp;&amp; Map[x_Key+1][y_Key] != 2) // Key-Input: d
        {
            if (Map[x_Key+1][y_Key] == 3)
                Points += 1;
            Map[x_Key++][y_Key] = 1;
        }
        //
        ////

		// Map-Render (includes all objects)  
		//    
        cout &lt;&lt; &quot;==========================================\n&quot;;

        for (int y = 0; y &lt; y_Map; y++)
        {
            cout &lt;&lt; &quot;|&quot;;
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == 1)
                    cout &lt;&lt; &quot;o&quot;;
                else if (Map[x][y] == 2)
                    cout &lt;&lt; &quot;X&quot;;
                else if (Map[x][y] == 3)
                    cout &lt;&lt; &quot;P&quot;;
                else
                    cout &lt;&lt; &quot; &quot;;
            }
            cout &lt;&lt; &quot;|&quot;;
            cout &lt;&lt; endl;  
        }

        cout &lt;&lt; &quot;==========================================\n&quot;;
        //
        ////

		// Game-Stats   
        cout &lt;&lt; &quot;Key ID: &quot; &lt;&lt; Key;
        cout &lt;&lt; &quot;     Points: &quot; &lt;&lt; Points;

        // Reset Game-Event
        Sleep(50);
        system(&quot;CLS&quot;);
    } 
}
</code></pre>
<p>Ich denke schon allein wegen der Spielart, wäre es das beste für jedes Objekt eine Klasse anzufertigen und eine bessere Lösung für die Initialisierung für die Objekte zu finden. Jedoch weiß ich nicht wie ich das in die Tat umsetzen soll und hoffe hier auf hilfe! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>MfG, Marvin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406421</guid><dc:creator><![CDATA[Moorky Dragonstrike]]></dc:creator><pubDate>Mon, 30 Jun 2014 23:30:47 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Tue, 01 Jul 2014 16:10:04 GMT]]></title><description><![CDATA[<pre><code>{
    // Variables initialization
    //
    int Points = 0;
    int Key = 0;
    int x_Key = 0;
    int y_Key = 0;
    int x_Map = 40;
    int y_Map = 20;
</code></pre>
<p>Alle Variablen sollten so lokal wie möglich deklariert werden. Solche gleich am Anfang der <code>main</code> -Funktion zu deklarieren ist daher extrem schlechter Programmierstil.<br />
Wegen der Zuweisung in Zeile 61 ist die Initialisierung von <code>Key</code> <em>theoretisch</em> sowieso überflüssig - pack' aber gleich die komplette Deklaration von <code>Key</code> nach Zeile 61!<br />
(Die Initialisierung ist hier aber nicht per se falsch, sondern richtig, und sollte dir prinzipiell als Angewohnheit bleiben)</p>
<pre><code>if (Map[x][y] == 1)
                    cout &lt;&lt; &quot;o&quot;;
                else if (Map[x][y] == 2)
                    cout &lt;&lt; &quot;X&quot;;
                else if (Map[x][y] == 3)
                    cout &lt;&lt; &quot;P&quot;;
                else
                    cout &lt;&lt; &quot; &quot;;
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /></p>
<pre><code>static char const output[] = &quot; oXP&quot;;
cout &lt;&lt; output[Map[x][y&rsqb;&rsqb;;
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /></p>
<pre><code>static char const output[] = &quot; oXP&quot;;
        for (int y = 0; y &lt; y_Map; y++)
                cout &lt;&lt; '|' &lt;&lt; output[Map[x][y&rsqb;&rsqb; &lt;&lt; &quot;|\n&quot;;
</code></pre>
<p>IIRC ist die Ausgabe auf Windows Zeilengepuffert.<br />
Und einzelne Zeichen wo möglich nicht in String- sondern Zeichen-Literale packen. Also <code>&quot;|&quot;</code> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /> <code>'|'</code> (ist im Code ja demonstriert).</p>
<pre><code>Map[x_Key][y_Key--] = 1;
</code></pre>
<p>Volkard würde dir eins mit dem Braten überziehen! Aber so richtig.</p>
<p>Die Zeilen 69 bis 92 sind auch alle recht ähnlich, findest du nicht? Verletzung von <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a>.</p>
<pre><code>auto check_key_with_indices = [&amp;]( char ch, std::size_t x_add, std::size_t y_add )
{
        if (Key == ch &amp;&amp; Map[x_Key + x_add][y_Key + y_add] != 2)
        {
            Map[x_Key][y_Key] = 1;
            // Reihenfolge verändert - ist die Äquivalenz vorhanden?
            y_Key += y_add;
            x_Key += x_add;
            if (Map[x_Key][y_Key] == 3)
                ++Points;
            return true;
        }
        return false;
}

     if( check_key_with_indices('w',  0, -1) )   ;
else if( check_key_with_indices('a', -1,  0) )   ;
else if( check_key_with_indices('s',  0,  1) )   ;
else if( check_key_with_indices('d',  1,  0) )   ;
</code></pre>
<p>(Alles ungetestet, keine Garantie)<br />
Mit einem Makro ginge es potenziell schöner, ist aber nicht gewünscht.</p>
<p>Und was auch direkt auffällt ist die Tatsache dass du immer neu renderst, obwohl dein Spiel darauf ausgerichtet ist dass nichts passiert solange keine Taste gedrückt wurde (oder?).<br />
Daher sollte das <code>if (kbhit())</code> in Zeile 64 wohl eher verschwinden - reicht AFAICS.</p>
<p>Ein <code>break</code> oder <code>return</code> sehe ich in der Schleife auch nicht. Sollte das Spiel nicht, sobald Q o.ä. gedrückt wurde, abgebrochen werden?</p>
<p>Und warum <code>Key == 115</code> ? Warum nicht mit einem Zeichenliteral vergleichen, also in diesem Fall <code>'s'</code> ? Und warum kein<a href="http://en.cppreference.com/w/cpp/string/byte/tolower" rel="nofollow"> <code>std::tolower</code> </a>(bspw. um Caps-Lock zu ignorieren)?<br />
(Eigentlich könnte man andersherum<a href="http://en.cppreference.com/w/cpp/string/byte/toupper" rel="nofollow"> <code>std::toupper</code> </a>verwenden und mit <code>'S'</code> vergleichen)</p>
<p>Warum keine Enumeration als Elementtyp der <code>Map</code> verwenden?</p>
<p>Kann momentan übrigens nichts testen, da ich unter Linux bin (Portabilität wäre ab einem gewissen Punkt durch platformunabhängige Libs auch nicht schlecht! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> ), verzeih' mir also jedwegige Patzer.<br />
~<br />
Edit³: Bugs im Lambda korrigiert.~</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406422</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 01 Jul 2014 16:10:04 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Tue, 01 Jul 2014 06:18:02 GMT]]></title><description><![CDATA[<p>Warum wird immer die ganze Konsole gelöscht und alles neu gezeichnet? Das dürfte zu mehr oder weniger heftigem Flackern führen.<br />
Entweder einfach ohne zu löschen alles übermalen - dann sollte es schon mal nicht flackern, oder, noch viel besser, nur die Positionen übermalen, die sich ändern.<br />
Wenn eine Spielfigur von 10,11 nach 10,12 läuft, sind im Normalfall nur diese beiden Positionen auf den neuen Stand zu bringen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406442</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Tue, 01 Jul 2014 06:18:02 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Tue, 01 Jul 2014 10:35:12 GMT]]></title><description><![CDATA[<p>Danke für die schnellen Antworten.</p>
<blockquote>
<p>Warum wird immer die ganze Konsole gelöscht und alles neu gezeichnet? Das dürfte zu mehr oder weniger heftigem Flackern führen.<br />
Entweder einfach ohne zu löschen alles übermalen - dann sollte es schon mal nicht flackern, oder, noch viel besser, nur die Positionen übermalen, die sich ändern.<br />
Wenn eine Spielfigur von 10,11 nach 10,12 läuft, sind im Normalfall nur diese beiden Positionen auf den neuen Stand zu bringen.</p>
</blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/22666">@Belli</a><br />
Leider weiß ich nicht wie man nur ganz bestimmte Positionen löschen kann...<br />
+ @Arcoth<br />
Ich hätte zwar auch getch() nehmen können und nicht kbhit(), dann hätte sich die Konsole nur bei Tastendruck neu gezeichnet, ich möchte aber noch frei bewegende NPC's einfügen.<br />
Da mein Wissen in C++ stark begrenzt ist, kenne ich nicht die schönere Lösung.<br />
Es wäre nett wenn du sie mir zeigen könntest. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<blockquote>
<p>Ein break oder return sehe ich in der Schleife auch nicht. Sollte das Spiel nicht, sobald Q o.ä. gedrückt wurde, abgebrochen werden?</p>
<p>Und warum Key == 115? Warum nicht mit einem Zeichenliteral vergleichen, also in diesem Fall 's'? Und warum kein std::tolower (bspw. um Caps-Lock zu ignorieren)?<br />
(Eigentlich könnte man andersherum std::toupper verwenden und mit 'S' vergleichen)</p>
<p>Warum keine Enumeration als Elementtyp der Map verwenden?</p>
</blockquote>
<p>@Arcoth<br />
Ich wollte im Nachhinein (da das Spiel noch in Entwicklung ist) ein Exit mit der ESC-Taste einfügen. (Also while(Key != ESC){...} return 0;)<br />
Zu dem Thema warum ich für die Keys Zahlen verwende und nicht das Zeichen ansich: Was mache ich wenn ich z.B. eine Pfeiltaste oder die ESC-Taste inkludieren möchte? Dann kommen da immer komische Zeichen wenn ich es dann mit cout &lt;&lt; Key; teste. Daher fand ich auf Key eine int statt einem char zu setzen für den Anfang einfacher. (Vielleicht kannst du mir da ja ein Lösungsvorschlag geben)<br />
Eine Enumeration einzusetzen habe ich mir auch überlegt, nur weiß ich nicht wie ich das in einem 2D-Array in die Tat umsetzen kann...</p>
<p>MfG, Marvin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406483</guid><dc:creator><![CDATA[Moorky Dragonstrike]]></dc:creator><pubDate>Tue, 01 Jul 2014 10:35:12 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Tue, 01 Jul 2014 10:44:08 GMT]]></title><description><![CDATA[<p>Moorky Dragonstrike schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/22666">@Belli</a><br />
Leider weiß ich nicht wie man nur ganz bestimmte Positionen löschen kann...</p>
</blockquote>
<p>Dafür solltest Du hier jede Menge Beispiele finden. Suche im Konsolenforum, das ist zwar für neue Beiträge geschlossen, aber da ist reichlich alter, guter Code für die Windows-Konsole zu finden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406486</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406486</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Tue, 01 Jul 2014 10:44:08 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Tue, 01 Jul 2014 16:18:51 GMT]]></title><description><![CDATA[<blockquote>
<p>Eine Enumeration einzusetzen habe ich mir auch überlegt, nur weiß ich nicht wie ich das in einem 2D-Array in die Tat umsetzen kann...</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Die Frage verstehe ich nicht ganz. Du verwendest als Elementtyp des multidimensionalen Arrays die Enumeration.</p>
<pre><code>// Ich kenne deine Objekte nicht, daher habe ich mal geraten:
enum BlockType
{
    nothing,
    human,
    rock,
    player
};

BlockType Map[x_Map][y_Map];
</code></pre>
<p>Statt dann ein Element mit einer Zahl zu vergleichen vergleichst du das Element mit dem entsprechenden Enumerator.</p>
<pre><code>if( /* [...] */ Map[x_Key+1][y_Key] != rock )
</code></pre>
<p>Du könntest dann in die OOP einsteigen indem du für die verschiedenen Bestandteile des Spiels Klassen definierst, darunter natürlich die Umgebung (Map), die darin enthaltenen Objekte, und so weiter.</p>
<blockquote>
<p>Dann kommen da immer komische Zeichen wenn ich es dann mit cout &lt;&lt; Key; teste. Daher fand ich auf Key eine int statt einem char zu setzen für den Anfang einfacher. (Vielleicht kannst du mir da ja ein Lösungsvorschlag geben)</p>
</blockquote>
<p>Nun, du vergleichst dann einfach für die Buchstaben mit den Zeichenliteralen <code>'a'</code> , <code>'s'</code> , usw. und für die Pfeil- und Escape-Taste(n) mit dem entsprechenden ASCII-Code. Keiner hindert dich daran diese zu vermischen.</p>
<p>Es wäre aber deutlich sinnvoller diese folgendermaßen festzuhalten:</p>
<pre><code>int const left_key = 'a';
int const right_key = 'd';
int const down_key = 's';
int const up_key = 'w';
int const escape_key = ...; // bzw. gleich den Namen geben der die Funktionsweise repräsentiert
int const arrow_right_key = ...; // dito
// ...
</code></pre>
<blockquote>
<p>ich möchte aber noch frei bewegende NPC's einfügen.</p>
</blockquote>
<p>Auch dann ist es überflüssig die gesamte Konsole neu zu Zeichnen; Belli hat es ja zusammengefasst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2406534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406534</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 01 Jul 2014 16:18:51 GMT</pubDate></item><item><title><![CDATA[Reply to Win32-Console 2D MAP-Game on Fri, 04 Jul 2014 12:05:45 GMT]]></title><description><![CDATA[<p>Danke für die ganzen Antworten! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Ich kann euch hier schon mal zeigen inwiefern ich den Quelltext schon verändert habe.<br />
Nun werde ich mich darum kümmern das:<br />
<s>1. enum für die Map benutzen</s><br />
<s>2. Nur die Positionen löschen, die sich ändern</s><br />
3. Den Quelltext für die Key's ein bisschen schöner gestalten (Auch wenn ich das noch nicht ganz verstanden habe^^)</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;Windows.h&gt;
#include &lt;conio.h&gt;
#include &lt;stdio.h&gt;

using namespace std;

void gotoxy(int x, int y)         //funktion gotoxy
{          
  COORD c;

  c.X = x;
  c.Y = y;
  SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main ()
{
	// Map initialization
	//
	enum Map_Objects
	{
		Nothing,
		Player,
		Wall,
		Point
	};
    const int x_Map = 40;
    const int y_Map = 20;
    Map_Objects Map[x_Map][y_Map];
	//
	////

	// Variables initialization
	//
    int Points = 0;
    int Key = 0;
    int x_Player = 0;
    int y_Player = 0;
    //
    ////

    // Reset MAP. MAP = 0 (Nothing &quot; &quot;)
    for (int y = 0; y &lt; y_Map; y++)
    {
        for (int x = 0; x &lt; x_Map; x++)
        {
            Map[x][y] = Nothing; 
        }   
    }

    // Initialization of all objects
    //
    // MAP: Player
    Map[0][0] = Player;

    // Map: Wall
    Map[1][0] = Wall; Map[1][1] = Wall; Map[0][3] = Wall; Map[1][3] = Wall; Map[2][3] = Wall;
	Map[3][3] = Wall; Map[3][2] = Wall; Map[3][1] = Wall; Map[5][0] = Wall; Map[5][1] = Wall;
	Map[5][2] = Wall; Map[5][3] = Wall;

    // MAP: Point
    Map[10][10] = Point;
    //
    ////

    // Game-Loop (MAP-render of all Map-parts &amp; Control)
    while(true)
    {
		// Map-Render (includes all objects)  
		//    
        cout &lt;&lt; &quot;==========================================\n&quot;;

        for (int y = 0; y &lt; y_Map; y++)
        {
            cout &lt;&lt; &quot;|&quot;;
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == Player)
                    cout &lt;&lt; &quot;o&quot;;
                else if (Map[x][y] == Wall)
                    cout &lt;&lt; &quot;X&quot;;
                else if (Map[x][y] == Point)
                    cout &lt;&lt; &quot;P&quot;;
                else
                    cout &lt;&lt; &quot; &quot;;
            }
            cout &lt;&lt; &quot;|&quot;;
            cout &lt;&lt; endl;  
        }

        cout &lt;&lt; &quot;==========================================\n&quot;;
        //
        ////

		// Game-Stats   
//x        cout &lt;&lt; &quot;Key ID: &quot; &lt;&lt; Key;
        cout &lt;&lt; &quot;     Points: &quot; &lt;&lt; Points;
		cout &lt;&lt; &quot;     Coords: (&quot; &lt;&lt; x_Player &lt;&lt; &quot;|&quot; &lt;&lt; y_Player &lt;&lt; &quot;)    &quot;;

    	// Reset all Player-Coordinates on Map
    	for (int y = 0; y &lt; y_Map; y++)
        {
            for (int x = 0; x &lt; x_Map; x++)
            {
                if (Map[x][y] == Player)
                    Map[x][y] = Nothing;
            } 
        }

        // Reset Key-Input
        Key = 0;

        // Initialize new Key-Input if kbhit()
        if (kbhit())
            Key = getch();

		// Check, Calculate &amp; Set - The Key-Input
		//  
        if (Key == 119 &amp;&amp; Map[x_Player][y_Player-1] != Wall) // Key-Input: w
        {
            if (Map[x_Player][y_Player-1] == Point)
                Points += 1;
            y_Player--;
        }
        else if (Key == 97 &amp;&amp; Map[x_Player-1][y_Player] != Wall) // Key-Input: a
        {
            if (Map[x_Player-1][y_Player] == Point)
                Points += 1;
            x_Player--;
        }
        else if (Key == 115 &amp;&amp; Map[x_Player][y_Player+1] != Wall) // Key-Input: s
        {
            if (Map[x_Player][y_Player+1] == Point)
                Points += 1;
            y_Player++;
        }
        else if (Key == 100 &amp;&amp; Map[x_Player+1][y_Player] != Wall) // Key-Input: d
        {
            if (Map[x_Player+1][y_Player] == Point)
                Points += 1;
            x_Player++;
        }
        //
        ////

		// Initialize new Player-Coordinates on Map
        Map[x_Player][y_Player] = Player;

        // Reset Game-Event
//x        Sleep(50);
        gotoxy(0, 0);
    } 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2406922</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2406922</guid><dc:creator><![CDATA[Moorky Dragonstrike]]></dc:creator><pubDate>Fri, 04 Jul 2014 12:05:45 GMT</pubDate></item></channel></rss>