2016-01-15 51 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