well, I'm a beginner at python and I'm trying to create a simple Breakdown game, a have been looking some topics around here bat non of them answer my question, here what a have now, when a ball touches one block the entire line behind it desapear instead of just the one that was hit.
I'm sorry but some of the words in the code aren't english
import pygame
import sys
pygame.init()
class Player(pygame.sprite.Sprite):
def __init__(self,bolax,bolay):
super().__init__()
self.image = pygame.image.load("bola.bmp")
self.rect = self.image.get_rect()
self.rect.x = bolax
self.rect.y = bolay
self.vel_x,self.vel_y = 5,5
def movendo(self):
if self.rect.y == 300 or self.rect.y == 0:
self.vel_y *= -1
if self.rect.x == 600 or self.rect.x == 0:
self.vel_x *= -1
self.rect.x += self.vel_x
self.rect.y += self.vel_y
class Pontos(pygame.sprite.Sprite):
def __init__(self,tela,x,y):
super().__init__()
self.rect = pygame.Rect(32,12,30,10)
self.rect.centerx = x
self.rect.centery = y
class Update():
def desenhando(tela,peças):
for peça in peças:
pygame.draw.rect(tela,(0,0,0),peça)
def colisão(sprite1,peças):
for peça in peças:
gets_hit = pygame.sprite.spritecollideany(sprite1, peças)
if gets_hit:
bola.vel_y *= -1
#### Geral ####
tela = pygame.display.set_mode((600,300))
peças = pygame.sprite.Group()
for i in range(19):
for j in range(5):
ponto = Pontos(tela,i*31 + 20,j*12 + 6)
peças.add(ponto)
bola = Player(300,150)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
tela.fill((200,200,200))
bola.movendo()
tela.blit(bola.image,(bola.rect.x,bola.rect.y))
Update.desenhando(tela,peças)
Update.colisão(bola,peças)
pygame.display.update()
clock.tick(30)
I just want to remove the sprite that is hit, my problem is here:
def colisão(sprite1,peças): for peça in peças: gets_hit = pygame.sprite.spritecollideany(sprite1, peças) if gets_hit: bola.vel_y *= -1
I don't know how to del just one sprite
What you actually do is to test if the sprite1
hits any item of peças
:
for peça in peças: gets_hit = pygame.sprite.spritecollideany(sprite1, peças)
Just test if peca
hits sprite1
with pygame.sprite.collide_rect()
:
for peça in peças:
gets_hit = pygame.sprite.collide_rect(peça, sprite1)
if gets_hit:
peça.kill()
sprite1.vel_y *= -1
However you can simplify the code with pygame.sprite.spritecollide()
:
def colisão(sprite1, peças):
if pygame.sprite.spritecollide(sprite1, peças, True):
sprite1.vel_y *= -1
Also see How do I detect collision in pygame?