Du bist nicht angemeldet.

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

1

22.10.2015, 13:54

Frage zu Labyrinth-Generation

Hi,

ich versuche in SFML/Otter einen LabyhrintMaker zu bauen :)
Also ein Spiel was, wenn man es startet ein zufälliges Labyhrint zeichnet.

Ich benutze dafür folgenden Code:

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();
        }
    }
}


Mein Problem ist das die erzeugten Labyhrinte nur einen kleinen Teil des Bildschirm am Ende bedecken und wenn die an eine Kante kommen wird einfach entlang der Kante wiedergezeichnet statt wieder in die Mitte zurückzukehren. Ich habe hier mal ein paar Bilder was ich meine:

(Link)

(Link)

(Link)


Ich hab es auch schon mit Debgging probiert ich finde den Fehler aber einfach nicht. Vielleicht kann mir hier ja jemand sagen was der Fehler ist :thumbup:

mfg

2

22.10.2015, 15:33

Danke für deine Hilfe

"Direction" ist ein Enum von der Library Otter selbst. Da sind Werte wie "Up", "Down", "LeftUp" und sowas drin. "Path" soll die Position aller bisher geziechneten Pfadkacheln besitzen. Der "Schwarz-Check" ist dafür da das der Weg sich nicht selbst übermalt was der aber trotzdem irgendwie tut :)

cojo2015

Alter Hase

Beiträge: 516

Wohnort: bei mir zu Hause

Beruf: Schüler

  • Private Nachricht senden

3

22.10.2015, 15:45

Ohne jetzt mir den Code im genauen durchgelesen zu haben, habe ich das gefunden: https://www.youtube.com/watch?v=i7U3sOU64Jo . Vielleicht hilft es dir ja :)

4

22.10.2015, 15:50

Zufall weil, genau von dem Video hab ich die Idee bekommen wie ich ein Labyhrint zeichne :)

5

22.10.2015, 16:01

Ich habe das jetzt so gemacht das die Liste jetzt gelöscht wird. Jetzt ist der Weg auch etwas logischer (keine Quadrate mehr). Aber in manchen Fällen passiert irgendwie eine Endlosschleife weil das Programm dann abgestürzt bzw. nicht mehr reagiert.

6

22.10.2015, 16:05

Ja List ist wowas wie ArrayList in Java mit dem Unterschied das man bei Java ".get()" aufsuchen muss um ein Element aus der Liste zu bekommen. Bei C# kann man das einfach mit den [] Klammern hinter dem Namen machen.

7

22.10.2015, 17:59

Ja, mein letztes Problem ist nur das, das Programm nach manchen Durchläufen sich aufhängt ich hab heir noch mal den ganzen Code:

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();
        }
    }
}


(Das man die Richtungen umkehrt wie du erklärst hast, ist derzeit noch nicht implementiert)

8

23.10.2015, 14:48

danke, sorry das ich erst so spät antworte :)

Werbeanzeige