finalement un truc sympa a regarder...
Code : Tout sélectionner
Declare redraw()
Declare change_prey()
InitSprite()
InitSprite3D()
InitKeyboard()
InitMouse()
Structure cell
tile.i
x.f; coord x
y.f; coord y
;
xd.i; destination x
yd.i; destination y
;
xp.f; pas x
yp.f; pas y
;
calcxd.i
calcyd.i
hunt.i ; cible
EndStructure
Structure food
tile.i
x.i
y.i
EndStructure
ExamineDesktops()
main=OpenWindow(#PB_Any,0,0,DesktopWidth(0),DesktopHeight(0),"",#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(main),0,0,DesktopWidth(0),DesktopHeight(0),0,0,0)
Global NewList cells.cell()
Global NewList foods.food()
;
;
;
Global s=16 ; taille carré
Global boardx=(DesktopWidth(0)-1)/s ; terrain
Global boardy=(DesktopHeight(0)-1)/s ; terrain
Global number=((boardx*boardy)*2)/100 ; nourriture
Global timer=10 ; rafraichissement
Global cellules=2 ; cellules
Global Dim tile(10)
StartDrawing(ScreenOutput())
Box(0,0,s,s,$777777)
Box(s*1,0,s,s,$444444)
Box(s*2,0,s,s,$ff0000)
Box(s*3,0,s,s,$0000ff)
Box(s*4,0,s,s,$00ff00)
Box(s*5,0,s,s,$00ffff)
Box(s*6,0,s,s,$ff00ff)
StopDrawing()
For til=0 To 6
tile(til)=GrabSprite(#PB_Any,til*s,0,s,s)
Next
For m=1 To cellules
AddElement(cells())
cells()\x=Random(boardx)
cells()\y=Random(boardy)
cells()\tile=4
Next
;
;
nd=99
AddWindowTimer(main,1,timer)
Repeat
ExamineKeyboard()
ev=WaitWindowEvent()
Select ev
Case #PB_Event_Timer
ForEach cells()
If cells()\hunt=0
change_prey()
;
If cells()\hunt=0 ; remplis a nouveau la liste
For a= 1 To number
Repeat
ForEach(foods())
xx=Random(boardx)
yy=Random(boardy)
If foods()\x=xx And foods()\y=yy
Break
EndIf
Next
If ListSize(foods())>0
If foods()\x<>xx Or foods()\y<>yy
Break
EndIf
Else
Break
EndIf
ForEver
AddElement(foods())
foods()\x=xx
foods()\y=yy
foods()\tile=3
Next
EndIf
Else
If ListSize(foods())>0 And cells()\hunt-1<ListSize(foods())
SelectElement(foods(),cells()\hunt-1)
If foods()\x<>cells()\xd Or foods()\y<>cells()\yd ;a bougé ou nouvelle chasse
dx=(foods()\x-cells()\x)
dy=(foods()\y-cells()\y)
If Abs(dx)>Abs(dy) ; recherche le nombre de pas
steps=Abs(dx)
Else
steps=Abs(dy)
EndIf
cells()\xp=dx/steps
cells()\yp=dy/steps
cells()\xd=foods()\x
cells()\yd=foods()\y
EndIf
EndIf
If (Round(cells()\x+cells()\xp,#PB_Round_Nearest))>=0 And (Round(cells()\x+cells()\xp,#PB_Round_Nearest))<=boardx
cells()\x+cells()\xp
EndIf
If (Round(cells()\y+cells()\yp,#PB_Round_Nearest))>=0 And (Round(cells()\y+cells()\yp,#PB_Round_Nearest))<=boardy
cells()\y+cells()\yp
EndIf
If Round(cells()\x,#PB_Round_Nearest)=foods()\x And Round(cells()\y,#PB_Round_Nearest)=foods()\y
actual=ListIndex(cells())
hunted=cells()\hunt
DeleteElement(foods(),1)
ForEach cells()
If cells()\hunt=hunted
cells()\hunt=0
cells()\xd=-1
cells()\yd=-1
cells()\xp=-1
cells()\yp=-1
cells()\x=Round(cells()\x,#PB_Round_Nearest)
cells()\y=Round(cells()\y,#PB_Round_Nearest)
EndIf
If cells()\hunt>hunted
cells()\hunt -1
EndIf
If cells()\hunt=0
change_prey()
EndIf
Next
SelectElement(cells(),actual)
EndIf
EndIf
Next
EndSelect
redraw()
FlipBuffers()
Until ev=#PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
Procedure redraw()
til=0
add=1
ClearScreen(0)
ForEach foods()
DisplaySprite(tile(foods()\tile),foods()\x*s,foods()\y*s)
Next
ForEach cells()
DisplaySprite(tile(cells()\tile),Round(cells()\x,#PB_Round_Nearest)*s,Round(cells()\y,#PB_Round_Nearest)*s)
DisplaySprite(tile(5),cells()\xd*s,cells()\yd*s)
Next
EndProcedure
Procedure change_prey()
nd=999999
ForEach foods() ; recherche du joueur le plus proche
dx=Abs(Round(cells()\x,#PB_Round_Nearest)-foods()\x)
dy=Abs(Round(cells()\y,#PB_Round_Nearest)-foods()\y)
If dx>dy
d=dx
Else
d=dy
EndIf
If nd>d
nd=d
cells()\hunt=ListIndex(foods())+1
EndIf
Next
EndProcedure