如何与列表中的所有内容发生碰撞?



我正在尝试让碰撞对列表中创建的所有矩形起作用,因此当球碰到在块 [] 列表中创建的矩形时,它会反弹,并附加(删除(块,就像在游戏突破中一样。我试过:

for block in blocks:
if blockRect.colliderect(ball):
speed[1] = -speed[1]
blocks.append(blockRect)

if blockRect.colliderect(ball):
speed[1] = -speed[1]
blocks.append(blockRect)

已编辑:这是尝试和测试它的完整代码

#December 16, 2019
#Final Project - Breakout
#IMPORTING LIBRARIES-----
import pygame
import sys
import time
#INITIALIZING SCREEN SIZE-----
pygame.init()
screen_size = (590, 750)
screen = pygame.display.set_mode((screen_size),0)
pygame.display.set_caption("BREAKOUT")
#retrieve screen measurements
screen_w = screen.get_width()
screen_h = screen.get_height()
#retrieve position of center of screen
center_x = int(screen_w/2)
center_y = int(screen_h/2)
#COLOURS-----
WHITE = (255,255,255)
BLACK = (0, 0, 0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
PURPLE = (154, 136, 180)
#BACKGROUND-----
screen.fill(BLACK)
pygame.display.update()
#SPEED-----
clock = pygame.time.Clock()
FPS = 60 #set frames per second
speed = [4,4]
paddle_speed = 6
#VARIABLES-----
#paddle
paddle_w = 100
paddle_h = 10
paddle_x = center_x
paddle_y = 670
paddle_dx = 0
paddle_dy = 0
#ball
ball_w = 10
ball_h = 10
ball_x = center_x
ball_y = center_y
#blocks
block_x = 2
block_y = 200
block_h = 10
block_w = 40
#RECTS-----
paddle = pygame.Rect(paddle_x, paddle_y, paddle_w, paddle_h)
ball = pygame.Rect(ball_x, ball_y, ball_w, ball_h)
#empty array to store rects for each block row
blocks = []
#layout of blocks
block_array = [
"B B B B B B B B B B B B B B",
]
#read the array and create the appropriate Rects, store them in the walls array
for row in block_array:
for col in row:
if col == "B":
blockRect = pygame.Rect(block_x, block_y, block_w, block_h)
blocks.append(blockRect)
block_x += 21
block_y += 20
block_x = 2
#LOOPS-----
meryem = False

#loop for game
meryem = True
while meryem:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
game = False
pygame.quit()
sys.exit()
#moving paddle with keys
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle_dx = -paddle_speed
elif event.key == pygame.K_RIGHT:
paddle_dx = paddle_speed
if event.type == pygame.KEYUP:
paddle_dx = 0
#constrain this loop to the specified FPS
clock.tick(FPS)
#PADDLE EVENTS-----
#store old paddle positions
old_paddle_x = paddle.x
old_paddle_y = paddle.y
#moving the paddle rect
paddle.move_ip(paddle_dx, paddle_dy)
#check to see if rect has left screen
if paddle.left < 0 or paddle.right > screen_w:
paddle.x = old_paddle_x
#BALL EVENTS-----
#moving ball
ball = ball.move(speed)
#collision left & right
if ball.left < 0 or ball.right > screen_w:
speed[0] = -speed[0]
#collision top
if ball.top < 0 or ball.bottom > screen_h:
speed[1] = -speed[1]
#collision of ball with paddle
if paddle.colliderect(ball):
speed[1] = -speed[1]
#BLOCKS EVENTS-----
if blockRect.colliderect(ball):
speed[1] = -speed[1]
#DRAWING/CREATING OBJECTS-----
#removes screen trail
screen.fill(BLACK)
#drawing paddle/ball inside rect
pygame.draw.rect(screen,PURPLE,paddle,0)
pygame.draw.rect(screen,WHITE,ball,0)
#draws a block for each "B"
for block in blocks:
pygame.draw.rect(screen,RED,block,0)
#updating the screen
pygame.display.update()
pygame.quit()
sys.exit()

如果你迭代for block in blocks:那么你应该使用block而不是blockRect来检查冲突

#BLOCKS EVENTS-----
for block in blocks:    
if block.colliderect(ball):
speed[1] = -speed[1]

顺便说一句:如果你对每个对象都使用 pygame.sprite.Sprite((,那么你可以在 pygame.sprite.Group(( 中保留块并使用 pygame.sprite.spritecollide(( 检查碰撞

pygame.sprite.spritecollide(sprite_ball, group_blocks)

编辑:删除触摸块

#BLOCKS EVENTS-----
keeped_blocks = []    
for block in blocks:    
if block.colliderect(ball):
speed[1] = -speed[1]
else:
keeped_blocks.append(block)
blocks = keeped_blocks

最新更新