2016-01-15 53 views
1

我想在pygame中做一個拖放機制,我正在部分成功(感謝this one和像this other one這樣的教程的答案)。我使用的機制如下:一旦檢測到按鈕事件(並且只有當鼠標在圖像上時),纔在每個循環中更新圖像的位置。爲此,我通過調用image.get_rect()創建了一個矩形對象,但看起來這個矩形是移動的,圖像的中心位於矩形的右下角。我既附件的代碼的結果:get_rect()在圖像中被移動pygame

import pygame, sys 
from pygame.locals import * 

FPS = 60 
fpsClock = pygame.time.Clock() 


def main(): 
    pygame.init() 

    DS = pygame.display.set_mode((400, 400), 0, 32) 
    pygame.display.set_caption('Drag-n-drop that cat') 

    WHITE = (255, 255, 255) 
    BLACK = (0, 0, 0) 
    catImg = pygame.image.load('cat.png') # I load the image 
    catImgRectObj = catImg.get_rect() # I create the rect object 
    catx = 200 
    caty = 200 
    catImgRectObj.center = [catx, caty] 
    IsMousePressed = False 

    while True: 
     lastPos = catImgRectObj.center 
     DS.fill(WHITE) 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      elif event.type == MOUSEBUTTONDOWN: 
       IsMousePressed = True 
      elif event.type == MOUSEBUTTONUP: 
       IsMousePressed = False 

     if IsMousePressed and isMouseOverObj(catImgRectObj): 
      catImgRectObj.center = pygame.mouse.get_pos() #I update the center 
     else: 
      catImgRectObj.center = lastPos 

     pygame.draw.rect(DS, BLACK, catImgRectObj) #draw the rect object 
     DS.blit(catImg, catImgRectObj.center) #draw the cat. 
     pygame.display.update() 
     fpsClock.tick(FPS) 


def isMouseOverObj(Obj): 
    return Obj.collidepoint(pygame.mouse.get_pos()) 

if __name__ == '__main__': 
    main() 

The result

+0

貓的圖像可以在這裏找到(https://inventwithpython.com/cat.png),它是從[tutorial webpage]下載的(http://inventwithpython.com/pygame/chapter2.html )。 – Miguelgondu

回答

1

使用

DS.blit(catImg, catImgRectObj) 

代替

DS.blit(catImg, catImgRectObj.center) 

畫貓。

enter image description here

catImgRectObj矩形已經描述了貓的形象是,如果你使用catImgRectObj.center做塊在屏幕上,但它的左上角移動到所需區域的中心。


而且,我會用這樣的:

import pygame, sys 
from pygame.locals import * 

FPS = 60 
fpsClock = pygame.time.Clock() 

def main(): 
    pygame.init() 

    DS = pygame.display.set_mode((400, 400), 0, 32) 
    pygame.display.set_caption('Drag-n-drop that cat') 

    catImg = pygame.image.load('cat.png').convert_alpha() 
    catMask = pygame.mask.from_surface(catImg) 
    catImgRectObj = catImg.get_rect(center=(200, 200)) 
    IsMousePressed = False 

    while True: 

     DS.fill(pygame.color.THECOLORS['white']) 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      elif event.type == MOUSEBUTTONDOWN and isMouseOverObj(catMask, catImgRectObj): 
       IsMousePressed = True 
      elif event.type == MOUSEBUTTONUP: 
       IsMousePressed = False 
      elif event.type == MOUSEMOTION and IsMousePressed: 
       catImgRectObj.move_ip(event.rel) 

     DS.blit(catImg, catImgRectObj) 
     pygame.display.update() 
     fpsClock.tick(FPS) 


def isMouseOverObj(mask, rect): 
    mouse_pos = pygame.mouse.get_pos() 
    rel_pos = (mouse_pos[0] - rect.left, mouse_pos[1] - rect.top) 
    return rect.collidepoint(mouse_pos) and mask.get_at(rel_pos) 

if __name__ == '__main__': 
    main() 

使碰撞檢測像素完美,簡化代碼了一下,並防止跳躍一旦你點擊貓。

+0

你不會相信這是多麼有幫助,非常感謝。順便說一句,面具對象背後的哲學是什麼,究竟是什麼? – Miguelgondu

+0

它基本上是一個2D數組,告訴你表面的特定像素/點是否實際填充或空(透明)空間。想象一下,它是某種表面的複製品,但只有2種顏色:黑色和白色。 – sloth

+0

因此,如果我在「黑色」像素上,mask.get_at(position)返回True。 – Miguelgondu