Pygame key.get_pressed作用于意外对象?

问题描述 投票:1回答:1

[我正在写一个简单的类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()




python-3.x graphics pygame keypress pygame-surface
1个回答
0
投票

每次curr_block = Block()创建新块时,都会在列表self.shape的末尾追加新的4个块。如果只想移动最近的块,则必须移动self.shape的最后4个元素。可以通过self.shape[-4:]访问最后4个元素。例如:

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)
© www.soinside.com 2019 - 2024. All rights reserved.