Ich versuche zu verstehen, wie get_rect()
funktioniert. In diesem einfachen Beispiel habe ich zwei Bilder und möchte die Position des zweiten Bildes erhalten und das erste Bild auf das zweite Bild verschieben.Pygame: Wie man richtig verwendet get_rect()
Ich habe eine Vielzahl von Beispielen online angeschaut und kann das nicht zum Funktionieren bringen. Was mache ich falsch?
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()
Wenn Sie sich etwas über eine Funktion wundern, verwenden Sie entweder den 'help()' Befehl in Python oder suchen Sie in der [Dokumentation] (https://www.pygame.org/docs/ref/surface.html#) pygame.Surface.get_rect). Geben Sie auch die Beispiele an, die Sie angeschaut haben, da keiner hätte erwähnen sollen, dass er "x, y des Raumschiffbildes" erhält. Die Dokumentation sagt: _ "Dieses Rechteck beginnt immer bei 0, 0" _. –