Ich hab heute mal wieder ein Game of Life geschrieben, das aber sehr langsam läuft. Hat jemand Vorschläge, da zu optimieren?
Code: Alles auswählen
InitSprite()
InitKeyboard()
InitMouse()
#bb=1280
#bh=800
breite.w=10
w=#bb/breite
h=#bh/breite
array=0
Dim f(w,h,1)
;Dim f2(w,h)
OpenScreen(#bb,#bh,16,"")
For y = 0 To h
For x =0 To w
f(x,y,array)=0;Random(1)
Next
Next
;- bild
CreateSprite(0,breite,breite,0)
StartDrawing(SpriteOutput(0))
Box(0,0,breite,breite,RGB(13, 27, 24))
StopDrawing()
#maus=1
CreateSprite(#maus,11,16)
StartDrawing(SpriteOutput(#maus))
FrontColor(RGB(20,0,0))
BackColor(RGB(0,0,0))
LineXY(0,0,0,15)
LineXY(1,15,5,11)
LineXY(5,11,11,11)
LineXY(11,11,0,0)
FillArea(2,5,RGB(20,0,0),RGB(20,0,0))
TransparentSpriteColor(#maus,RGB(0,0,0))
StopDrawing()
Procedure bound(i.w,a.w,b.w)
If i<a
i+b
EndIf
If i>b
i-b
EndIf
ProcedureReturn i
EndProcedure
Repeat
ExamineKeyboard()
maus = MouseButton(1)
ExamineMouse()
ClearScreen(RGB(255,255,255))
For y = 0 To h
For x =0 To w
If f(x,y,array)
DisplaySprite(0,x*breite,y*breite)
EndIf
If g
livingaround=0
livingaround+f(bound(x-1,0,w),bound(y-1,0,h),array)
livingaround+f(bound(x,0,w),bound(y-1,0,h),array)
livingaround+f(bound(x+1,0,w),bound(y-1,0,h),array)
livingaround+f(bound(x-1,0,w),bound(y,0,h),array)
livingaround+f(bound(x+1,0,w),bound(y,0,h),array)
livingaround+f(bound(x-1,0,w),bound(y+1,0,h),array)
livingaround+f(bound(x,0,w),bound(y+1,0,h),array)
livingaround+f(bound(x+1,0,w),bound(y+1,0,h),array)
If (livingaround=2 And f(x,y,array)=1) Or livingaround=3
f(x,y,1-array)=1
Else
f(x,y,1-array)=0
EndIf
EndIf
Next
Next
If g
array=1-array
EndIf
DisplayTransparentSprite(#maus,MouseX(),MouseY())
If KeyboardReleased(57)
g=1-g
EndIf
If MouseButton(1)
If KeyboardPushed(#PB_Key_N)
f(MouseX()/breite,MouseY()/breite, array)=0
Else
f(MouseX()/breite,MouseY()/breite, array)=1
EndIf
EndIf
If KeyboardPushed(#PB_Key_B)
For i = 0 To w*h/100
f(Random(w),Random(h),array)=1
Next
EndIf
FlipBuffers()
Until KeyboardPushed(1)