'Python help on TKinter
i'm trying to make an brick destroyer game on python with TKinter for my school project, it's barely finished but i have a problem when it's about to break some bricks. It says that the list index is out of range and i don't know how can i make this.
import tkinter
import time
from tkinter import *
fen = Tk()
fen.title("Casse brique")
bou1 = Button(fen, text="Quitter", command=fen.destroy)
can1 = Canvas(fen,bg="Black" ,height=900 ,width=1300)
x1 = 490
y1 = 750
def avance(gd, hb):
global x1,y1
x1,y1 = (x1 + gd) % 1000, (y1+hb) % 1000
can1.coords(bar,x1,y1,x1+300,y1+30)
def depl_droit(event=None):
avance(20,0)
def depl_gauche(event=None):
avance(-20,0)
can1.bind_all("<Right>",depl_droit)
can1.bind_all("<Left>",depl_gauche)
can1.pack()
bou1.pack(padx=5, pady=5)
a = 490
b = 750
c = 790
d = 780
bar = can1.create_rectangle(a,b,c,d, fill='white')
def start():
start_button.config(state=DISABLED)
Balle = can1.create_rectangle(405, 405, 435, 435, fill="Blue")#x0 y0 x2 y2
brique1 = can1.create_rectangle(50,100,250,180, fill='white')#x0 y0 x2 y2
X_Axis_Speed = 5
Y_Axis_Speed = 5
while True:
can1.move(Balle,X_Axis_Speed,Y_Axis_Speed)
pos_balle = can1.coords(Balle)
if pos_balle[2]>1300 or pos_balle[0]<=0: #droite --- gauche
X_Axis_Speed = -X_Axis_Speed
if pos_balle[1]<=0: # top
Y_Axis_Speed = -Y_Axis_Speed
if (can1.coords(Balle)[3]==can1.coords(bar)[1]) and can1.coords(Balle)[0]<can1.coords(bar)[2] and can1.coords(Balle)[2]>can1.coords(bar)[0]: #barre
Y_Axis_Speed = -Y_Axis_Speed
if pos_balle[3]==900: #bot
can1.delete(Balle)
start_button.config(state=NORMAL)
lose = Toplevel(fen)
lose.geometry('150x150')
rt = Button(lose,text="Réessayer",command=lose.destroy)
label = Label(lose, text="Tu as perdu ! réessaye")
label.pack()
rt.pack()
return
if (pos_balle[0]==can1.coords(brique1)[2]) and pos_balle[3]<can1.coords(brique1)[3]:
can1.delete(brique1)
fen.update()
time.sleep(0.01)
start_button = Button(fen,text="Démarrer", command=start)
start_button.pack()
fen.mainloop()
Thanks in advance ^^ (Sorry for the variable name i'm french and my teacher will think that i find on internet if i wrote it in english)
Solution 1:[1]
I modified your line 58 so I could break when the error happens. The pos_balle list was sufficiently long. The other list was returned from a function call so I added the var 'x' to capture it. It is an empty list. This is not your root problem and I'm not familiar with using canvases so that's as far as I went. I wanted to share with you to show you a little of debug techiniques and maybe this will point you in the right direction. Good luck.
try:
x = can1.coords(brique1)
if (pos_balle[0]==can1.coords(brique1)[2]) and pos_balle[3]<can1.coords(brique1)[3]:
can1.delete(brique1)
except Exception as e:
print(e)
pass
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ron Lewis |