Pygame:如何正确使用 get_rect()
Pygame:如何正确使用 get_rect()
我正在尝试理解get_rect()
是如何工作的。在这个简单的例子中,我有两个图像,并想要获取第二个图像的位置,并将第一个图像移动到第二个图像的位置。
我已经查看了各种在线示例,但无法使之工作。我做错了什么?
import pygame, sys from pygame.locals import * import time pygame.init() FPS = 10 # 设置每秒帧数 fpsClock = pygame.time.Clock() # 设置窗口 DISPLAYSURF = pygame.display.set_mode((600, 400), 0, 32) pygame.display.set_caption('get_rect()测试程序') WHITE = (255, 255, 255) # 加载两个图像 baseImg = pygame.image.load('image1.jpg') spaceshipImg = pygame.image.load('image2.jpg') DISPLAYSURF.fill(WHITE) # 将一个图像放置在屏幕底部 DISPLAYSURF.blit(baseImg, (300, 300)) pygame.display.update() # 将第二个图像放置在屏幕顶部 DISPLAYSURF.blit(spaceshipImg, (300, 0)) pygame.display.update() # 等待一秒钟 time.sleep(1) # 获取每个图像的矩形 baseRect = baseImg.get_rect() spaceshipRect = spaceshipImg.get_rect() # 这是我认为我做错的地方 # 我理解这是获取飞船图像的x、y坐标 # 将顶部图像的xy坐标设置为底部图像的xy坐标 spaceshipRect.x = baseRect.x spaceshipRect.y = baseRect.y # 将顶部图像移动到新的xy位置 # 但是这并不起作用 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()