cellules

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
case
Messages : 1546
Inscription : lun. 10/sept./2007 11:13

cellules

Message par case »

a la base un test pour gérer des déplacements d'objets sur un terrain en 'case par case'
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

ImageImage
Warkering
Messages : 808
Inscription : ven. 08/janv./2010 1:14
Localisation : Québec, Canada

Re: cellules

Message par Warkering »

Je dois avoir passer 10 minutes à le regarder en bavant sur le bureau! :lol:
C'est sympa!
Avatar de l’utilisateur
venom
Messages : 3138
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: cellules

Message par venom »

ah ouai c'est fort ces petites bébêtes qui avalent tout :lol:






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
G-Rom
Messages : 3641
Inscription : dim. 10/janv./2010 5:29

Re: cellules

Message par G-Rom »

Bien sympathoche ce p'tit bout de code ^^
Avatar de l’utilisateur
Ar-S
Messages : 9540
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: cellules

Message par Ar-S »

ouai j'aime ça ! Merci pour ce lobotomisator maison. :mrgreen:
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: cellules

Message par djes »

C'est un jeu de la vie, j'adore :)
Il faudrait que de temps en temps, il y ait une nouvelle génération ;)
Répondre