Sélectionner un carré (Jaune, vert ou rouge) et déplacer le sur un emplacement libre (En gris).
Code : Tout sélectionner
Enumeration Form
#MainForm
EndEnumeration
Enumeration Sprite
#Empty
#Square_Red
#Square_Green
#Square_Yellow
#Cursor
EndEnumeration
Global Dim Sprites(4, 4), Col, Row, SelCol=-1, SelRow=-1 ;5 Colonnes et 5 lignes
Procedure GamePreload()
Protected BorderColor = RGB(128, 128, 128)
;Creation du sprite vide
CreateSprite(#Empty, 100, 100)
StartDrawing(SpriteOutput(#Empty))
Box(0, 0, 100, 100, BorderColor)
RoundBox(2, 2, 96, 96, 10, 10, BorderColor)
StopDrawing()
;Creation du carré Rouge
CreateSprite(#Square_Red, 100, 100)
StartDrawing(SpriteOutput(#Square_Red))
Box(0, 0, 100, 100, BorderColor)
RoundBox(2, 2, 96, 96, 10, 10, RGB(255, 0, 0))
StopDrawing()
;Creation du carré Vert
CreateSprite(#Square_Green, 100, 100)
StartDrawing(SpriteOutput(#Square_Green))
Box(0, 0, 100, 100, BorderColor)
RoundBox(2, 2, 96, 96, 10, 10, RGB(50, 205, 50))
StopDrawing()
;Creation du carré Jaune
CreateSprite(#Square_Yellow, 100, 100)
StartDrawing(SpriteOutput(#Square_Yellow))
Box(0, 0, 100, 100, BorderColor)
RoundBox(2, 2, 96, 96, 10, 10, RGB(255, 215, 0))
StopDrawing()
;Creation du curseur de sélection
CreateSprite(#Cursor, 25, 25)
StartDrawing(SpriteOutput(#Cursor))
Box(0, 0, 25, 25, RGB(250, 128, 114))
StopDrawing()
EndProcedure
Procedure GameUpdate()
Protected Mx = MouseX()
Protected My = MouseY()
;Affichage des différents carrés de couleurs
For Col = 0 To 4
For Row = 0 To 4
DisplaySprite(Sprites(Col, Row), Col*100, Row*100)
Next
Next
;Affichage du curseur
DisplayTransparentSprite(#Cursor, Mx, My, 128)
If MouseButton(#PB_MouseButton_Left)
;Le bouton gauche de la souris est préssé :
; ? Il y a un sprite sue cette case (Sprites(Col, Row) <> 0)
; ? ET il n'y a pas de sprite en cours de selection (SelCol = -1)
; OUI => Mémorisation des coordonnées du sprite
For Col = 0 To 4
For Row = 0 To 4
If Sprites(Col, Row) <> 0 And (Mx > Col*100 And Mx < (Col+1)*100) And (My > Row*100 And My < (Row+1)*100) And SelCol = -1
SelCol = Col
SelRow = Row
EndIf
Next
Next
Else
;Le bouton de la souris est relaché :
; ? La Case ou se trouve la souris est libre
; ? ET Relacher un sprite (SelCol <> -1)
; OUI ==> Permutation des cellules
For Col = 0 To 4
For Row = 0 To 4
If Sprites(Col, Row) = 0 And (Mx > Col*100 And Mx < (Col+1)*100) And (My > Row*100 And My < (Row+1)*100) And SelCol <> -1
Swap Sprites(Col, Row), Sprites(SelCol, SelRow)
EndIf
Next
Next
;Plus aucune sélection en cours
SelCol = -1
SelRow = -1
EndIf
EndProcedure
Procedure GameStart()
Protected cr.b = #True
Protected Width = 500
Protected Height = 500
;Initialisation de l'environnement 2D
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 And InitSound() = 0
MessageRequester("Error", "Sprite system can't be initialized", 0)
cr=#False
EndIf
If OpenWindow(#mainform, 0, 0, Width, Height, "New Game", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(#mainform), 0, 0, Width, Height)
;Creation des sprites
GamePreload()
;Ajout des sprites dans le jeu
For Col=0 To 4
For Row=0 To 4
Sprites(Col, Row) = Random(3, 0)
Next
Next
EndIf
EndIf
;-Boucle evenementielle
Repeat
Repeat
Event = WindowEvent()
Select event
Case #PB_Event_CloseWindow
End
EndSelect
Until event=0
FlipBuffers()
ClearScreen(RGB(0,0,0))
ExamineKeyboard()
ExamineMouse()
GameUpdate()
Until KeyboardPushed(#PB_Key_Escape)
EndProcedure
GameStart()