Bonjour à tous. Mon fils a copié ce programme dans le livre Python for kids mais il semble ne pas fonctionner. En fait le jeu s'affiche correctement mais impossible de bouger quoi que ce soit et ce message d'erreur s'affiche dans le shell :

Traceback (most recent call last):
File "I:\filiforme\programme.py", line 279, in <module>
jeu.boucle_principale()
File "I:\filiforme\programme.py", line 31, in boucle_principale
lutin.deplacer()
AttributeError: 'LutinPlateForme' object has no attribute 'deplacer'


Bon voici le programme :

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from tkinter import *
import random
import time
 
class Jeu:
    def __init__(self):
        self.tk=Tk()
        self.tk.title("Agent Filiforme")
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas=Canvas(self.tk, width=500, height=500,\
                           highlightthickness=0)
        self.canvas.pack()
        self.tk.update()
        self.hauteur_canvas=500
        self.largeur_canvas=500
        self.ap=PhotoImage(file="i:/filiforme/fon1.gif")
        larg=self.ap.width()
        haut=self.ap.height()
        for x in range(0, 5):
            for y in range(0, 5):
                self.canvas.create_image(x * larg, y * haut,\
                                         image=self.ap, anchor='nw' )
        self.lutins =[]
        self.enfonction = True
 
    def boucle_principale(self):
        while 1:
          if self.enfonction == True:
            for lutin in self.lutins:
                lutin.deplacer()
            self.tk.update_idletasks()
            self.tk.update()
            time.sleep(0.01)
 
class Coords:
    def __init__(self, x1=0, y1=0, x2=0, y2=0):
        self.x1=x1
        self.y1=y1
        self.x2=x2
        self.y2=y2
 
    def dans_x(co1, co2):
     if(co1.x1 > co2.x1 and co1.x1 <co.x2)\
                        or(co1.x2>co2.x1 and co2.x1<co.x2)\
                        or(co2.x1>co1.x1 and co2.x1<co1.x2)\
                        or(co2.x2>co1.x1 and co2.x2<co1.x1):
        return True
     else:
        return False
 
    def dans_y(co1, co2):
        if(co1.y1>co2.y1 and co1.y2<co2.y2)\
                         or (co1.y2>co2.y1 and co1.y2<co2.y2)\
                         or(co2.y1>co1.y1 and co1.y1<co2.y2)\
                         or(co2.y2>co1.y1 and co2.y2<co1.y1):
            return True
        else:
            return False
    def collision_gauche(co1, co2):
        if dans_y(co1, co2):
            if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
                return True
            return False
 
    def collision_droite(co1, co2):
        if dans_y(co1, co2):
            if co1.x2 >= co2.x1 and co1.x2<=co2.y2:
               return True
        return False
 
    def collision_haut(co1, co2):
        if dans_x(co1, co2):
            if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
                return True
            return False
    def collision_bas(y, co1,co2):
        if dans_x(co1, co2):
            y_calc = co1.y2 + y
            if y_calc >= co2.y1 and y_calc <= co2.y2:
                return True
            return False
 
class Lutin:
    def __init__(self, jeu):
        self.jeu=jeu
        self.finjeu = False
        self.coordonees = None
 
 
 
 
class LutinPlateForme(Lutin):
    def __init__(self, jeu, image_photo, x, y, largeur, hauteur):
        Lutin.__init__(self, jeu)
        self.image_photo = image_photo
        self.image = jeu.canvas.create_image(x, y,\
                                           image=self.image_photo, anchor= 'nw')
        self.coordonnees=Coords(x, y, x + largeur, y + hauteur)
 
class LutinPersonnage(Lutin):
    def __init__(self,jeu):
        Lutin.__init__(self,jeu)
        self.images_gauche=[
            PhotoImage(file="i://filiforme/fil21.gif"),
            PhotoImage(file="i://filiforme/fil22.gif"),
            PhotoImage(file="i://filiforme/fil23.gif")
                        ]
        self.images_droite=[
            PhotoImage(file="i://filiforme/fil_01.gif"),
            PhotoImage(file="i://filiforme/fil02.gif"),
            PhotoImage(file="i://filiforme/fil03.gif")
                        ]
        self.image=jeu.canvas.create_image(200, 470,\
                                           image=self.images_gauche[0],anchor="nw")
        self.x=-2
        self.y=0
        self.image_courante=0
        self.ajout_image_courante=1
        self.compte_sauts=0
        self.derniere_heure=time.time()
        self.coordonnees=Coords()
        jeu.canvas.bind_all('<KeyPress-Left>',self.tourner_a_gauche)
        jeu.canvas.bind_all("<KeyPress-Right>",self.tourner_a_droite)
        jeu.canvas.bind_all('<space>',self.sauter)
 
    def tourner_a_gauche(self,evt):
        if self.y == 0:
            self.x = -2
 
    def tourner_a_droite(self,evt):
        if self.y == 0:
            self.x = 2
 
    def sauter(self,evt):
        if self.y == 0:
            self.x=-4
            self.compte_sauts=0
 
    def animer(self):
        if self.x!=0 and self.y==0:
         if time.time() - self.derniere_heure>0.1:
            self.derniere_heure =time.time()
            self.image_courante+=self.ajout_image_courante
            if self.image_courante>=2:
                self.ajout_image_courante=-1
            if self.image_courante<=0:
                self.ajout_image_courante=1
        if self.x<0:
            if self.y!=0:
                self.jeu.canvas.itemconfig(self.image,\
                                           image=self.images_gauche[2])
            else:
                    self.jeu.canvas.itemconfig(self.image,\
                                               image=self.images_gauche[self.image_courante])
        elif self.x>0:
            if self.y!=0:
                self.jeu.canvas.itemconfig(self.image,\
                                           image=self.images_droite[2])
            else:
                    self.jeu.canvas.itemconfig(self.image,\
                                               image=self.images_droite[self.image_courante])
 
    def coords(self):
        xy=self.jeu.canvas.coords(self.image)
        self.coordonnees.x1=xy[0]
        self.coordonnees.y1=xy[1]
        self.coordonnees.x2=xy[0]+27
        self.coordonnees.y2=xy[1]+30
        return self.coordonnees
 
    def deplacer(self):
        self.animer()
        if self.y<0:
            self.compte_sauts+=1
            if self.compte_sauts>20:
                self.y=4
        if self.y>0:
            self.compte_sauts-=1
        co=self.coords()
        gauche=True
        droite=True
        haut=True
        bas=True
        tombe=True
        if self.y>0 and co.y2>=self.jeu.hauteur_canevas:
                    self.y=0
                    bas=False
        elif self.y<0 and co.y1<=0:
                    self.y=0
                    haut = False
        if self.x>0 and co.x2>=self.jeu.largeur_canevas:
                    self.x=0
                    droite=False
        elif self.x<0 and co.x1<=0:
                    self.x=0
                    gauche=False
        for lutin in self.jeu.lutins:
                    if lutin == self:
                        continue
                    co_lutin=lutin.coords()
                    if haut and self.y<0 and collision_haut(co,co_lutin):
                        self.y= -self.y
                        haut=False
                    if bas and self.y>0 \
                            and collision_bas(self.y,co,co_lutin):
                        self.y=co_lutin.y1-co.y2
                        if self.y<0:
                            self.y=0
                        bas=False
                        haut=False
                    if bas and tombe and self.y==0\
                       and co.y2<self.jeu.hauteur_canevas\
                       and collision_bas(1,co,co_lutin):
                        tombe=False
                    if gauche and self.x<0 and collision_gauche(co,co_lutin):
                        self.x=0
                        gauche=False
                        if lutin.finjeu:
                            self.jeu.enfonction=False
                    if droite and self.x>0 and collision_droite(co,co_lutin):
                        self.x=0
                        droite=False
                        if lutin.finjeu:
                            self.jeu.enfonction = False
        if tombe and bas and self.y == 0 \
                   and co.y2<self.jeu.hauteur_canevas:
                self.y=4
        self.jeu.canvas.move(self.image,self.x,self.y)
 
class LutinPorte(Lutin):
      def __init__(self,jeu,image_photo,x,y,largeur,hauteur):
          Lutin.__init__(self,jeu)
          self.image_photo=image_photo
          self.image=jeu.canvas.create_image(x,y,\
                                             image=self.image_photo,anchor='nw')
          self.coordonnees = Coords(x,y,x+(largeur/2),y+hauteur)
          self.finjeu=True
 
#mettre les classes avant
jeu = Jeu()
platforme1=LutinPlateForme(jeu, PhotoImage(file='i://filiforme/platforme100.gif'), \
                          0, 480,  100, 10)
platforme2=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme100.gif"), \
                            150, 440, 100, 10)
platforme3=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme100.gif"), \
                            300,400,100,10)
platforme4=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme100.gif"), \
                           300,160,100,10)
platforme5=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme60.gif"), \
                           175, 350, 66, 10)
platforme6=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme60.gif"), \
                           50, 300, 66,10)
platforme7=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme60.gif"), \
                          170, 120, 66, 10)
platforme8=LutinPlateForme(jeu, PhotoImage(file="i://filiforme/platforme60.gif"),\
                           45, 60,66,10)
platforme9=LutinPlateForme(jeu,PhotoImage(file="i://filiforme/platforme30.gif"),\
                           170,250,32,10)
platforme10=LutinPlateForme(jeu,PhotoImage(file="i://filiforme/platforme30.gif"),\
                            230,200,32,10)
 
 
 
jeu.lutins.append(platforme1)
jeu.lutins.append(platforme2)
jeu.lutins.append(platforme3)
jeu.lutins.append(platforme4)
jeu.lutins.append(platforme5)
jeu.lutins.append(platforme6)
jeu.lutins.append(platforme7)
jeu.lutins.append(platforme8)
jeu.lutins.append(platforme9)
jeu.lutins.append(platforme10)
porte=LutinPorte(jeu,PhotoImage(file='porte fermée.gif'),45,30,40,35)
jeu.lutins.append(porte)
personnage=LutinPersonnage(jeu)
jeu.lutins.append(personnage)
jeu.boucle_principale()

J'espère que quelqu'un pourra nous aider. Merci d'avance.