2D Waypoint TuT by Basecamp

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

 

Basecamp

Betreff: 2D Waypoint TuT by Basecamp

BeitragSo, Apr 27, 2008 16:28
Antworten mit Zitat
Benutzer-Profile anzeigen
Hey Leute Hab euch hier ein kleines Waypoint Example, das ich selber gecodet und selber dokumentiert habe... Ist mein erstes TuT also pls ned zusammenscheissen^^ Aber Kritik is trotzdem erwünscht Wink

Code: [AUSKLAPPEN]
; Waypoint2D Example 2008 by Basecamp
; www.basecamp-online.net
;
; Ein einfaches 2D Waypoint Beispiel um z.b Bots zu programmieren
; ---------------------------------------------------------------------

Graphics 800,600,16,2 ; Grafikmodus setzen
SetBuffer BackBuffer() ; Buffer setzen
SeedRnd MilliSecs() ; Wert für Zufallszahlen einstellen
AppTitle "Waypoint2D Example" ; Titel des Programms

w = 20 ; Anzahl Waypoints

Dim WayPointX(w) ; WayPointX mit Anzahl Waypoints in ein Feld setzen
Dim WayPointY(w) ; WayPointY mit Anzahl Waypoints in ein Feld setzen

For i = 1 To w ; Von 1 bis n (Anzahl Waypoints)
 WayPointX(i) = Rnd(800) ; WayPointX darf höchstens 800 Pixel betragen
 WayPointY(i) = Rnd(600) ; WayPointY darf höchstens 600 Pixel betragen
Next

; Global PosX# = 400 ; Definierter Anfang (Beispiel Mitte [X Position])
; Global PosY# = 300 ; Definierter Anfang (Beispiel Mitte [Y Position])
Global PosX# = Rnd(800) ; Zufälliger Anfang (X Position)
Global PosY# = Rnd(800) ; Zufälliger Anfang (Y Position)

Speed = 5 ; Tempo des Bots
Global WP ; Waypoint als Global setzen
Winkel = NextWayPoint() ; Winkel definieren (als NextWayPoint())

Repeat ; Repeat-Until Schleife definiert
Cls ; Grafik vom Bildschirm löschen

For i = 1 To w ; Von 1 bis n (Anzahl Punkte)
Next ; For-Next Schleife beenden

Color 0,255,0: Oval PosX-5, PosY-5, 20, 20 ; Farbe, Form, Position und Grösse des Bots einstellen

PosX = PosX + Cos(Winkel) * speed ; PosX entspricht PosX + Kosinuswert * Tempo des Bots
PosY = PosY + Sin(Winkel) * speed ; PosY entspricht PosY + Sinuswert * Tempo des Bots

If Abs(WayPointX(WP) - PosX) =< Speed And Abs(WayPointY(WP) - PosY) =< Speed Then Winkel = NextWayPoint() ; Unwichtiges Zeugs                                                                                                       

Flip ; Alles anzeigen
Until KeyHit(1) ; Wenn Taste 1 (Esc) gedrückt wird
End ; dann wird das Proggy beendet

Function NextWayPoint() ; NextWayPoint() Funktionsbeginn
 WP = WP + 1 ; Das der Punkt immer vom aktuellen eins weiter geht
 If WP = 21 Then WP = 1 ; Wenn einer mehr als der letzte erreicht wird, wieder zu Punkt 1
 Winkel# = (450 - ATan2((WayPointX(WP) - PosX), (WayPointY(WP) - PosY))) Mod 360 ; Winkel bestimmen
 Return Winkel# ; Zur Winkel# Funktion zurück
End Function ; Funktion beenden
-------------------------
www.basecamp-online.net
-------------------------

Eingeproggt

BeitragSo, Apr 27, 2008 16:50
Antworten mit Zitat
Benutzer-Profile anzeigen
Habs mal ein bisschen nach meinem Geschmack abgeändert...
Was mich am meisten gestört hat war, dass du das suchen des nächsten Waypoints als "Unwichtiges zeugs" kommentriert hattest Confused

Naja, sonst noch ein paar kleine Änderungen gemacht. Viel Spass.
Code: [AUSKLAPPEN]

; Waypoint2D Example 2008 by Basecamp, modified by Eingeproggt
; www.basecamp-online.net
;
; Ein einfaches 2D Waypoint Beispiel um z.b Bots zu programmieren
; ---------------------------------------------------------------------

gfx_x=800 ;Graphik-Fenstergröße
gfx_y=600
Graphics gfx_x,gfx_y,16,2 ; Grafikmodus setzen
SetBuffer BackBuffer() ; Buffer setzen
SeedRnd MilliSecs() ; Wert für Zufallszahlen einstellen
AppTitle "Waypoint2D Example" ; Titel des Programms

Global w = 20 ; Anzahl Waypoints

Dim WayPointX(w) ; WayPointX mit Anzahl Waypoints in ein Feld setzen
Dim WayPointY(w) ; WayPointY mit Anzahl Waypoints in ein Feld setzen

For i = 1 To w ; Von 1 bis w (Anzahl Waypoints)
   WayPointX(i) = Rnd(gfx_x) ; WayPointX darf höchstens 800 Pixel betragen
   WayPointY(i) = Rnd(gfx_y) ; WayPointY darf höchstens 600 Pixel betragen
Next

; Global PosX# = 400 ; Definierter Anfang (Beispiel Mitte [X Position])
; Global PosY# = 300 ; Definierter Anfang (Beispiel Mitte [Y Position])
Global PosX# = Rnd(gfx_x) ; Zufälliger Anfang (X Position)
Global PosY# = Rnd(gfx_y) ; Zufälliger Anfang (Y Position)

Speed = 5 ; Tempo des Bots
Global WP ; Waypoint als Global setzen
Winkel = NextWayPoint() ; Winkel definieren (als NextWayPoint())

timer=CreateTimer(60) ;Timer, damit CPU nicht 100% ausgelastet wird

Repeat ; Repeat-Until Schleife definiert
   Cls ; Grafik vom Bildschirm löschen
   
   ;Vorschau der Waypoints (Kann weggelassen werden, nur zur Veranscheulichung)
   Color 0,0,200
   For i = 1 To w ; Von 1 bis w (Anzahl Punkte)
      Rect WayPointX(i)-2,WayPointY(i)-2,4,4,1
      Text WayPointX(i)+2,WayPointY(i)+2,i
   Next ; For-Next Schleife beenden
   
   Color 0,255,0:Oval PosX-5, PosY-5, 20, 20 ; Farbe, Form, Position und Grösse des Bots einstellen
   
   PosX = PosX + Cos(Winkel) * speed ; PosX entspricht PosX + Kosinuswert * Tempo des Bots
   PosY = PosY + Sin(Winkel) * speed ; PosY entspricht PosY + Sinuswert * Tempo des Bots
   
   ;Wenn Waypoint erreicht wurde, nächsten suchen und Bewegung darauf einstellen
   If Abs(WayPointX(WP) - PosX) =< Speed And Abs(WayPointY(WP) - PosY) =< Speed Then Winkel = NextWayPoint()
   
   WaitTimer timer
   Flip 0; Alles anzeigen
Until KeyHit(1) ; Wenn Taste 1 (Esc) gedrückt wird
End ; dann wird das Proggy beendet

Function NextWayPoint() ; NextWayPoint() Funktionsbeginn
   WP = WP + 1 ; Das der Punkt immer vom aktuellen eins weiter geht
   If WP>w Then WP = 1 ; Wenn einer mehr als der letzte erreicht wird, wieder zu Punkt 1
   Winkel# = (450 - ATan2((WayPointX(WP) - PosX), (WayPointY(WP) - PosY))) Mod 360 ; Winkel bestimmen
   Return Winkel# ; Zur Winkel# Funktion zurück
End Function ; Funktion beenden


mfG, Christoph

PS. ich bin ein Type-Fan, hab aber die Arrays dringelassen, weil sonst hätt ich gleich mein eigenes Tut schreiben können Smile

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group