Bonjour,
Je suis en train d'écrire (étant à la retraite c'est juste une exercice de style) un programme pour jouer au jeu Simon (4 couleurs, il faut reproduire la séquence de couleurs choisie par l'ordinateur). J'ai fini le jeu, en le testant j'ai trouvé un problème que je n'arrive pas à résoudre.
Ci-dessous le code entier en premier et en suivant un condensé pour tester les actions PC et joueur.
J'ai fait avec un canvas et quatre rectangles, je fais des "bind" sur les rectangles pour comptabiliser les actions du joueur.
Le jeu se lance en appuyant sur le bouton Jouer, la machine joue une fois, je joue en cliquant, la machine joue deux fois etc.
Cette partie fonctionne, sauf que si je clique pendant que la machine joue (à tester au delà du troisième choix machine), le clic (valeur r) est pris en compte alors que je suis ds une boucle ( for i in range(compte) pendant laquelle le canvas est .unbind (can1.unbind("<Button-1>").
J'ai essayé de changer la logique en faisant un timer (.after) sur la fonction JeuxPC, mais rien de plus...
Une solution?
[
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 from tkinter import * from tkinter import colorchooser from tkinter import messagebox import time import random TemPor=0.1 def Ombre(valCoul): global TemPor #print("***",valCoul) #print(listeClic[valCoul]) can1.itemconfig(valCoul,fill=listeCoulOmb[valCoul-1]) can1.update_idletasks() time.sleep(TemPor) can1.itemconfig(valCoul,fill=listeCoul[valCoul-1]) can1.update_idletasks() time.sleep(TemPor) def JeuxPC(evt): global compte global compteJH global flag global listeClic print(flag) if flag=="jeuPC": #IdAfter=can1.after(1,JeuxPC(evt=None)) message['text']=" PC joue..." can1.unbind("<Button-1>") compte=compte+1 valCoul=random.randint(1,4) listeClic.append(valCoul) print(listeClic) for i in range(compte): #print (i," ",compte) #print(listeClic[i]) Ombre(listeClic[i]) time.sleep(TemPor) #Ombre(valCoul) flag="jeuHM" message['text']=" Humain joue..." compteJH=0 #compte=compte+1 can1.bind("<Button-1>",JeuxPC) #IdAfter=can1.after(10,JeuxPC(evt)) else: print ("compteJH=",compteJH,"compte=",compte) #if compteJH==0: #can1.bind("<Button-1>",JeuxPC) #print("JE BIND") if compteJH <compte: r = can1.find_overlapping(evt.x, evt.y, evt.x, evt.y) compteJH+=1 print ("compteJH=",compteJH) print("Clic=",listeCoul[r[0]-1]) print("listeClic=",listeClic[compteJH-1]) fen.bell() if r[0]!=listeClic[compteJH-1]: messagebox.showinfo("PERDU","PERDUUU...") can1.unbind("<Button-1>") b1.config(state='normal') compteJH=0 compte=0 listeClic[:]=[] message['text']=" En attente" return if compteJH==compte: time.sleep(TemPor*10) flag="jeuPC" #IdAfter=can1.after(10,JeuxPC(evt)) JeuxPC(evt=None) def LancerJeux(): b1.config(state='disabled') JeuxPC(evt=None) listeCoul=['red','blue','green','yellow'] listeCoulOmb=['#d93199','#0090d9','#00d9c4','#f4d9a6'] listeClic=[] ht=800 lg=800 #cp =colorchooser.askcolor() fen=Tk() compte=0 flag="jeuPC" can1=Canvas(fen,width=lg, height=ht) can1.grid(row=0,column=0) for countLig in range(0,2): for countCol in range(0,2): #coulF=listeCoul(2*countCol) i = can1.create_rectangle(lg*countCol/2,ht*countLig/2,(countCol+1)*ht/2,(countLig+1)*lg/2,fill=listeCoul[2*countLig+countCol]) #print(i) b1=Button(fen, text="Jouer",command=LancerJeux) b1.grid(row=4,column=0,columnspan=2,sticky="NSWE") b2=Button(fen, text="Sol") b2.grid(row=5,column=0,columnspan=2,sticky="NSWE") message = Label(fen,font=('sans', 20, 'bold'),text="En attente") message.grid(row=3,column=0,columnspan=2,sticky="NSWE") fen.mainloop()
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 from tkinter import * from tkinter import colorchooser from tkinter import messagebox import time import random def Ombre(valCoul): #print("***",valCoul) #print(listeClic[valCoul]) can1.itemconfig(valCoul,fill=listeCoulOmb[valCoul-1]) can1.update_idletasks() time.sleep(0.1) can1.itemconfig(valCoul,fill=listeCoul[valCoul-1]) def JeuxPC(evt): global compte global compteJH global flag global listeClic if flag=="jeuPC": # #IdAfter=can1.after(1,JeuxPC(evt=None)) print(flag) #can1.unbind("<Button-1>") compte=compte+1 for i in range(compte): print (compte) time.sleep(1) can1.bind("<Button-1>",JeuxPC) flag="jeuHM" compteJH=0 else: print(flag) print ("compteJH=",compteJH,"compte=",compte) r = can1.find_overlapping(evt.x, evt.y, evt.x, evt.y) print("r=",r) if compteJH <compte: compteJH+=1 print ("compteJH=",compteJH) if compteJH==compte: flag="jeuPC" can1.unbind("<Button-1>") #IdAfter=can1.after(1,JeuxPC(evt=None)) JeuxPC(evt=None) def LancerJeux(): JeuxPC(evt=None) listeCoul=['red','blue','green','yellow'] listeCoulOmb=['#d93199','#0090d9','#00d9c4','#f4d9a6'] listeClic=[] ht=800 lg=800 #cp =colorchooser.askcolor() fen=Tk() compte=0 flag="jeuPC" can1=Canvas(fen,width=lg, height=ht) can1.grid(row=0,column=0) for countLig in range(0,2): for countCol in range(0,2): #coulF=listeCoul(2*countCol) i = can1.create_rectangle(lg*countCol/2,ht*countLig/2,(countCol+1)*ht/2,(countLig+1)*lg/2,fill=listeCoul[2*countLig+countCol]) #print(i) b1=Button(fen, text="Jouer",command=LancerJeux) b1.grid(row=4,column=0,columnspan=2,sticky="NSWE") b2=Button(fen, text="Sol") b2.grid(row=5,column=0,columnspan=2,sticky="NSWE") message = Label(fen,font=('sans', 20, 'bold'),text="En attente") message.grid(row=3,column=0,columnspan=2,sticky="NSWE") fen.mainloop()
Partager