Bonjour, voilà je suis dans la réalisation d'un jeu de labyrinthe en Pascal avec une interface graphique avec GTK.
Je bloque sur 2 difficultés :
- Comment retourner dans une fonction un tableau ? (le tableau à retourner est tabtest que l'on écrit à la fin du code créant le labyrinthe)
- Comment afficher à l'interieur d'une fenêtre créée avec GTK des carrés de différentes couleurs afin de modéliser le labyrinthe généré précédemment ?
Voici le code GTK
Le fichier pascal créant le labyrinthe
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 program labyrinthe; uses glib2, gtk2,Laby2v0; procedure choix_taille; var pFenetre : PGtkWidget; pVBox : PGtkWidget; pBtn : PGtkWidget; begin gtk_init(@argc, @argv); pFenetre := gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_position(GTK_WINDOW(pFenetre), GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(pFenetre), 80, 30); gtk_container_set_border_width(GTK_CONTAINER(pFenetre), 4); gtk_window_set_title(GTK_WINDOW(pFenetre), 'Taille'); gtk_signal_connect(pGTKOBJECT(pFenetre), 'destroy', GTK_SIGNAL_FUNC(@gtk_main_quit), NULL); pVBox := gtk_vbox_new(TRUE, 0); gtk_container_add(GTK_CONTAINER(pFenetre), pVBox); // Création du bouton 1 pBtn := gtk_button_new_with_label('Petit'); gtk_box_pack_start(GTK_BOX(pVBox), pBtn, TRUE, FALSE, 5); g_signal_connect(pGTKOBJECT(pBtn), 'clicked', GTK_SIGNAL_FUNC(@dessinpetit), NULL); // Création du bouton 2 pBtn := gtk_button_new_with_label('Moyen'); gtk_box_pack_start(GTK_BOX(pVBox), pBtn, TRUE, FALSE, 5); g_signal_connect(pGTKOBJECT(pBtn), 'clicked', GTK_SIGNAL_FUNC(@dessinmoyen), NULL); // Création du bouton 3 pBtn := gtk_button_new_with_label('Grand'); gtk_box_pack_start(GTK_BOX(pVBox), pBtn, TRUE, FALSE, 5); g_signal_connect(pGTKOBJECT(pBtn), 'clicked', GTK_SIGNAL_FUNC(@dessingrand), NULL); gtk_widget_show_all(pFenetre); gtk_main; end; var pFenetre : PGtkWidget; pVBox : PGtkWidget; pBarreMenu : PGtkWidget; pMenuItemFichier : PGtkWidget; pMenuFichier : PGtkWidget; pMenuItemNouveau : PGtkWidget; pMenuItemSauvegarder : PGtkWidget; pMenuItemQuitter : PGtkWidget; pMenuSeparateur : PGtkWidget; pImage : PGtkWidget; pTable : PGtkWidget; begin gtk_init(@argc, @argv); pFenetre := gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_position(GTK_WINDOW(pFenetre), GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(pFenetre), 320, 200); gtk_window_set_title(GTK_WINDOW(pFenetre), 'Labyrinthe'); gtk_signal_connect(pGTKOBJECT(pFenetre), 'destroy', GTK_SIGNAL_FUNC(@gtk_main_quit), NULL); // Création de la GtkVBox pVBox := gtk_vbox_new(FALSE, 5); gtk_container_add(GTK_CONTAINER(pFenetre), pVBox); // Création de la barre de menu pBarreMenu := gtk_menu_bar_new; gtk_box_pack_start(GTK_BOX(pVBox), pBarreMenu, FALSE, FALSE, 0); // Création de l'item Fichier et rattachement à la barre de menu pMenuItemFichier := gtk_menu_item_new_with_label('Fichier'); gtk_menu_shell_append(GTK_MENU_SHELL(pBarreMenu), pMenuItemFichier); // Création du menu Fichier et rattachement à l'item Fichier pMenuFichier := gtk_menu_new; gtk_menu_item_set_submenu(GTK_MENU_ITEM(pMenuItemFichier), pMenuFichier); // Création de l'item Nouveau et rattachement au menu Fichier pMenuItemNouveau := gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, NULL); gtk_menu_shell_append(GTK_MENU_SHELL(pMenuFichier), pMenuItemNouveau); g_signal_connect(pGTKOBJECT(pMenuItemNouveau), 'activate', G_CALLBACK(@choix_taille), NULL); // Création de l'item sauvegarder et rattachement au menu Fichier pMenuItemSauvegarder := gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE, NULL); gtk_menu_shell_append(GTK_MENU_SHELL(pMenuFichier), pMenuItemSauvegarder); // Création du séparateur pMenuSeparateur := gtk_separator_menu_item_new; gtk_menu_shell_append(GTK_MENU_SHELL(pMenuFichier), pMenuSeparateur); pTable := gtk_table_new (10, 10, TRUE); gtk_container_add (GTK_CONTAINER (pFenetre), pTable); pImage := gtk_image_new_from_file ('./23.bmp'); gtk_table_attach_defaults (GTK_TABLE (pTable), pImage, 0, 10, 0, 10); // Création de l'item Quitter et rattachement au menu Fichier pMenuItemQuitter := gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL); gtk_menu_shell_append(GTK_MENU_SHELL(pMenuFichier), pMenuItemQuitter); g_signal_connect(pGTKOBJECT(pMenuItemQuitter), 'activate', G_CALLBACK(@gtk_main_quit), NULL); gtk_widget_show_all(pFenetre); gtk_main; end.
Et enfin l'unité fonctionessai.pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 unit Laby2v0; INTERFACE uses fonctionsessai; procedure Tableau; IMPLEMENTATION procedure Tableau; var tabtest : array [1..10,1..10] of integer; ld, cd, i, j, nbpas, ca, la, x, lig, col,resultat, n, o, s, e, cptblocage : integer; v0,v1,v2,v3, essai4, arrivee : boolean ; begin randomize; cptblocage:=0; arrivee:=false; repeat {initialisation} for i := 1 to 10 do begin for j := 1 to 10 do begin tabtest[i,j]:=0; end; end; {Création de la case de départ} repeat ld:=Hasard(1,10); cd:=Hasard(1,10); until (ld=1) or (ld=10) or (cd=1) or (cd=10); tabtest[ld,cd]:=1; {Génération aléatoire du chemin} la:=ld; ca:=cd; nbpas:=0; repeat v0:=false; v1:=false; v2:=false; v3:=false; essai4:=false; repeat {recherche d'une nouvelle case} repeat {recherche d'une direction} x:=Hasard(0,3); lig:=ProchaineCaseLigne(la,x); col:=ProchaineCaseColonne(ca,x); if la=1 then begin v0:=true; end; if ca=1 then begin v3:=true; end; if la=10 then begin v2:=true; end; if ca=10 then begin v1:=true; end; until (lig>0) and (col>0); {direction acceptée car case dans matrice} case x of 0 : begin v0 := true; end; 1 : begin v1 := true; end; 2 : begin v2 := true; end; 3 : begin v3 := true; end; end; {Calcul somme des cases voisines} n:=0; s:=0; o:=0; e:=0; if lig>1 then begin n:=tabtest[lig-1,col]; end; if col>1 then begin o:=tabtest[lig,col-1]; end; if lig<10 then begin s:=tabtest[lig+1,col]; end; if col<10 then begin e:=tabtest[lig,col+1]; end; resultat:=n+o+s+e; if (resultat=1) and (nbpas=0) then begin resultat:=2; end; {Fin du calcul} if not ((resultat=2) and (tabtest[lig,col]<>1)) then begin essai4 := (v0) and (v1) and (v2) and (v3); end; until ((resultat=2) and (tabtest[lig,col]<>1)) or (essai4); {nouvelle case acceptée} if not essai4 then begin la:=lig; ca:=col; nbpas:=nbpas+1; tabtest[la,ca]:=2; end; until (((la=1)or(la=10)or(ca=1)or(ca=10))and(ca<>cd)and(la<>ld)and(nbpas>34))or(essai4); if essai4 then begin cptblocage:=cptblocage+1; end else begin tabtest[la,ca]:=3; arrivee:=true; end; until arrivee; writeln(nbpas); {Affichage du tableau} for i := 1 to 10 do begin for j := 1 to 10 do begin if tabtest[i,j]=2 then begin write('- '); end else begin write(tabtest[i,j],' '); end ; end; writeln(''); end; writeln('Blocages : ',cptblocage); end; END.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 unit fonctionsessai; INTERFACE function Hasard(deb,fin : integer) : integer; function ProchaineCase(caseact, a : integer) : integer; function ProchaineCaseLigne(lig, a : integer) : integer; function ProchaineCaseColonne(col, a : integer) : integer; IMPLEMENTATION function Hasard(deb,fin : integer) : integer; begin Hasard:=random(fin-deb+1)+deb; end; function ProchaineCase(caseact, a : integer) : integer; var casetest:integer; begin case a of 0 : begin if caseact>10 then begin casetest:=caseact-10; end else begin casetest:=0; end ; end ; 1 : begin if caseact mod 10 =0 then begin casetest:=0; end else begin casetest:=caseact+1; end end ; 2 : begin if caseact<91 then begin casetest:=caseact+10; end else begin casetest:=0; end ; end ; 3 : begin if caseact mod 10 =1 then begin casetest:=0;end else begin casetest:=caseact-1; end end ; end; ProchaineCase:=casetest; end; function ProchaineCaseLigne(lig, a : integer) : integer; var ligne : integer; begin ligne:=lig; case a of 0 : begin if lig>1 then begin ligne:=lig-1; end else begin ligne:=0; end end ; 2 : begin if lig<10 then begin ligne:=lig+1; end else begin ligne:=0; end end ; end ; ProchaineCaseLigne:=ligne; end; function ProchaineCaseColonne(col, a : integer) : integer; var colonne : integer; begin colonne:=col; case a of 3 : begin if col>1 then begin colonne:=col-1; end else begin colonne:=0; end end ; 1 : begin if col<10 then begin colonne:=col+1; end else begin colonne:=0; end end ; end ; ProchaineCaseColonne:=colonne; end; END.
Partager