I'm a few days studying and trying to get the ball by hitting the rectangles from above they add and invert the side
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
self.r = []
for i in range(3):
c = random.choice(['green', 'yellow','red'])
#retangulos superiores horizontais
r = self.canvas.create_rectangle(6,10,80,30,fill=c)
r = self.canvas.create_rectangle(82,10,160,30,fill=c)
r = self.canvas.create_rectangle(162,10,240,30,fill=c)
r = self.canvas.create_rectangle(242,10,320,30,fill=c)
r = self.canvas.create_rectangle(322,10,398,30,fill=c)
#retangulos superiores verticais 2ª FILEIRA
r = self.canvas.create_rectangle(6,32,80,52,fill=c)
r = self.canvas.create_rectangle(82,32,160,52,fill=c)
r = self.canvas.create_rectangle(162,32,240,52,fill=c)
r = self.canvas.create_rectangle(242,32,320,52,fill=c)
r = self.canvas.create_rectangle(322,32,398,52,fill=c)
#3ª FILERIRA
r = self.canvas.create_rectangle(6,54,80,74,fill=c)
r = self.canvas.create_rectangle(82,54,160,74,fill=c)
r = self.canvas.create_rectangle(162,54,240,74,fill=c)
r = self.canvas.create_rectangle(242,54,320,74,fill=c)
r = self.canvas.create_rectangle(322,54,398,74,fill=c)
#4ª FILEIRA
r = self.canvas.create_rectangle(6,76,80,96,fill=c)
r = self.canvas.create_rectangle(82,76,160,96,fill=c)
r = self.canvas.create_rectangle(162,76,240,96,fill=c)
r = self.canvas.create_rectangle(242,76,320,96,fill=c)
r = self.canvas.create_rectangle(322,76,398,96,fill=c)
self.r.append(r)
self.jogando = True
def comecar(self):
self.jogar()
def jogar(self):
if self.jogando:
self.update()
self.root.after(5, 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)
#se o numero de colisões é diferente de 0
if len(colisoes) != 1 :
#verificar se o id do objeto colidido é diferente do id do objeto player
if len(colisoes) != self.player:
if __name__ == '__main__':
Jogo()