Bonjour,

Nous avons décidé de réaliser un casse brique en Python à l'aide module Tkinter.
Seulement voilà plusieurs jours que nous sommes bloqué sur un point. En effet la balle rebondit correctement sur la longueur de chaque rectangles (constituant les briques) mais pas sur la largeur. Cela implique plusieurs bugs puisque la balle entre à l'intérieur des rectangles tout en rebondissant sur la longueur de ces derniers.

Pouvez-vous m'aider à résoudre ce problème, je n'arrive vraiment pas à comprendre pourquoi la balle rebondit bien sur la longueur mais pas la largeur alors que j'ai utilisé le même procédé que pour la longueur.

PS : Nous n'avons pas l'autorisation d'utiliser le module PyGame.

Voici le programme (j'ai joint l'ensemble des images et .txt nécessaire à l'exécution) : https://drive.google.com/drive/folde...H8?usp=sharing

niveau.txtNom : BV.gif
Affichages : 220
Taille : 4,9 KoNom : BR.gif
Affichages : 226
Taille : 4,4 KoNom : BJ.gif
Affichages : 232
Taille : 3,9 KoThe Py Breaker.pyNom : Background.gif
Affichages : 253
Taille : 622,5 Ko

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
from tkinter import *
 
#INITIALISATION FENETRE + FOND
 
fenetre =Tk()
fenetre.title("The Py Breaker !")
fenetre.geometry("1100x800")
 
Fond=Canvas(fenetre,width=1100,height=800,bg="black")
Fond.place(x=0,y=0)
 
Background=PhotoImage(file="Background.gif")
Fond.create_image(0,0,image=Background,anchor="nw")
 
#INITIALISATION DU NIVEAU
 
#1
BV=PhotoImage(file="BV.gif")
x,y=0,0
fichier=open('niveau.txt')
V=[]
 
for ligne in fichier:
    for i in range(11): #Nb. caractères dans une ligne
        case=ligne[i]
        if case=="V":
            Fond.create_image(x,y,image=BV,anchor="nw")
            a=Fond.create_rectangle(x,y,x+99,y+49,outline="green")
            V.append(a)
        x=x+100
    x=0
    y=y+100
 
#2
BJ=PhotoImage(file="BJ.gif")
x,y=0,0
fichier=open('niveau.txt')
J=[]
 
for ligne in fichier:
    for i in range(11): #Nb. caractères dans une ligne
        case=ligne[i]
        if case=="J":
            Fond.create_image(x,y,image=BJ,anchor="nw")
            b=Fond.create_rectangle(x,y,x+99,y+49,outline="yellow")
            J.append(b)
        x=x+100
    x=0
    y=y+100
 
#3
BR=PhotoImage(file="BR.gif")
x,y=0,0
fichier=open('niveau.txt')
R=[]
 
for ligne in fichier:
    for i in range(11): #Nb. caractères dans une ligne
        case=ligne[i]
        if case=="R":
            Fond.create_image(x,y,image=BR,anchor="nw")
            c=Fond.create_rectangle(x,y,x+99,y+49,outline="red")
            R.append(c)
        x=x+100            
    x=0
    y=y+100
 
#INITIALISATION BARRE
 
Barre=Fond.create_rectangle(480,775,620,750,fill="black")
 
 
#INITIALISATION BALLE
 
Balle=Fond.create_oval(530,720,560,750,fill='red')
 
#REBODNDS BORDS ECRAN
 
def deplacement():
    global dx,dy
    if Fond.coords(Balle)[3]<=0:
        dy=-1*dy
    if (Fond.coords(Balle)[1]>800):
        dy=-1*dy
    if (Fond.coords(Balle)[0]<0) or (Fond.coords(Balle)[2]>1100):
        dx=-1*dx
 
 
#REBONDS BRIQUES VERTES
 
    if (Fond.coords(Balle)[1]<Fond.coords(V[0])[3]) and (Fond.coords(Balle)[0]<Fond.coords(V[0])[2]) and (Fond.coords(Balle)[2]>Fond.coords(V[0])[0]) and (Fond.coords(Balle)[3]>Fond.coords(V[0])[1]): 
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(V[1])[3]) and (Fond.coords(Balle)[0]<Fond.coords(V[1])[2]) and (Fond.coords(Balle)[2]>Fond.coords(V[1])[0]) and (Fond.coords(Balle)[3]>Fond.coords(V[1])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(V[2])[3]) and (Fond.coords(Balle)[0]<Fond.coords(V[2])[2]) and (Fond.coords(Balle)[2]>Fond.coords(V[2])[0]) and (Fond.coords(Balle)[3]>Fond.coords(V[2])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(V[3])[3]) and (Fond.coords(Balle)[0]<Fond.coords(V[3])[2]) and (Fond.coords(Balle)[2]>Fond.coords(V[3])[0]) and (Fond.coords(Balle)[3]>Fond.coords(V[3])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(V[4])[3]) and (Fond.coords(Balle)[0]<Fond.coords(V[4])[2]) and (Fond.coords(Balle)[2]>Fond.coords(V[4])[0]) and (Fond.coords(Balle)[3]>Fond.coords(V[4])[1]):
            dy=-1*dy
 
#REBONDS BRIQUES JAUNES
 
    if (Fond.coords(Balle)[1]<Fond.coords(J[0])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[0])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[0])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[0])[1]): 
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[1])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[1])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[1])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[1])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[2])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[2])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[2])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[2])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[3])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[3])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[3])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[3])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[4])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[4])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[4])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[4])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[5])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[5])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[5])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[5])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[6])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[6])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[6])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[6])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(J[7])[3]) and (Fond.coords(Balle)[0]<Fond.coords(J[7])[2]) and (Fond.coords(Balle)[2]>Fond.coords(J[7])[0]) and (Fond.coords(Balle)[3]>Fond.coords(J[7])[1]):
            dy=-1*dy
 
#REBONDS BRIQUES ROUGES
 
    if (Fond.coords(Balle)[1]<Fond.coords(R[0])[3]) and (Fond.coords(Balle)[0]<Fond.coords(R[0])[2]) and (Fond.coords(Balle)[2]>Fond.coords(R[0])[0]) and (Fond.coords(Balle)[3]>Fond.coords(R[0])[1]): 
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(R[1])[3]) and (Fond.coords(Balle)[0]<Fond.coords(R[1])[2]) and (Fond.coords(Balle)[2]>Fond.coords(R[1])[0]) and (Fond.coords(Balle)[3]>Fond.coords(R[1])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(R[2])[3]) and (Fond.coords(Balle)[0]<Fond.coords(R[2])[2]) and (Fond.coords(Balle)[2]>Fond.coords(R[2])[0]) and (Fond.coords(Balle)[3]>Fond.coords(R[2])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(R[3])[3]) and (Fond.coords(Balle)[0]<Fond.coords(R[3])[2]) and (Fond.coords(Balle)[2]>Fond.coords(R[3])[0]) and (Fond.coords(Balle)[3]>Fond.coords(R[3])[1]):
            dy=-1*dy
    if (Fond.coords(Balle)[1]<Fond.coords(R[4])[3]) and (Fond.coords(Balle)[0]<Fond.coords(R[4])[2]) and (Fond.coords(Balle)[2]>Fond.coords(R[4])[0]) and (Fond.coords(Balle)[3]>Fond.coords(R[4])[1]):
            dy=-1*dy
 
#REBONDS SUR BARRE
 
    if (Fond.coords(Balle)[3]>Fond.coords(Barre)[1]) and (Fond.coords(Balle)[0]<Fond.coords(Barre)[2]) and (Fond.coords(Balle)[2]>Fond.coords(Barre)[0]):
        dy=-1*dy
 
    Fond.move(Balle,dx,dy)
    fenetre.after(1,deplacement)
 
dx=-2
dy=-10
 
#DEPLACEMENT BARRE
 
def droite(event):
    Fond.move(Barre,15,0)
 
def gauche(event):
    Fond.move(Barre,-15,0)
 
Fond.bind_all('<Right>',droite)
Fond.bind_all('<Left>',gauche)
 
#LANCEMENT DU JEU
 
fichier.close()
deplacement()
fenetre.mainloop()