私はどのように理解しようとしていますget_rect()
。この単純な例では、2つのイメージがあり、2番目のイメージの位置を取得し、最初のイメージを2番目のイメージに移動する必要があります。Pygame:正しくget_rect()を使用する方法
私は様々な例をオンラインで見てきましたが、これを動作させることはできません。私は間違って何をしていますか?
import pygame, sys
from pygame.locals import *
import time
pygame.init()
FPS = 10 # frames per second setting
fpsClock = pygame.time.Clock()
# Set up the window
DISPLAYSURF = pygame.display.set_mode((600, 400), 0, 32)
pygame.display.set_caption('Test program for get_rect()')
WHITE = (255, 255, 255)
# Load two images
baseImg = pygame.image.load('image1.jpg')
spaceshipImg = pygame.image.load('image2.jpg')
DISPLAYSURF.fill(WHITE)
# Place one image at the bottom of the screen
DISPLAYSURF.blit(baseImg, (300, 300))
pygame.display.update()
# Place the second image at the top of the screen
DISPLAYSURF.blit(spaceshipImg, (300, 0))
pygame.display.update()
# Wait for one second
time.sleep(1)
# Obtain the rectangle for each image
baseRect = baseImg.get_rect()
spaceshipRect = spaceshipImg.get_rect()
# This is where I believe I'm going wrong
# I understand this to obtain the x,y of the spaceship image
# Set the xy coordinates for the top image to the xy of the bottom image
spaceshipRect.x = baseRect.x
spaceshipRect.y = baseRect.y
# Move the top image to new xy position
# However this doesn't work
DISPLAYSURF.blit(spaceshipImg, (spaceshipRect.x, spaceshipRect.y))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Pythonで 'help()'コマンドを使用するか、[documentation](https://www.pygame.org/docs/ref/surface.html##)を参照してください。 pygame.Surface.get_rect)。また、あなたが見てきた例を提供してください。どちらも、「宇宙船画像のx、yを取得してください」という記述はありませんでした。ドキュメントは次のように述べています:_ "この四角形は常に0、0で始まります" _。 –