Der Weg ist das Ziel - wer hat einen Weg gefunden?
Das war die Aufgabe
Postet hier eure Ergebnisse, Codes, Gedanken. Lernt von den anderen, seht euch deren Quelltext an und versucht euren eigenen zu verbessern.
Diskussion
Postet zu euren Codes stets eine kurze Erklärung mit euren Gedanken in denen ihr simpel gesagt die Frage "Wieso habe ich XY auf diese Art gelöst?" beantwortet. Beiträge, die nur den Code enthalten werden wir aus dem Thread entfernen.
Nächste Aufgabe
In zwei Wochen, wird die Musterlösung nach editiert und die nächste Aufgabe eingestellt.
Viel Spaß & viel Erfolg!
Musterlösung:
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] SuperStrict AppTitle = "Types / Waypoints / KI"
Global gfx_w:Int = 800, gfx_h:Int = 600 Graphics(gfx_w, gfx_h) Local FrameTimer:TTimer = TTimer.Create(60) SetClsColor(255, 255, 255) SetColor(0, 0, 0)
Local wp_A:Twaypoint = Twaypoint.Create("A", gfx_w *.5, gfx_h *.1) Local wp_B:Twaypoint = Twaypoint.Create("B", gfx_w *.25, gfx_h *.33) Local wp_C:Twaypoint = Twaypoint.Create("C", gfx_w *.75, gfx_h *.33) Local wp_D:Twaypoint = Twaypoint.Create("D", gfx_w *.25, gfx_h *.66) Local wp_E:Twaypoint = Twaypoint.Create("E", gfx_w *.75, gfx_h *.66) Local wp_F:Twaypoint = Twaypoint.Create("F", gfx_w *.5, gfx_h *.5) Local wp_G:Twaypoint = Twaypoint.Create("G", gfx_w *.5, gfx_h *.9)
wp_A.ConnectToWP(wp_B) wp_A.ConnectToWP(wp_C) wp_B.ConnectToWP(wp_D) wp_C.ConnectToWP(wp_E) wp_F.ConnectToWP(wp_A) wp_F.ConnectToWP(wp_D) wp_F.ConnectToWP(wp_E) wp_F.ConnectToWP(wp_G) wp_G.ConnectToWP(wp_E) wp_G.ConnectToWP(wp_D)
Tki.Create("Bob", Rand(0, gfx_w), Rand(0, gfx_h)) Tki.Create("Alice", Rand(0, gfx_w), Rand(0, gfx_h)) Tki.Create("Nimbus", Rand(0, gfx_w), Rand(0, gfx_h))
Repeat Cls Twaypoint.DrawAll() Tki.UpdateAll() Flip(0) FrameTimer.wait() Until KeyHit(KEY_ESCAPE) Or AppTerminate() End
Type Twaypoint Field x:Int, y:Int Field name:String Field Nachbarn:Twaypoint[] Global AllWPs:TList = New TList Method New() AllWPs.AddLast(Self) End Method Function Create:Twaypoint(_name:String, _x:Int, _y:Int) Local WP:Twaypoint = New Twaypoint WP.name = _name WP.x = _x WP.y = _y Return WP End Function Method ConnectToWP(_wp:Twaypoint) Self.Nachbarn:+[_wp] _wp.Nachbarn:+[Self] End Method Function DrawAll() For Local wp:Twaypoint = EachIn Twaypoint.AllWPs wp.Draw() Next End Function Method Draw() SetColor(0, 255, 0) DrawOval(Self.x - 16, Self.y - 16, 32, 32) SetColor(0, 0, 0) DrawText(Self.name, Self.x - 5, Self.y - 5) End Method End Type
Type Tki Field x:Int, y:Int Field name:String Field Ziel_WP:Twaypoint Field Letzter_WP:Twaypoint Global AllKIs:TList = New TList
Method New() AllKIs.AddLast(Self) End Method Function Create:Tki(_name:String, _x:Int, _y:Int) Local ki:Tki = New Tki ki.name = _name ki.x = _x ki.y = _y ki.Ziel_WP = Twaypoint(Twaypoint.AllWPs.ValueAtIndex(Rand(1, 4))) End Function Function UpdateAll() For Local ki:Tki = EachIn Tki.AllKIs Local vx:Float = ki.Ziel_WP.x - ki.x Local vy:Float = ki.Ziel_WP.y - ki.y Local vbetrag:Float = Sqr(vx * vx + vy * vy) vx:/vbetrag vy:/vbetrag If vbetrag > 5 Then ki.x:+vx * 3 ki.y:+vy * 3 Else Local NewWP:Twaypoint Repeat NewWP = ki.Ziel_WP.Nachbarn[Rand(0, ki.Ziel_WP.Nachbarn.Length - 1)] If NewWP <> ki.Letzter_WP Then Exit Forever ki.Letzter_WP = ki.Ziel_WP ki.Ziel_WP = NewWP EndIf SetColor(128, 128, 255) DrawOval(ki.x - 8, ki.y - 8, 16, 16) SetColor(0, 0, 0) DrawText(ki.name, ki.x, ki.y + 12) Next End Function End Type
|