2017-03-03 30 views
2

私はどのように理解しようとしています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() 
+0

Pythonで 'help()'コマンドを使用するか、[documentation](https://www.pygame.org/docs/ref/surface.html##)を参照してください。 pygame.Surface.get_rect)。また、あなたが見てきた例を提供してください。どちらも、「宇宙船画像のx、yを取得してください」という記述はありませんでした。ドキュメントは次のように述べています:_ "この四角形は常に0、0で始まります" _。 –

答えて

2

まず、images/pygame.Surfacesには位置がないため、blit位置をrectに格納する必要があります。 get_rect pygame.Surfaceのメソッドを呼び出すと、Pygameはイメージのサイズとx、y座標(0、0)で新しい矩形を作成します。インスタンス化中にrectを他の座標に与えるには、get_rectに引数を渡すことができます。ほとんどがcenterまたはtopleftが使用されます。

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)です:後でRECTを移動するには、矩形のこれらの属性のいずれかを変更することができます

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

関連する問題