python - Collision Detection Problems and Invalid Command Name? -
from tkinter import * root = tk() drawpad = canvas(root, width=600,height=600, background='white') player = drawpad.create_rectangle(260,590,340,595, fill = "blue") ball = drawpad.create_oval(293,576,307,590, fill = "white") brick1 = drawpad.create_rectangle(30,20,80,50, fill='green') brick2 = drawpad.create_rectangle(30,100,80,130, fill='green') brick3 = drawpad.create_rectangle(30,180,80,210, fill='green') brick4 = drawpad.create_rectangle(100,20,150,50, fill='green') brick5 = drawpad.create_rectangle(100,100,150,130, fill='green') brick6 = drawpad.create_rectangle(100,180,150,210, fill='green') brick7 = drawpad.create_rectangle(170,20,220,50, fill='green') brick8 = drawpad.create_rectangle(170,100,220,130, fill='green') brick9 = drawpad.create_rectangle(170,180,220,210, fill='green') brick10= drawpad.create_rectangle(240,20,290,50, fill='green') brick11= drawpad.create_rectangle(240,100,290,130, fill='green') brick12= drawpad.create_rectangle(240,180,290,210, fill='green') brick13= drawpad.create_rectangle(310,20,360,50, fill='green') brick14= drawpad.create_rectangle(310,100,360,130, fill='green') brick15= drawpad.create_rectangle(310,180,360,210, fill='green') brick16= drawpad.create_rectangle(380,20,430,50, fill='green') brick17= drawpad.create_rectangle(380,100,430,130, fill='green') brick18= drawpad.create_rectangle(380,180,430,210, fill='green') brick19= drawpad.create_rectangle(450,20,500,50, fill='green') brick20= drawpad.create_rectangle(450,100,500,130, fill='green') brick21= drawpad.create_rectangle(450,180,500,210, fill='green') brick22= drawpad.create_rectangle(520,20,570,50, fill='green') brick23= drawpad.create_rectangle(520,100,570,130, fill='green') brick24= drawpad.create_rectangle(520,180,570,210, fill='green') bricka1 = drawpad.create_rectangle(60,60,110,90, fill='cyan') bricka2 = drawpad.create_rectangle(60,140,110,170, fill='cyan') bricka3 = drawpad.create_rectangle(130,60,180,90, fill='cyan') bricka4 = drawpad.create_rectangle(130,140,180,170, fill='cyan') bricka5 = drawpad.create_rectangle(200,60,250,90, fill='cyan') bricka6 = drawpad.create_rectangle(200,140,250,170, fill='cyan') bricka7 = drawpad.create_rectangle(270,60,320,90, fill='cyan') bricka8 = drawpad.create_rectangle(270,140,320,170, fill='cyan') bricka9 = drawpad.create_rectangle(340,60,390,90, fill='cyan') bricka10= drawpad.create_rectangle(340,140,390,170, fill='cyan') bricka11= drawpad.create_rectangle(410,60,460,90, fill='cyan') bricka12= drawpad.create_rectangle(410,140,460,170, fill='cyan') bricka13= drawpad.create_rectangle(480,60,530,90, fill='cyan') bricka14= drawpad.create_rectangle(480,140,530,170, fill='cyan') bricklist = [brick1,brick2,brick3,brick4,brick5,brick6,brick7,brick8,brick9,brick10,brick11,brick12,brick13,brick14,brick15,brick16,brick17,brick18,brick19,brick20,brick21,brick22,brick23,brick24,bricka1,bricka2,bricka3,bricka4,bricka5,bricka6,bricka7,bricka8,bricka9,bricka10,bricka11,bricka12,bricka13,bricka14] direction = 0 import random randangle = 0 angle = 0 overlap = 0 listplace = 0 length = 0 brick = 0 class myapp(object): def __init__(self, parent): global drawpad self.myparent = parent self.mycontainer1 = frame(parent) self.mycontainer1.pack() # score self.prompt = "score:" self.label1 = label(root, text=self.prompt, width=len(self.prompt), bg='green') self.label1.pack() self.score = 0 self.scoretxt = label(root, text=str(self.score), width=3, bg='green') self.scoretxt.pack() drawpad.pack() root.bind_all('<key>', self.key) self.animate() def animate(self): global drawpad global ball global direction global angle global randangle global listplace global brick x1,y1,x2,y2 = drawpad.coords(ball) px1,py1,px2,py2 = drawpad.coords(player) if y1 <= 0: direction = 5 elif x1 >= px1 , x2 <= px2 , y2 >= py1: direction = -5 randangle = random.randint(0,12) angle = randangle - 6 elif x1 <= 0 , y2 <= 600 or x2 >= 600 , y2 <= 600: angle = -angle didwehit = self.collisiondetect if didwehit == true: #for x in bricklist: # if x == brick: brick = bricklist[listplace] bx1,by1,bx2,by2 = drawpad.coords(brick) if x1 <= bx1 or x2 >= bx2: angle = -angle if y1 <= by1 or by2 >= y2: direction = -direction drawpad.delete(brick) drawpad.move(ball, angle, direction) drawpad.after(5,self.animate) def key(self,event): global drawpad global player global ball global direction x1,y1,x2,y2 = drawpad.coords(ball) px1,py1,px2,py2 = drawpad.coords(player) if event.char == " ": direction = -5 if event.char == "a": if x1 != 293 , y1 != 576 , x2 != 307 , y2 != 590 , px1 > 0: drawpad.move(player,-8,0) if event.char == "d": if x1 != 293 , y1 != 576 , x2 != 307 , y2 != 590 , px2 < 600: drawpad.move(player,8,0) def collisiondetect(self): global drawpad global bricklist global direction global angle global overlap global listplace global length x1,y1,x2,y2 = drawpad.coords(ball) overlap = drawpad.find_overlapping(x1,y1,x2,y2) length = len(overlap) if length > 1: listplace = overlap[1] - 3 return true self.score = self.score + 5 self.scoretxt.config(text=str(self.score)) app = myapp(root) root.mainloop()
for project in computer class creating brick breaker style game, , i'm done writing collision detection isn't working. there message when run program says:
invalid command name "182718608lcallit" while executing "182718608lcallit" ("after" script)
what mean , why isn't collision detection functioning?
okay edit: have different version of same code, know collision detection working score changes, bricks don't disappear , ball doesn't bounce. don't understand why following version has working score while first doesn't, , don't understand why neither of codes delete brick , bounce ball. code has similar error message, different numbers. can please explain, i'm new coding.
from tkinter import * root = tk() drawpad = canvas(root, width=600,height=600, background='white') player = drawpad.create_rectangle(260,590,340,595, fill = "blue") ball = drawpad.create_oval(293,576,307,590, fill = "white") brick1 = drawpad.create_rectangle(30,20,80,50, fill='green') brick2 = drawpad.create_rectangle(30,100,80,130, fill='green') brick3 = drawpad.create_rectangle(30,180,80,210, fill='green') brick4 = drawpad.create_rectangle(100,20,150,50, fill='green') brick5 = drawpad.create_rectangle(100,100,150,130, fill='green') brick6 = drawpad.create_rectangle(100,180,150,210, fill='green') brick7 = drawpad.create_rectangle(170,20,220,50, fill='green') brick8 = drawpad.create_rectangle(170,100,220,130, fill='green') brick9 = drawpad.create_rectangle(170,180,220,210, fill='green') brick10= drawpad.create_rectangle(240,20,290,50, fill='green') brick11= drawpad.create_rectangle(240,100,290,130, fill='green') brick12= drawpad.create_rectangle(240,180,290,210, fill='green') brick13= drawpad.create_rectangle(310,20,360,50, fill='green') brick14= drawpad.create_rectangle(310,100,360,130, fill='green') brick15= drawpad.create_rectangle(310,180,360,210, fill='green') brick16= drawpad.create_rectangle(380,20,430,50, fill='green') brick17= drawpad.create_rectangle(380,100,430,130, fill='green') brick18= drawpad.create_rectangle(380,180,430,210, fill='green') brick19= drawpad.create_rectangle(450,20,500,50, fill='green') brick20= drawpad.create_rectangle(450,100,500,130, fill='green') brick21= drawpad.create_rectangle(450,180,500,210, fill='green') brick22= drawpad.create_rectangle(520,20,570,50, fill='green') brick23= drawpad.create_rectangle(520,100,570,130, fill='green') brick24= drawpad.create_rectangle(520,180,570,210, fill='green') bricka1 = drawpad.create_rectangle(60,60,110,90, fill='cyan') bricka2 = drawpad.create_rectangle(60,140,110,170, fill='cyan') bricka3 = drawpad.create_rectangle(130,60,180,90, fill='cyan') bricka4 = drawpad.create_rectangle(130,140,180,170, fill='cyan') bricka5 = drawpad.create_rectangle(200,60,250,90, fill='cyan') bricka6 = drawpad.create_rectangle(200,140,250,170, fill='cyan') bricka7 = drawpad.create_rectangle(270,60,320,90, fill='cyan') bricka8 = drawpad.create_rectangle(270,140,320,170, fill='cyan') bricka9 = drawpad.create_rectangle(340,60,390,90, fill='cyan') bricka10= drawpad.create_rectangle(340,140,390,170, fill='cyan') bricka11= drawpad.create_rectangle(410,60,460,90, fill='cyan') bricka12= drawpad.create_rectangle(410,140,460,170, fill='cyan') bricka13= drawpad.create_rectangle(480,60,530,90, fill='cyan') bricka14= drawpad.create_rectangle(480,140,530,170, fill='cyan') bricklist = [brick1,brick2,brick3,brick4,brick5,brick6,brick7,brick8,brick9,brick10,brick11,brick12,brick13,brick14,brick15,brick16,brick17,brick18,brick19,brick20,brick21,brick22,brick23,brick24,bricka1,bricka2,bricka3,bricka4,bricka5,bricka6,bricka7,bricka8,bricka9,bricka10,bricka11,bricka12,bricka13,bricka14] direction = 0 import random randangle = 0 angle = 0 overlap = 0 listplace = 0 length = 0 brick = 0 class myapp(object): def __init__(self, parent): global drawpad self.myparent = parent self.mycontainer1 = frame(parent) self.mycontainer1.pack() # score self.prompt = "score:" self.label1 = label(root, text=self.prompt, width=len(self.prompt), bg='green') self.label1.pack() self.score = 0 self.scoretxt = label(root, text=str(self.score), width=3, bg='green') self.scoretxt.pack() drawpad.pack() root.bind_all('<key>', self.key) self.animate() def animate(self): global drawpad global ball global direction global angle global randangle global listplace global brick x1,y1,x2,y2 = drawpad.coords(ball) px1,py1,px2,py2 = drawpad.coords(player) if y1 <= 0: direction = 5 elif x1 >= px1 , x2 <= px2 , y2 >= py1: direction = -5 randangle = random.randint(0,12) angle = randangle - 6 elif x1 <= 0 , y2 <= 600 or x2 >= 600 , y2 <= 600: angle = -angle didwehit = self.collisiondetect() if didwehit == true: brick = bricklist[listplace] bx1,by1,bx2,by2 = drawpad.coords(brick) if x1 <= bx1 or x2 >= bx2: angle = -angle if y1 <= by1 or by2 >= y2: direction = -direction drawpad.move(ball, angle, direction) drawpad.after(5,self.animate) def key(self,event): global drawpad global player global ball global direction x1,y1,x2,y2 = drawpad.coords(ball) px1,py1,px2,py2 = drawpad.coords(player) if event.char == " ": direction = -5 if event.char == "a": if x1 != 293 , y1 != 576 , x2 != 307 , y2 != 590 , px1 > 0: drawpad.move(player,-8,0) if event.char == "d": if x1 != 293 , y1 != 576 , x2 != 307 , y2 != 590 , px2 < 600: drawpad.move(player,8,0) def collisiondetect(self): global drawpad global bricklist global direction global angle global overlap global listplace global length x1,y1,x2,y2 = drawpad.coords(ball) overlap = drawpad.find_overlapping(x1,y1,x2,y2) length = len(overlap) if length > 1: listplace = overlap[1] - 3 brick = bricklist[listplace] bx1,by1,bx2,by2 = drawpad.coords(brick) if x1 <= bx1 or x2 >= bx2: angle = -angle if y1 <= by1 or by2 >= y2: direction = -direction self.score = self.score + 5 self.scoretxt.config(text=str(self.score)) return true drawpad.delete(brick) app = myapp(root) root.mainloop()
your collisiondetect method deletes brick, try things brick later (in if didwehit
part of code). make sure wait delete brick until you've done need it!
in tkinter, kind of error occurs if try object has been deleted. tkinter assigns number each tk object creates, funny number in error got. trying execute command named 182718608lcallit
, doesn't exist more since deleted corresponding brick.
Comments
Post a Comment