Collision problems in Tkinter (game)

Asked

Viewed 110 times

0

I’m learning Tkinter developing a little game but I’m in trouble to erase objects collided with find.overlapping

from tkinter import *
from constantes import *

import random 
class Jogo():
     def __init__(self):

          #Criar tela principal

          self.root = Tk()
          self.root.geometry('%ix%i' %(LARGURA, ALTURA))
          self.root.title('Arcade')
          self.root.resizable(False,False)

          #Criar frame para conter o canvas

          self.frame = Frame(bg='blue')
          self.frame.pack()

          #Criar canvas

          self.canvas = Canvas(self.frame, bg='blue', width=CANVAS_L, height=CANVAS_A, cursor='target')
          self.canvas.pack()

          #Criando objetos dentro do canvas

          #self.canvas.create_line(10,10,390,390,            fill='white')

          self.comecar = Button(self.root, text='INICIAR', command=self.comecar )
          self.comecar.pack()

          self.novoJogo()


     def novoJogo(self):
          #Criar objetos do jogo
          self.player = self.canvas.create_rectangle(195,360,280,375, fill='white')
               #Criar a bolinha
          raio = 29
          p = (100,200)
          self.ovo = self.canvas.create_oval(p[0],p[1],p[0]+raio,p[1]+raio, fill='grey',)
          #Velocidade da bola
          self.b_vx = 7
          self.b_vy = 7
          #Posição da bola
          self.b_x, self.b_y = p

          x_inicial, y_inicial = 4, 22
          altura, largura = 20, 77
          espaçamento = 2

          self.retangulos = []

          for colunas in range(5):
               for linhas in range(5):
                    cor = random.choice(['red','white','green','brown','orange','yellow','black','darkred','lightyellow','darkorange','darkgreen','','','',])
                    x1 = x_inicial+(largura+espaçamento)*colunas
                    x2 = x1+largura
                    y1 = y_inicial+(altura+espaçamento)*linhas
                    y2 = y1+altura

                    retangulos = self.canvas.create_rectangle(x1,y1,x2,y2, fill=cor)

          self.retangulos.append(retangulos)


          self.jogando = True

     def comecar(self):
          self.jogar()

     def jogar(self):
          if self.jogando:
               self.update()
               self.root.after(10, self.jogar)
          else:
               self.acabou(self.msg)

     def update(self):
          self.canvas.move(self.ovo, self.b_vx , self.b_vy)
          #atualizar o movimento da bola e sua posição
          self.b_x += self.b_vx
          self.b_y += self.b_vy
          #verificar se a bola esta batendo dos lados
          if self.b_x > CANVAS_L - 29 or self.b_x < 0:
               self.b_vx *= -1
          if self.b_y > CANVAS_A - 29 or self.b_y < 0:
               self.b_vy *= -1
          #verificar se existe colisões com os objetos
          self.verificar_colisao()

     def verificar_colisao(self):
               #Criar uma boulding box para capturar a posição da bola
          coord = self.canvas.bbox(self.ovo)
          colisoes = self.canvas.find_overlapping(*coord)
          print(*colisoes)
          print(coord)
          if len(colisoes) != 1 :
        #verificar se o id do objeto colidido é diferente do id do objeto player
             self.b_vy *= -1
             for item in colisoes:
                if item not in self.retangulos:  # colisão com a propria bola ou com o player
                    continue
                self.retangulos.remove(item)

                self.canvas.delete(item)








if __name__ == '__main__':
     Jogo()
  • Hello - you asked the same question last week using another user, have a very complete answer, and no comment, or accept yours. Please take into account that answering these questions takes time and effort, although we gladly do.

  • https://answall.com/questions/308135/problemas-para-desenvolver-um-jogo-no-estilo-bricks-breaking-no-tkinter-colis%C3%B5e/308291#308291

  • If you have created two accounts accidentally and want to merge them, send an msg via the link: https://answall.com/contact

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.