Page 1 sur 1

[R] Collecter l'ID d'un bouton cliqué

Posté : ven. 15 juil. 2016 15:35
par GhostLine
Salut à tous !

A des fins de formation (à dispenser à d'autres), j'ai entrepris ce matin de réaliser un petit match-3 (type Candy Crush, pour ceux qui ne seraient pas familiers avec le terme).

J'ai essayé de bien faire mon panneau de boutons (première fois dans des arrays, yay ! ca valait le coup de m'engueuler, les gars :)), les contrôles fonctionnent ... mais je ne sais pas comment faire pour récupérer la valeur du bouton cliqué (sans déclarer des case pour chaque, ce qui ferait un code bien indigeste).

Si vous avez des idées ou correction (en oubliant pas que le programme servira de support pour un cour à des grands débutants, donc pas de compilations d'expression svp :)), je suis preneur !

Merci d'avance !


[edit : rajout des icônes en pièce jointe]

[codeautoit]#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $tableau[4][4]
Global $interface[4][4]
Global $points = 0

Global $Form1 = GUICreate("Match3 --- "&$points&" points", 332, 328, 1832, 168)
$interface[0][0] = GUICtrlCreateButton("", 8, 8, 75, 73, $BS_ICON)
$interface[0][1] = GUICtrlCreateButton("", 88, 8, 75, 73, $BS_ICON)
$interface[0][2] = GUICtrlCreateButton("", 168, 8, 75, 73, $BS_ICON)
$interface[0][3] = GUICtrlCreateButton("", 248, 8, 75, 73, $BS_ICON)
$interface[1][0] = GUICtrlCreateButton("", 8, 88, 75, 73, $BS_ICON)
$interface[1][1] = GUICtrlCreateButton("", 88, 88, 75, 73, $BS_ICON)
$interface[1][2] = GUICtrlCreateButton("", 168, 88, 75, 73, $BS_ICON)
$interface[1][3] = GUICtrlCreateButton("", 248, 88, 75, 73, $BS_ICON)
$interface[2][0] = GUICtrlCreateButton("", 8, 168, 75, 73, $BS_ICON)
$interface[2][1] = GUICtrlCreateButton("", 88, 168, 75, 73, $BS_ICON)
$interface[2][2] = GUICtrlCreateButton("", 168, 168, 75, 73, $BS_ICON)
$interface[2][3] = GUICtrlCreateButton("", 248, 168, 75, 73, $BS_ICON)
$interface[3][0] = GUICtrlCreateButton("", 8, 248, 75, 73, $BS_ICON)
$interface[3][1] = GUICtrlCreateButton("", 88, 248, 75, 73, $BS_ICON)
$interface[3][2] = GUICtrlCreateButton("", 168, 248, 75, 73, $BS_ICON)
$interface[3][3] = GUICtrlCreateButton("", 248, 248, 75, 73, $BS_ICON)
Generation_tableau()
Affichage_tableau()
GUISetState(@SW_SHOW)
Controle()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit


EndSwitch
WEnd

Func Generation_tableau()
For $i = 0 To 3
For $j = 0 To 3
$tableau[$i][$j] = Random(1, 4, 1)
Next
Next
EndFunc ;==>Generation_tableau

Func Affichage_tableau()
For $i = 0 To 3
For $j = 0 To 3
GUICtrlSetImage($interface[$i][$j], @ScriptDir & "\" & $tableau[$i][$j] & ".ico")
Next
Next
EndFunc ;==>Affichage_tableau

Func Controle()
Do
$points_tmp = $points
;~ -------------------------------------------------------------------------------
;~ Si plus de trois symboles identiques sont détectés côtes à côtes, on les efface
;~ Barême : 3 en ligne = 1 point, 4 en ligne = 3 points (idem pour les colonnes)
;~ -------------------------------------------------------------------------------
For $i = 0 To 3 ; on commence par les colonnes
If $tableau[0][$i] = $tableau[1][$i] And $tableau[1][$i] = $tableau[2][$i] Then
$tableau[0][$i] = Random(1, 4, 1)
$tableau[1][$i] = Random(1, 4, 1)
$tableau[2][$i] = Random(1, 4, 1)
$points = 1 + $points
ElseIf $tableau[1][$i] = $tableau[2][$i] And $tableau[2][$i] = $tableau[3][$i] Then
$tableau[3][$i] = $tableau[0][$i]
$tableau[0][$i] = Random(1, 4, 1)
$tableau[1][$i] = Random(1, 4, 1)
$tableau[2][$i] = Random(1, 4, 1)
$points = 1 + $points
ElseIf $tableau[0][$i] = $tableau[1][$i] And $tableau[1][$i] = $tableau[2][$i] And $tableau[2][$i] = $tableau[3][$i] Then
$tableau[0][$i] = Random(1, 4, 1)
$tableau[1][$i] = Random(1, 4, 1)
$tableau[2][$i] = Random(1, 4, 1)
$tableau[3][$i] = Random(1, 4, 1)
$points = 3 + $points
EndIf
Next
Sleep(500)
WinSetTitle($Form1,"","Match3 --- "&$points&" points")
Affichage_tableau()

For $j = 0 To 3 ; puis les lignes
If $tableau[$j][0] = $tableau[$j][1] And $tableau[$j][1] = $tableau[$j][2] Then
For $k = $j To 0 Step -1
If $k - 1 >= 0 Then
$tableau[$k][0] = $tableau[$k - 1][0]
$tableau[$k][1] = $tableau[$k - 1][1]
$tableau[$k][2] = $tableau[$k - 1][2]
Else
$tableau[$k][0] = Random(1, 4, 1)
$tableau[$k][1] = Random(1, 4, 1)
$tableau[$k][2] = Random(1, 4, 1)
EndIf
Next
$points = 1 + $points
ElseIf $tableau[$j][1] = $tableau[$j][2] And $tableau[$j][2] = $tableau[$j][3] Then
For $k = $j To 0 Step -1
If $k - 1 >= 0 Then
$tableau[$k][1] = $tableau[$k - 1][1]
$tableau[$k][2] = $tableau[$k - 1][2]
$tableau[$k][3] = $tableau[$k - 1][3]
Else
$tableau[$k][1] = Random(1, 4, 1)
$tableau[$k][2] = Random(1, 4, 1)
$tableau[$k][3] = Random(1, 4, 1)
EndIf
Next
$points = 1 + $points
ElseIf $tableau[$j][0] = $tableau[$j][1] And $tableau[$j][1] = $tableau[$j][2] = $tableau[$j][3] Then
For $k = $j To 0 Step -1
If $k - 1 >= 0 Then
$tableau[$k][0] = $tableau[$k - 1][0]
$tableau[$k][1] = $tableau[$k - 1][1]
$tableau[$k][2] = $tableau[$k - 1][2]
$tableau[$k][3] = $tableau[$k - 1][3]
Else
$tableau[$k][0] = Random(1, 4, 1)
$tableau[$k][1] = Random(1, 4, 1)
$tableau[$k][2] = Random(1, 4, 1)
$tableau[$k][3] = Random(1, 4, 1)
EndIf
Next
$points = 3 + $points
EndIf
Next
Sleep(500)
WinSetTitle($Form1,"","Match3 --- "&$points&" points")
Affichage_tableau()
;~ -------------------------------------------------------------------------------
;~ On laisse tourner la fonction jusqu'à ce qu'après deux vérifications successives,
;~ on obtienne le même nombre de points (càd que plus rien n'est détecté).
Until $points_tmp = $points
;~ -------------------------------------------------------------------------------
Return $points
EndFunc ;==>Controle[/codeautoit]

Re: [..] Collecter l'ID d'un bouton cliqué

Posté : ven. 15 juil. 2016 18:17
par orax
Difficile de répondre puisque je ne sais pas exactement quel sera le code pour chaque bouton.
J'ai fait une fonction qui est activée quand on clique sur un des boutons ; elle récupère certaines informations sur le bouton (numéro, ligne, colonne).
[codeautoit]While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $interface[0][0] To $interface[3][3]
Faire_quelque_chose($nMsg)
EndSwitch
WEnd

Func Faire_quelque_chose($idBouton)
$numBouton = $idBouton - $interface[0][0]
ConsoleWrite("Numéro = " & $numBouton + 1 & @CRLF)
ConsoleWrite("Ligne = " & Int($numBouton / 4) + 1 & @CRLF)
ConsoleWrite("Colonne = " & Mod($numBouton, 4) + 1 & @CRLF)
ConsoleWrite("-----------" & @CRLF)
EndFunc ;==>Faire_quelque_chose[/codeautoit]

Re: [..] Collecter l'ID d'un bouton cliqué

Posté : ven. 15 juil. 2016 18:24
par GhostLine
:shock:

Case $interface[0][0] To $interface[3][3]

Je ne savais pas que l'on pouvais faire ça ! C'est super green, comme dirait l'autre (j'ai revu le Cinquième Élément hier soir ^^) !