Bonjour a tous !

Je débute en python avec excellent livre "Apprendre à programmer avec Python" de Gérard Swinnen, c'est très interresant.
Bon, je m'amuse je teste des petits programmes etc... et alors j'ai essayé d'en faire un où l'on devrait cliquer sur des balles, afin de les faire disparaitre avant qu'elles n'arrivent au bord de l'écran (classique) mais bon...

Voilà le code :

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
176
177
178
179
180
181
182
183
184
# -*- coding:Utf-8 -*-
#!/usr/bin/python
 
####
 
############
# Importation des fonctions externes
from Tkinter import *
from random import randrange
 
############
 
####################
# Définition locale de fonctions
####################
 
def couleur():
        pal=['purple','cyan','maroon','green','red','blue','orange','yellow']
        c = randrange(8)
        return pal[c]
 
def move():
        "Déplace la balle"
        global x1, y1, dx, dy, flag, score, vague, oval
        if oval[0]:
 
                x1[0], y1[0] = x1[0] + dx, y1[0] + dy
                if x1[0] > 500:
                        score = score -1
                        chaineScore.configure(text = "score :" +  str(score))
                        can1.delete(oval[0])
                        flag[0] = 0
                        x1[0] =10 
                        oval[0] = 0
                else:
                        can1.coords (oval[0], x1[0], y1[0], x1[0]+20, y1[0]+20)
        else:
                y1[0] = randrange(10,220)
                oval[0] = can1.create_oval(x1[0],y1[0],x1[0]+20,y1[0]+20,width=1,fill=couleur())
                vague = vague + 1
                chaineVague.configure(text='Attaquants : ' + str(vague))
                flag[0] = 1
        if flag[0] > 0:
                fen1.after(50, move)
 
def move2():
        "Déplace la balle"
        global x1, y1, dx, dy, flag, score, vague, oval
        if oval[1]:
 
                x1[1], y1[1] = x1[1] + dx, y1[1] + dy
                if x1[1] > 500:
                        score = score -1
                        chaineScore.configure(text = "score :" +  str(score))
                        can1.delete(oval[1])
                        flag[1] = 0
                        x1[1] =10 
                        oval[1] = 0
                else:
                        can1.coords (oval[1], x1[1], y1[1], x1[1]+20, y1[1]+20)
        else:
                y1[1] = randrange(10,220)
                oval[1] = can1.create_oval(x1[1],y1[1],x1[1]+20,y1[1]+20,width=1,fill=couleur())
                vague = vague + 1
                chaineVague.configure(text='Attaquants : ' + str(vague))
                flag[1] = 1
        if flag[1] > 0:
                fen1.after(50, move2)
 
def move3():
        "Déplace la balle"
        global x1, y1, dx, dy, flag, score, vague, oval
        if oval[2]:
 
                x1[2], y1[2] = x1[2] + dx, y1[2] + dy
                if x1[2] > 500:
                        score = score -1
                        chaineScore.configure(text = "score :" +  str(score))
                        can1.delete(oval[2])
                        flag[2] = 0
                        x1[2] =10 
                        oval[2] = 0
                else:
                        can1.coords (oval[2], x1[2], y1[2], x1[2]+20, y1[2]+20)
        else:
                y1[2] = randrange(10,220)
                oval[2] = can1.create_oval(x1[2],y1[2],x1[2]+20,y1[2]+20,width=1,fill=couleur())
                vague = vague + 1
                chaineVague.configure(text='Attaquants : ' + str(vague))
                flag[2] = 1
        if flag[2] > 0:
                fen1.after(50, move3)
 
def stop_it():
        "Stoppe l'animation"
        global flag
        flag = [0]*3
 
def start_it():
        "Démarre l'animation"
        global flag, test
        if flag[0] == 0:
                flag[0] = 1
                move()
        if flag[1] == 0:
                flag[1] = 1
                move2()
        if flag[2] == 0:
                flag[2] = 1
                move3()
        fen1.after(2000, start_it) #et tu boucle :p
 
 
def finDePartie():
        global test, score, flag
        if score <= 0:
                fen2 = Tk()
                fen2.title('PERDU')
                Label(fen2, text='PERDU !').pack()
                Button(fen2, text='Quitter', command=fen1.quit).pack(side=BOTTOM)
                test = 0
                flag = [0]*3
                fen2.mainloop()
 
        if test == 1:
                fen1.after(50, finDePartie) #et tu boucle :p
 
def testClic(event):
        global x1, y1, flag, oval
        if event.x > x1[0] and event.x < x1[0]+30 and event.y > y1[0] and event.y < y1[0]+30:
                can1.delete(oval[0])
                flag[0] = 0
                x1[0] =10 
                oval[0] = 0
        if event.x > x1[1] and event.x < x1[1]+30 and event.y > y1[1] and event.y < y1[1]+30:
                can1.delete(oval[1])
                flag[1] = 0
                x1[1] =10
                oval[1] = 0
        if event.x > x1[2] and event.x < x1[2]+30 and event.y > y1[2] and event.y < y1[2]+30:
                can1.delete(oval[2])
                flag[2] = 0
                x1[2] =10
                oval[2] = 0
 
########################
#Corps principal du programme
########################
 
 #Variables globales :
x1 = [10]*3
y1 = [randrange(10,220), randrange(10,220), randrange(10,220)]
flag = [0]*3
oval = [0]*3
dx, dy = 5, 0#Pas du déplacement
test = 1
score = 10
vague = 0 
 
#Fenêtre
fen1 = Tk()
fen1.title("Invasion")
 
#Widgets
can1 = Canvas(fen1,bg="dark grey", height=250,width=505)
can1.bind("<Button-1>", testClic)
can1.pack(side=LEFT, padx =5, pady =5)
 
#can1.create_rectangle(500,0,505,250,width=0,fill='green')
 
 
chaineScore = Label(fen1, text='Score : 10')
chaineScore.pack(side=BOTTOM)
 
chaineVague = Label(fen1, text='Attaquants : ' + str(vague))
chaineVague.pack(side=BOTTOM)
 
Button(fen1, text='Quitter', command=fen1.quit).pack(side=BOTTOM)
Button(fen1, text='Go !', command=start_it).pack()
Button(fen1, text='Stop !', command=stop_it).pack()
 
# Démarrage du réceptionnaire d'évènements
finDePartie()
fen1.mainloop()

Ouhhhhh.... que c'est moche C'est codé avec les pied... je sais ! Et en plus c'est très lourd. Mais bon, je l'ai codé comme ça au fur et à meusure que je lisais le livre par petits bouts, en partant de l'exercice ou l'on doit faire bouger une boule

Quelques chapitres plus tard, après avoir ingurgité de gros morceaux de théorie bien indigeste, j'essaye de le recoder en POO

et voilà :
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
 
# -*- coding:Utf-8 -*-
#!/usr/bin/python
 
####
 
############
# Importation des fonctions externes
from Tkinter import *
from random import randrange
 
############
 
 
####################
# Définition des CLASSES
####################
 
class Attaquant:
        "Un attaquant"
        def __init__(self):
                self.x1 = 10
                self.y1 = randrange(10,220)
                self.flag = 0
                self.oval = 0
                self.vitesseX = 5
                self.taille = 20
                self.pal=['purple','cyan','maroon','green','red','blue','orange','yellow']
                self.c = randrange(8)
                self.couleur = self.pal[self.c]
 
        def attaque(self):
                "Créer et déplace l'attaquant"
                global score, vague
                if self.oval:
                        self.bouge()
                        if self.x1 > 500:
                                score = score -1
                                chaineScore.configure(text = "Vie : " +  str(score))
                                self.tuer()
                        self.miseAjour()
 
                else:
                        self.apparait()
                        vague = vague + 1
                        chaineVague.configure(text='Attaquants : ' + str(vague))
                if self.flag > 0:
                        fen1.after(50, self.attaque())
 
        def tuer(self):
                "Détruit l'attaquant"
                zone.delete(self.oval)
                self.flag = 0
                self.x1 =10 
                self.oval = 0
 
        def bouge(self):
                "Incrémente les coordonnées de l'attaquant pour le faire avancer"
                self.x1 = self.x1 + self.vitesseX
 
 
        def miseAjour(self):
                "Met à jour la position de l'attaquant"
                zone.coords(self.oval, self.x1, self.y1,  self.x1+self.taille, self.y1+self.taille)
 
        def apparait(self):
                "Créer l'attaquant "
                self.y1 = randrange(10,220)
                self.oval = zone.create_oval(self.x1, self.y1, self.x1+self.taille, self.y1+self.taille,width=1,fill=self.couleur)
                self.flag = 1
 
 
        def stop(self):
                "Arrête l'attaquant"
                self.flag = 0
 
####################
# Définition locale de fonctions
####################
 
def start_it():
        "Démarre le jeu"
        a1 = Attaquant()
        a1.attaque()
 
 
def finDePartie():
        "Teste le score et termine la partie"
        global test, score
        if score <= 0:
                fen2 = Tk()
                fen2.title('PERDU')
                Label(fen2, text='PERDU !').pack()
                Button(fen2, text='Quitter', command=fen1.quit).pack(side=BOTTOM)
                test = 0
                fen2.mainloop()
        if test == 1:
                fen1.after(50, finDePartie) #et tu boucle :p
 
def testClic(event):
        "Teste le clic"
        if event.x > a1.x1 and event.x < a1.x1+a1.taille and event.y > a1.y1 and event.y < a1.y1+a1.taille:
                a1.tuer()
 
 
 
########################
#Corps principal du programme
########################
 
 #Variables globales :
test = 1
score = 10
vague = 0 
 
#Fenêtre
fen1 = Tk()
fen1.title("Invasion")
 
#Widgets
zone = Canvas(fen1,bg="dark grey", height=250,width=505)
zone.bind("<Button-1>", testClic)
zone.pack(side=LEFT, padx =5, pady =5)
 
chaineScore = Label(fen1, text='Vie: 10')
chaineScore.pack(side=BOTTOM)
 
chaineVague = Label(fen1, text='Attaquants : ' + str(vague))
chaineVague.pack(side=BOTTOM)
 
baseVerte = zone.create_rectangle(500,0,505,250,width=0,fill='green')
 
Button(fen1, text='Quitter', command=fen1.quit).pack(side=BOTTOM)
Button(fen1, text='Jouer', command=start_it).pack()
 
# Démarrage du réceptionnaire d'évènements
finDePartie()
fen1.mainloop()

Seulement quand je clique sur "GO" le jeu bugue (le boutton reste enfoncé) et enfin il reprend mais, le bonhomme et déjà passé, donc le score est diminué, le nombre d'attaquants aussi mais on a pas pu jouer... pas cool !

Je pense que c'est parce que la procédure "start_it" attend que toutes les opérations soit effectuées avant de redonner la main... comment faire pour résoudre ce problème ? Merci d'avance