首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测点击shape pygame

检测点击shape pygame
EN

Stack Overflow用户
提问于 2014-02-12 04:17:26
回答 1查看 1.3K关注 0票数 0

所以我试着通过制作两个形状来检测碰撞,但其中有一些错误,我想知道是否有更简单的方法。这就是我所尝试的:

代码语言:javascript
复制
cursorSurface = pygame.Surface((0,0))
cursor = pygame.draw.rect(cursorSurface, (0,0,0),(mouseX,mouseY,2,2))            
mouse_mask = pygame.mask.from_surface(cursorSurface,255)
mouse_rect = cursor    
if mouseAction[0] == 'move':
    if mouseX > x and mouseX < xEnd and mouseY > y and mouseY < yEnd:          
        topTriangle = selectedSquare.subsurface(x+4,y+4,xEnd-(x+5),int((yEnd-(y+5))*0.25))
        bottomTriangle = selectedSquare.subsurface(x+4,y+4+int((yEnd-(y+5))*0.75),xEnd-(x+5),int((yEnd-(y+5))*0.25))
        leftTriangle = selectedSquare.subsurface(x+4,y+4,int((xEnd-(x+5))*0.25),yEnd-(y+5))
        rightTriangle = selectedSquare.subsurface(x+4+int((xEnd-(x+5))*0.75),y+4,int((xEnd-(x+5))*0.25),yEnd-(y+5))

        collisionTop_mask = pygame.mask.from_surface(topTriangle,255)
        collisionTop_rect = topTriangle.get_rect()                    
        collisionTop_rect.topleft = (0,0)

        pygame.draw.rect(selectedSquare, colorDark,(x+5+int((xEnd-(x+5))*0.25),y+5+int((yEnd-(y+5))*0.25),int((xEnd-(x+5))*0.75)-int((xEnd-(x+5))*0.25)-2,int((yEnd-(y+5))*0.75)-int((yEnd-(y+5))*0.25)-2))     
        pygame.draw.polygon(topTriangle, colorDark, ((1,0), (topTriangle.get_width()-2,0), (int((xEnd-(x+7))/2),(int((yEnd-(y+7))/2)-1))))
        pygame.draw.polygon(leftTriangle, colorDark, ((0,1), (0,leftTriangle.get_height()-2), (int((xEnd-(x+7))/2)-1,(int((yEnd-(y+7))/2)))))
        pygame.draw.polygon(bottomTriangle, colorDark, ((1,yEnd-(y+6)-int((yEnd-(y+5))*0.75)), (bottomTriangle.get_width()-2,yEnd-(y+6)-int((yEnd-(y+5))*0.75)), (int((xEnd-(x+7))/2),(int((yEnd-(y+7))/2)+1-(yEnd-(y+5))*0.75))))
        pygame.draw.polygon(rightTriangle, colorDark, ((xEnd-(x+6)-int((xEnd-(x+5))*0.75),1), (xEnd-(x+6)-int((xEnd-(x+5))*0.75),rightTriangle.get_height()-2), (int((xEnd-(x+7))/2)+1-int((xEnd-(x+5))*0.75),(int((yEnd-(y+7))/2)))))

        screen.blit(selectedSquare, (0,0))
        if collisionTop_mask.overlap(mouse_mask,(mouse_rect.left-collisionTop_rect.left,mouse_rect.top-collisionTop_rect.top)) != None:
            print('detect')

但我也见过这样的方法:

代码语言:javascript
复制
if topTriangle.get_rect().collidepoint(pygame.mouse.get_pos()):

问题是这只检测正方形,而我需要检测一个三角形。有没有人能帮我一下?

EN

回答 1

Stack Overflow用户

发布于 2014-02-12 04:42:19

你可以这样做,我认为(没有测试):

代码语言:javascript
复制
posx, posy = pygame.mouse.get_pos()
posx -= XCoordinateOfTopTriangleOnScreen
posy -= YCoordinateOfTopTriangleOnScreen
if topTriangle.get_at((posx,posy)) == colorDark:
    print('detect')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21712128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档