2011-01-13 51 views
1

我正嘗試使用碰撞檢測來檢測我的鼠標何時擊中了我導入的圖像。我得到錯誤「元組沒有屬性矩形」Pygame中使用精靈的碰撞檢測

def main(): 


    #Call the SDL arg to center the window when it's inited, and then init pygame 
    os.environ["SDL_VIDEO_CENTERED"] = "1" 
    pygame.init() 

    #Set up the pygame window 
    screen = pygame.display.set_mode((600,600)) 


    image_one = pygame.image.load("onefinger.jpg").convert() 

    screen.fill((255, 255, 255)) 
    screen.blit(image_one, (225,400)) 
    pygame.display.flip() 

    while 1: 
     mousecoords = pygame.mouse.get_pos() 
     left = (mousecoords[0], mousecoords[1], 10, 10) 
     right = image_one.get_bounding_rect() 
     if pygame.sprite.collide_rect((left[0]+255, left[1]+400, left[2], left[3]), right): 
      print('Hi') 

回答

1

問題是,pygame.sprite.collide_rect()需要兩個Sprite對象。你傳遞給它兩個元組 - 遊標和圖像都不是Sprites,所以缺少rect屬性。

您可以使用image_one創建一個Sprite,但將光標轉換爲Sprite會更加棘手。我認爲手動測試光標是否在圖像中會更容易。

#Set up the pygame window 
screen = pygame.display.set_mode((200,200)) 
screen.fill((255, 255, 255)) 

#Set up image properties (would be better to make an object) 
image_one = pygame.image.load("image_one.png").convert() 
image_x = 225 
image_y = 400 
image_width = image_one.get_width() 
image_height = image_one.get_height() 

# Mouse properties 
mouse_width = 10 
mouse_height = 10 

screen.blit(image_one, (image_x, image_y)) 
pygame.display.flip() 

while 1: 
    for event in pygame.event.get(): 
     if event.type == pygame.MOUSEMOTION: 
      mouse_x, mouse_y = pygame.mouse.get_pos() 

      # Test for 'collision' 
      if image_x - mouse_width < mouse_x < image_x + image_width and image_y - mouse_height < mouse_y < image_y + image_height: 
       print 'Hi!' 

注意,我測試鼠標是否已經測試它是否是圖像中的前移動,以避免不必要的重複計算。

+0

非常好的幫助!我發現了一個更簡單的方法來解決它。當你將圖像「blit」到屏幕上時,它會返回一個Rect。您只需指定一個等於該矩形的變量,即:FirstL_Rect = screen.blit(image_one,(185,400)),然後使用「碰撞」功能執行碰撞檢測。你可以通過簡單地做這個來改變你的mous einto: mousecoords = pygame.mouse.get_pos() coordinateRect = Rect(mousecoords [0],mousecoords [1],10,10) – Parseltongue 2011-01-16 00:39:30

0

這與pygame或python無關,但可能有所幫助。

Lazy Foo爲SDL提供了許多優秀的教程,但它們都是用C++編寫的。儘管他們評論非常好。他們的鏈接在這裏:http://www.lazyfoo.net/SDL_tutorials/index.php

0

彼得建議,你也想通了,它的更容易碰撞檢測處理時Rects工作位置代替。

我會更進一步:總是與精靈

使用精靈,您可以訪問pygame.sprite中所有那些方便的碰撞檢測功能。如果您決定移動該圖像,則更新位置和動畫更容易。它還包含圖像表面,全部在一個對象中。更不用說雪碧了羣組

精靈也有.rect屬性,所以你總是可以做低水平的rect操作,如果你想與mysprite.rect

這就是說,這裏是你如何可以得到一個雪碧出你的形象:

image_one = pygame.sprite.Sprite() 
image_one.image = pygame.image.load("image_one.png").convert() 
image_one.rect = pygame.Rect((image_x, image_y), image_one.image.get_size()) 

image_two_three等創造更多的精靈或者創建一個函數(或者,甚至更好,子類Sprite),接收位置和文件名作爲參數,所以你可以在一個單一作爲創建精靈:

image_two = MySprite(filename, x, y) 

現在,您可以選擇把它們分組:

my_images = pygame.sprite.Group(image_one, image_two, image_three) 

繪圖一樣簡單:

my_images.draw(screen) 

,將在一次的blit 所有圖像,每一個在其自己的位置!很酷,嗯?

讓我們創建一個「假」雪碧鼠標光標:

mouse = pygame.sprite.Sprite() 
mouse.rect = pygame.Rect(pygame.mouse.get_pos(), (1, 1)) 

我做了它一個1x1的精靈,因此只在鼠標熱點碰撞。注意它沒有.image屬性(因此「假」精靈),但pygame並不在意,因爲我們不打算繪製它。

而現在最好的部分:

imagehit = pygame.sprite.spritecollideany(mouse, my_images) 
print imagehit 

在你測試對碰撞的所有圖像單行線,而不是隻有你知道如果鼠標相撞任何,也哪一個它做到了!

確實值得使用Sprites;)