Pygame key.get_pressed 作用于非预期对象?

Pygame key.get_pressed acted on unintended object?

我正在写一个简单的 class Block,其中包含来自 pygame 的四个 Rect。它有一个 key_press 功能,可以在按下相应的键时移动四个 Rect。它还有一个 draw 函数,可以在屏幕上绘制四个 Rect

问题出在下面的 while 循环中。在 while 循环中,我想用键移动块。每当当前块到达屏幕底部时,它就会停在那里并创建一个新块。按键功能现在应该只作用于新块。

但是,我在下面的代码中遇到的问题是,在创建新块之后,按键会同时作用于两个块(即按右键会将两个块都向右移动)。

所以我想我的问题可以归结为一个问题:

为什么按键功能不是只作用于当前块,而是作用于两者?

非常感谢您!

import pygame
from pygame.rect import Rect

pygame.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Set the height and width of the screen
block_size = 20
row, col = 20, 20
color = RED
size = [block_size * col, block_size * row]
screen = pygame.display.set_mode(size)


class Block(Rect):
    shape = []  # hold blocks which are part of the shape

    def __init__(self):
        self.block1 = Rect(0, block_size, block_size, block_size)
        self.block2 = Rect(0, block_size * 2, block_size, block_size)
        self.block3 = Rect(0, block_size * 3, block_size, block_size)
        self.block4 = Rect(0, block_size * 4, block_size, block_size)
        self.shape.append(self.block1)
        self.shape.append(self.block2)
        self.shape.append(self.block3)
        self.shape.append(self.block4)

    def key_press(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            for item in self.shape:
                item.move_ip(-block_size, 0)

        if key[pygame.K_RIGHT]:
            for item in self.shape:
                item.move_ip(block_size, 0)

        if key[pygame.K_UP]:
            for item in self.shape:
                item.move_ip(0, -block_size)

        if key[pygame.K_DOWN]:
            for item in self.shape:
                item.move_ip(0, block_size)

    def draw(self, surface):
        for item in self.shape:
            pygame.draw.rect(surface, color, item)

    def get_bottom(self):
        return self.block4.bottom

done = False
clock = pygame.time.Clock()

curr_block = Block()

while not done:

    clock.tick(10)

    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

    screen.fill(WHITE)

    curr_block.key_press()

    if curr_block.get_bottom() >= block_size * row:
        curr_block = Block()

    curr_block.draw(screen)  # draw the falling block

    pygame.display.flip()

pygame.quit()




每当 curr_block = Block() 创建一个新块时,都会在列表的末尾追加 4 个新元素 self.shape。如果你只想移动最反感的块,那么你必须移动 self.shape 的最后 4 个元素。最后 4 个元素可以通过 self.shape[-4:] 访问(参见 Subscriptions)。例如:

class Block(Rect):
    # [...]

    def key_press(self):
        key = pygame.key.get_pressed()

        for item in self.shape[-4:]:

            if key[pygame.K_LEFT]:
                item.move_ip(-block_size, 0)

            if key[pygame.K_RIGHT]:
                item.move_ip(block_size, 0)

            if key[pygame.K_UP]:
                item.move_ip(0, -block_size)

            if key[pygame.K_DOWN]:
                item.move_ip(0, block_size)