Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pygame.KEYDOWN拾取块_Python_Pygame_Sprite_Block_Keydown - Fatal编程技术网

Python 使用pygame.KEYDOWN拾取块

Python 使用pygame.KEYDOWN拾取块,python,pygame,sprite,block,keydown,Python,Pygame,Sprite,Block,Keydown,我正在尝试使用pygame.KEYDOWN,这样当我按下pygame.kq代表的键时,玩家与之碰撞的块可以被拖动到玩家移动的位置,就像使用pygame.MOUSEBUTTONDOWN一样 但按q键拖动块不起作用 当我试图在另一个更大的代码中实现这个功能时,我就产生了这种需求。所以我决定让另一个来设置我的MWE。我想看看是否有人问过这个问题,但我只是找到了一些相对的问题,但不是确切的问题,这些问题使用了非常不同或非常大的代码结构,我仍然没有弄清楚为什么我的MWE代码无法正常工作。这应该是一个简单的

我正在尝试使用pygame.KEYDOWN,这样当我按下pygame.kq代表的键时,玩家与之碰撞的块可以被拖动到玩家移动的位置,就像使用pygame.MOUSEBUTTONDOWN一样

但按q键拖动块不起作用

当我试图在另一个更大的代码中实现这个功能时,我就产生了这种需求。所以我决定让另一个来设置我的MWE。我想看看是否有人问过这个问题,但我只是找到了一些相对的问题,但不是确切的问题,这些问题使用了非常不同或非常大的代码结构,我仍然没有弄清楚为什么我的MWE代码无法正常工作。这应该是一个简单的问题,可能有人已经发布了这个问题,所以如果是这样,请让我知道在哪里已经发布了一个问题来澄清我的问题

我的MWE:

import pygame
import random

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

class Block(pygame.sprite.Sprite):
    """
    This class represents the block to be picked up.
    It derives from the "Sprite" class in Pygame.
    """

    def __init__(self, color, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """

        # Call the parent class (Sprite) constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):
    """ The class is the player-controlled sprite. """

    carry_block_list = []

    def __init__(self, x, y):
        """Constructor function"""
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(RED)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):

        diff_x = self.rect.x - 4
        diff_y = self.rect.y - 4

        # Loop through each block that we are carrying and adjust
        # it by the amount we moved.
        for block in self.carry_block_list:
            block.rect.x -= diff_x
            block.rect.y -= diff_y
            print("something")

# Call this function so the Pygame library can initialize itself
pygame.init()

# Create an 800x600 sized screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

# Set the title of the window
pygame.display.set_caption('Move Sprite With Keyboard')

# Create the player paddle object
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
block_list = pygame.sprite.Group()


for i in range(50):
    # This represents a block
    block = Block(BLACK, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)

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

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
               print("pick up")
            # When the mouse button is pressed, see if we are in contact with
            # other sprites:

               blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)

            # Set the list of blocks we are in contact with as the list of
            # blocks being carried.

               player.carry_block_list = blocks_hit_list

            if event.key == pygame.K_l:
               print("let go")
            # When we let up on the mouse, set the list of blocks we are
            # carrying as empty.

               player.carry_block_list = []

            if event.key == pygame.K_LEFT:
                player.rect.x -= player.rect.width
            elif event.key == pygame.K_RIGHT:
                player.rect.x += player.rect.width
            elif event.key == pygame.K_UP:
                player.rect.y -= player.rect.height
            elif event.key == pygame.K_DOWN:
                player.rect.y += player.rect.height

    # -- Draw everything
    # Clear screen
    screen.fill(WHITE)

    # Draw sprites
    all_sprites_list.draw(screen)

    # Flip screen
    pygame.display.flip()

    # Pause
    clock.tick(40)

pygame.quit()
在Player.update中移动块,但从未执行

首先,我尝试在每个循环中执行此函数,但最后我将此函数更改为仅在移动玩家时运行

def update(self, diff_x, diff_y):
    # Loop through each block that we are carrying and adjust
    # it by the amount we moved.
    for block in self.carry_block_list:
        block.rect.x += diff_x
        block.rect.y += diff_y

完整代码:

import pygame
import random

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

class Block(pygame.sprite.Sprite):
    """
    This class represents the block to be picked up.
    It derives from the "Sprite" class in Pygame.
    """

    def __init__(self, color, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """

        # Call the parent class (Sprite) constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):
    """ The class is the player-controlled sprite. """

    carry_block_list = []

    def __init__(self, x, y):
        """Constructor function"""
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(RED)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self, diff_x, diff_y):
        # Loop through each block that we are carrying and adjust
        # it by the amount we moved.
        for block in self.carry_block_list:
            block.rect.x += diff_x
            block.rect.y += diff_y


# Call this function so the Pygame library can initialize itself
pygame.init()

# Create an 800x600 sized screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

# Set the title of the window
pygame.display.set_caption('Move Sprite With Keyboard')

# Create the player paddle object
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
block_list = pygame.sprite.Group()


for i in range(50):
    # This represents a block
    block = Block(BLACK, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)

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

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:

            # When the mouse button is pressed, see if we are in contact with
            # other sprites:

               blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)

            # Set the list of blocks we are in contact with as the list of
            # blocks being carried.

               player.carry_block_list = blocks_hit_list

            if event.key == pygame.K_l:

            # When we let up on the mouse, set the list of blocks we are
            # carrying as empty.

               player.carry_block_list = []

            if event.key == pygame.K_LEFT:
                player.rect.x -= player.rect.width
                player.update(-player.rect.width, 0)

            elif event.key == pygame.K_RIGHT:
                player.rect.x += player.rect.width
                player.update(player.rect.width, 0)

            elif event.key == pygame.K_UP:
                player.rect.y -= player.rect.height
                player.update(0, -player.rect.height)

            elif event.key == pygame.K_DOWN:
                player.rect.y += player.rect.height
                player.update(0, player.rect.height)



    # -- Draw everything
    # Clear screen
    screen.fill(WHITE)

    # Draw sprites
    all_sprites_list.draw(screen)

    # Flip screen
    pygame.display.flip()

    # Pause
    clock.tick(40)

pygame.quit()

在代码中,您使用p而不是q来拖动它。@furas感谢您的提示,我将更正问题的陈述……首先,我将使用print来查看变量中的值以及执行的代码。通过这种方式,您可以看到它是否执行应该向列表中添加块的代码,移动它并绘制它。您在Player.update中移动块,所以我在这个函数中使用了print,它似乎从未执行过。我认为您不需要新的类或函数。最后,您可以在player.update中添加Block.update或Block.move并运行Block.updatediff\u x、diff\u y或Block.movediff\u x、diff\u y-尤其是如果Block.update/Block.move可以做更多的事情。
import pygame
import random

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

class Block(pygame.sprite.Sprite):
    """
    This class represents the block to be picked up.
    It derives from the "Sprite" class in Pygame.
    """

    def __init__(self, color, width, height):
        """ Constructor. Pass in the color of the block,
        and its x and y position. """

        # Call the parent class (Sprite) constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values
        # of rect.x and rect.y
        self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):
    """ The class is the player-controlled sprite. """

    carry_block_list = []

    def __init__(self, x, y):
        """Constructor function"""
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(RED)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self, diff_x, diff_y):
        # Loop through each block that we are carrying and adjust
        # it by the amount we moved.
        for block in self.carry_block_list:
            block.rect.x += diff_x
            block.rect.y += diff_y


# Call this function so the Pygame library can initialize itself
pygame.init()

# Create an 800x600 sized screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

# Set the title of the window
pygame.display.set_caption('Move Sprite With Keyboard')

# Create the player paddle object
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
block_list = pygame.sprite.Group()


for i in range(50):
    # This represents a block
    block = Block(BLACK, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)

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

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:

            # When the mouse button is pressed, see if we are in contact with
            # other sprites:

               blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)

            # Set the list of blocks we are in contact with as the list of
            # blocks being carried.

               player.carry_block_list = blocks_hit_list

            if event.key == pygame.K_l:

            # When we let up on the mouse, set the list of blocks we are
            # carrying as empty.

               player.carry_block_list = []

            if event.key == pygame.K_LEFT:
                player.rect.x -= player.rect.width
                player.update(-player.rect.width, 0)

            elif event.key == pygame.K_RIGHT:
                player.rect.x += player.rect.width
                player.update(player.rect.width, 0)

            elif event.key == pygame.K_UP:
                player.rect.y -= player.rect.height
                player.update(0, -player.rect.height)

            elif event.key == pygame.K_DOWN:
                player.rect.y += player.rect.height
                player.update(0, player.rect.height)



    # -- Draw everything
    # Clear screen
    screen.fill(WHITE)

    # Draw sprites
    all_sprites_list.draw(screen)

    # Flip screen
    pygame.display.flip()

    # Pause
    clock.tick(40)

pygame.quit()