
Eine witzige Simulation:
Man Stellt sich ein leeres Blatt vor. Auf dieses Blatt setzt man eine imagimäre
Ameise.
Move:
Die Ameise macht einen schritt vorwärts. Ist der Punkt unter ihr weiss, so färbt sie den Punkt schwarz und dreht sich nach links. Ist der Punkt aber schwarz, färbt sie ihn weiss und dreht sich nach rechts.
Dann, um es in PB zu sagen: "Goto Move:"

Info: Die Rechts-links-Drehung wäre auch mit Winkeln zu realisieren gewesen, aber die im Code verwendete Änderung ist ein bisschen Laufzeit-effizienter.
Nach dem Start muss "p" gedrückt werden.
Code: Alles auswählen
Enumeration
#Window
#Pause
#Image
#Print
#Slow
EndEnumeration
OpenWindow(#Window,0,0,600,400,"Ameisensimulation",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
AddKeyboardShortcut(#Window,#PB_Shortcut_P,#Pause)
AddKeyboardShortcut(#Window,#PB_Shortcut_D,#Print)
AddKeyboardShortcut(#Window,#PB_Shortcut_S,#Slow)
CreateImage(#Image,600,400)
StartDrawing(ImageOutput(#Image))
Box(0,0,600,400,RGB(255,255,255))
StopDrawing()
StartDrawing(WindowOutput(#Window))
Box(0,0,600,400,RGB(255,255,255))
StopDrawing()
;x=1
;antx=360
;anty=370
;sehr chaotisch:
x=1
antx=80
anty=100
;x=1
;antx=20
;anty=20
;x=1
;antx=370
;anty=370
;x=-1
;antx=30
;anty=30
;y=1
;antx=20
;anty=20
;y=1
;antx=380
;anty=200
;y=-1
;antx=32
;anty=390
;x=1
;antx=2
;anty=394
Repeat
event=WindowEvent()
Select event
Case #PB_Event_MoveWindow
pause=1
Case #PB_Event_Repaint
StartDrawing(WindowOutput(#Window))
DrawImage(ImageID(#Image),0,0)
StopDrawing()
Case #PB_Event_Menu
Select EventMenu()
Case #Print
SaveImage(#Image,"Ameise.bmp")
Case #Pause
If pause=0
pause=1
Delay(100)
Else
pause=0
Delay(100)
EndIf
Case #Slow
If slow=0
slow=1
Delay(100)
Else
slow=0
Delay(100)
EndIf
EndSelect
EndSelect
If pause=0
Gosub MoveAnt
EndIf
If slow=1
Delay(1)
EndIf
Until event=#PB_Event_CloseWindow
End
MoveAnt:
StartDrawing(WindowOutput(#Window))
Color=Point(AntX,AntY)
If Color=RGB(255,255,255)
Color=RGB(0,0,0)
Else
Color=RGB(255,255,255)
EndIf
Plot(AntX,AntY,Color)
StopDrawing()
StartDrawing(ImageOutput(#Image))
Plot(AntX,AntY,Color)
StopDrawing()
If Color=RGB(255,255,255)
;links
If x=-1
x=0
y=1
ElseIf x=1
x=0
y=-1
ElseIf y=-1
x=-1
y=0
ElseIf y=1
x=1
y=0
EndIf
Else
;rechts
If x=-1
x=0
y=-1
ElseIf x=1
x=0
y=1
ElseIf y=-1
x=1
y=0
ElseIf y=1
x=-1
y=0
EndIf
EndIf
If AntX>598
x=-1
y=0
AntX-2
If AntY<2
AntY+1
ElseIf AntY>398
AntY-1
EndIf
ElseIf AntX<2
x=1
y=0
AntX+2
If AntY<2
AntY+1
ElseIf AntY>398
AntY-1
EndIf
ElseIf AntY>398
x=0
y=-1
AntY-2
If AntX<2
AntX+1
ElseIf AntX>598
AntX-1
EndIf
ElseIf AntY<2
x=0
y=1
AntY+2
If AntY<2
AntY+1
ElseIf AntY>398
AntY-1
EndIf
Else
AntX+x
Anty+y
EndIf
Return