2017-03-03 48 views
2

我想了解get_rect()如何工作。在這個簡單的例子中,我有兩個圖像,並希望獲得第二個圖像的位置並將第一個圖像移動到第二個圖像。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() 
+0

當您想知道某個函數的某些內容時,請在Python中使用'help()'命令或查找[documentation](https://www.pygame.org/docs/ref/surface.html# pygame.Surface.get_rect)。另外,提供你看過的例子,因爲它們都不應該提到它「獲得飛船圖像的x,y」。該文件說:_「這個矩形將始終從0,0」開始。 –

回答

2

首先,images/pygame.Surfaces沒有位置,所以您必須在rect中存儲blit位置。當你調用pygame.Surface的get_rect方法時,Pygame會創建一個新的矩形,其中包含圖像大小和x,y座標(0,0)。爲了在實例化過程中給出矩形其他座標,您可以將參數傳遞給get_rect,主要使用centertopleft。以後要移動矩形,您可以更改任何RECT的這些屬性:

x,y 
top, left, bottom, right 
topleft, bottomleft, topright, bottomright 
midtop, midleft, midbottom, midright 
center, centerx, centery 
size, width, height 
w,h 

下面是一個例子(按a或d改變矩形的位置,從而使圖像的塊傳輸POS):

import sys 
import pygame as pg 

BG_COLOR = pg.Color(80, 60, 70) 
PLAYER_COLOR = pg.Color(90, 140, 190) 

def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 

    player_img = pg.Surface((40, 60)) 
    player_img.fill(PLAYER_COLOR) 
    # Create a rect with the size of the image/pygame.Surface 
    # and immediately set it's topleft coords to (100, 300). 
    player_rect = player_img.get_rect(topleft=(100, 300)) 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_d: 
        # Set the center to these new coords. 
        player_rect.center = (400, 200) 
       if event.key == pg.K_a: 
        # Set the x coord to 300. 
        player_rect.x = 300 

     screen.fill(BG_COLOR) 
     screen.blit(player_img, player_rect) 
     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
    sys.exit() 
+0

感謝您花時間回答我的問題和詳細回覆。這是非常有用的,並全面回答了我的問題。再次感謝你,你讓我擺脫了這個控制循環。 – henrco