python - Collision Detection in a List -


i'm new collision detection , have been working on brick breaker style game tkinter. each of bricks created separately instead of creating if statements each of 30+ bricks put them in list, when run program collision detection function doesn't work. can please don't understand wrong in code.

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  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=len(str(self.score)), 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         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:             if (x1 >= bx1 , x2 <=bx2 , y1 >= by2) or (x1 >= bx1 , x2 <=bx2 , y2 <= by1):                 direction = -direction             if (x1 >= bx1 , y2 <= by1 , y1 >= by2) or (x2 <=bx2 , y1 >= by2 , y2 <= by1):                 angle = -angle             didwehit = false          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         x1,y1,x2,y2 = drawpad.coords(ball)         brick in bricklist:             bx1,by1,bx2,by2 = drawpad.coords(brick)             if x1 >= bx1 , x2 <= bx2 , y1 >= by2 , y2 <= by1:                 return true                 drawpad.delete(brick)                 self.score = self.score + 5                 self.scoretxt.config(text=str(self.score))  app = myapp(root) root.mainloop() 


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -