使用 pygame.KEYDOWN 拾取方块
Pick up block using pygame.KEYDOWN
我正在尝试使用 pygame.KEYDOWN
这样当我按下 pygame.K_q
代表的键时 block player 发生了碰撞,可以 拖动 (拾取)到玩家移动的地方,因为它是在这个 tutorial.[=18 中使用 pygame.MOUSEBUTTONDOWN
完成的=]
但是按q
键拖动方块不起作用...
当我试图在另一个更大的代码中实现这个功能时,我的这个需求就出现了。所以我决定用另一个 tutorial2 来设置我的 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
和
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)
完整代码:
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()
我正在尝试使用 pygame.KEYDOWN
这样当我按下 pygame.K_q
代表的键时 block player 发生了碰撞,可以 拖动 (拾取)到玩家移动的地方,因为它是在这个 tutorial.[=18 中使用 pygame.MOUSEBUTTONDOWN
完成的=]
但是按q
键拖动方块不起作用...
当我试图在另一个更大的代码中实现这个功能时,我的这个需求就出现了。所以我决定用另一个 tutorial2 来设置我的 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
和
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)
完整代码:
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()