我如何使用 x 和 y 坐标在我的 pygame 游戏中移动一块
how would I go about moving a piece in my pygame game using the x and y coordinates
我需要帮助将棋子移动到特定坐标。
这是我的代码中移动一块的部分:
screen.blit(mountain, (300, 500))
screen.blit(mountain, (300, 400))
def enemy_castle():
castle = pygame.image.load('castle.png')
castle = pygame.transform.scale(castle, (100, 100))
screen.blit(castle, (0, 500))
def player_castle():
castle = pygame.image.load('castle.png')
castle = pygame.transform.scale(castle, (100, 100))
screen.blit(castle, (500, 0))
def enemy_sorcerer():
s_piece = pygame.image.load('sorcerer.png')
s_piece = pygame.transform.scale(s_piece, (100, 100))
screen.blit(s_piece, (400, 0))
screen.blit(s_piece, (500, 100))
所以你有一张图片,需要绘制到特定位置。因此,不要对 (500, 0)
进行“硬编码”,而是开始将其存储在变量中。
PyGame Rect class 将是完美的,因为它也可以与 blit()
函数一起使用,并且已经有一堆 handy collision functions。
您可以创建一个 Rect,只需向图像询问其 Rect - 除了它位于 (0,0)
,因为图像没有位置的概念。
castle_image = pygame.image.load('castle.png').convert_alpha()
castle_image = pygame.transform.scale(castle_image, (100, 100))
castle_rect = castle_image.get_rect()
castle_rect.topleft = ( 500, 0 )
screen.blit( castle_image, castle_rect )
所以现在变量 castle_rect
类似于 [ x=500, y=0, width=100, height=100 ]
要移动图片的绘制位置,现在只需要调整castle_rect
的co-ordinates即可,例如:
castle_rect.x += 10
castle_rect.y += 100
screen.blit( castle_image, castle_rect )
或者,通过再次重置 co-ordinates 来移动整个图像:
castle_rect.topleft = ( 200, 200 )
screen.blit( castle_image, castle_rect )
更有效的方法是在 start-up 上 一次 加载和缩放图像,然后将它们全部绘制在 main-loop 内当前 rectangle-positions.
def loadImageWithScale( image_filename, size=(100,100) ):
""" Load the given image file, then rescale the image to size """
new_image = pygame.image.load( image_filename ).convert_alpha()
new_image = pygame.transform.scale( new_image, size )
return new_image
def getImageRectAt( image, position ):
return image.get_rect( topleft=position )
# Load images
enemy_castle = loadImageWithScale( 'castle.png' )
enemy_sorcerer = loadImageWithScale( 'sorcerer.png' )
player_castle = loadImageWithScale( 'castle.png' )
# Set initial positions
enemy_castle_rect = getImageRectAt( enemy_castle, ( 0, 500 ) )
enemy_sorceror_rect = getImageRectAt( enemy_sorceror, ( 400, 0 ) )
player_castle_rect = getImageRectAt( player_castle, ( 500, 0 ) )
### Main Loop
clock = pygame.time.Clock()
exiting = False
while not exiting:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True
# Update the window, but not more than 60fps
window.fill( ( 128, 128, 128 ) )
window.blit( enemy_castle, enemy_castle_rect )
window.blit( enemy_sorceror, enemy_sorceror_rect )
window.blit( player_castle, player_castle_rect )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop( 60 )
pygame.quit()
可能当你得到的画面元素比较多的时候,你可以把它们整理成方便的列表,让绘图代码更简洁。
我需要帮助将棋子移动到特定坐标。 这是我的代码中移动一块的部分:
screen.blit(mountain, (300, 500))
screen.blit(mountain, (300, 400))
def enemy_castle():
castle = pygame.image.load('castle.png')
castle = pygame.transform.scale(castle, (100, 100))
screen.blit(castle, (0, 500))
def player_castle():
castle = pygame.image.load('castle.png')
castle = pygame.transform.scale(castle, (100, 100))
screen.blit(castle, (500, 0))
def enemy_sorcerer():
s_piece = pygame.image.load('sorcerer.png')
s_piece = pygame.transform.scale(s_piece, (100, 100))
screen.blit(s_piece, (400, 0))
screen.blit(s_piece, (500, 100))
所以你有一张图片,需要绘制到特定位置。因此,不要对 (500, 0)
进行“硬编码”,而是开始将其存储在变量中。
PyGame Rect class 将是完美的,因为它也可以与 blit()
函数一起使用,并且已经有一堆 handy collision functions。
您可以创建一个 Rect,只需向图像询问其 Rect - 除了它位于 (0,0)
,因为图像没有位置的概念。
castle_image = pygame.image.load('castle.png').convert_alpha()
castle_image = pygame.transform.scale(castle_image, (100, 100))
castle_rect = castle_image.get_rect()
castle_rect.topleft = ( 500, 0 )
screen.blit( castle_image, castle_rect )
所以现在变量 castle_rect
类似于 [ x=500, y=0, width=100, height=100 ]
要移动图片的绘制位置,现在只需要调整castle_rect
的co-ordinates即可,例如:
castle_rect.x += 10
castle_rect.y += 100
screen.blit( castle_image, castle_rect )
或者,通过再次重置 co-ordinates 来移动整个图像:
castle_rect.topleft = ( 200, 200 )
screen.blit( castle_image, castle_rect )
更有效的方法是在 start-up 上 一次 加载和缩放图像,然后将它们全部绘制在 main-loop 内当前 rectangle-positions.
def loadImageWithScale( image_filename, size=(100,100) ):
""" Load the given image file, then rescale the image to size """
new_image = pygame.image.load( image_filename ).convert_alpha()
new_image = pygame.transform.scale( new_image, size )
return new_image
def getImageRectAt( image, position ):
return image.get_rect( topleft=position )
# Load images
enemy_castle = loadImageWithScale( 'castle.png' )
enemy_sorcerer = loadImageWithScale( 'sorcerer.png' )
player_castle = loadImageWithScale( 'castle.png' )
# Set initial positions
enemy_castle_rect = getImageRectAt( enemy_castle, ( 0, 500 ) )
enemy_sorceror_rect = getImageRectAt( enemy_sorceror, ( 400, 0 ) )
player_castle_rect = getImageRectAt( player_castle, ( 500, 0 ) )
### Main Loop
clock = pygame.time.Clock()
exiting = False
while not exiting:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True
# Update the window, but not more than 60fps
window.fill( ( 128, 128, 128 ) )
window.blit( enemy_castle, enemy_castle_rect )
window.blit( enemy_sorceror, enemy_sorceror_rect )
window.blit( player_castle, player_castle_rect )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop( 60 )
pygame.quit()
可能当你得到的画面元素比较多的时候,你可以把它们整理成方便的列表,让绘图代码更简洁。