检测形状pygame上的点击 [英] detect click on shape pygame

查看:174
本文介绍了检测形状pygame上的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  cursorSurface = pygame.Surface((0,0))
cursor = pygame (cursorSurface,255)
mouse_rect = cursor
if mouseAction [0] =='move':
if mouseX> x和鼠标X< xEnd和mouseY> y和mouseY< yEnd:
topTriangle = selectedSquare.subsurface(x + 4,y + 4,xEnd-(x + 5),int((yEnd-(y + 5))* 0.25))
bottomTriangle = selectedSquare (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 (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。 (选择平方,colorDark,(x + 5 + int((xEnd-(x + 5))* 0.25),y + 5 + int((yEnd-(y + 5))* 0.25) xEnd-(X + 5))* 0.75)-int((xEnd-(X + 5))* 0.25)-2,INT((yEnd-(Y + 5))* 0.75)-int((yEnd-( (顶部三角,colorDark,((1,0),(topTriangle.get_width() - 2,0),(int((xEnd))* 0.25)-2))
pygame.draw.polygon - (X + 7))/ 2),(INT((Y (),(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 - (y + 5))* 0.75)),(int((xEnd-(x + 7))/ 2),(int((yEnd-(y + 7))/ 2)+ 1-(yEnd- (xEnd-(x + 5))* 0.75))* 0.75))))
pygame.draw.polygon(rightTriangle,colorDark,((xEnd- 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' )

但我也看到了这种方法:

  if topTriangle.get_rect()。collidepoin t(pygame.mouse.get_pos()):

问题是这只能检测正方形和I需要检测三角形。有人可以请求帮助我吗?

解决方案

您可以按照我的想法做这样的事情(未测试) p>

  posx,posy = pygame.mouse.get_pos()
posx - = XCoordinateOfTopTriangleOnScreen
posy - = YCoordinateOfTopTriangleOnScreen
if topTriangle.get_at((posx,posy))== colorDark:
print('detect')


so I have tried detecting the collision by making 2 shapes but there is some kind of mistake in it and I am wondering if there are easier ways. so this is what i tried:

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')

but i have also seen this methode:

if topTriangle.get_rect().collidepoint(pygame.mouse.get_pos()):

the problem is that this only detects squares and I need to detect a triangle. could someone pleas help me with this?

解决方案

You could do something like this I think (didn't test):

posx, posy = pygame.mouse.get_pos()
posx -= XCoordinateOfTopTriangleOnScreen
posy -= YCoordinateOfTopTriangleOnScreen
if topTriangle.get_at((posx,posy)) == colorDark:
    print('detect')

这篇关于检测形状pygame上的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆