如何处理列表中的所有内容?

2024-05-12 21:04:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()

Tags: theinrecteventforifblockscreen
1条回答
网友
1楼 · 发布于 2024-05-12 21:04:28

如果迭代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

相关问题 更多 >