本文介绍了当鼠标“点击" .rect时,pygame动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个测试函数,以了解对pygame.rect的鼠标点击"动作如何产生响应.

I have been writing a test function to learn how a mouse 'click' action on a pygame.rect will result in a reponse.

到目前为止:

def test():
    pygame.init()
    screen = pygame.display.set_mode((770,430))
    pygame.mouse.set_visible(1)
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250,250,250))
    screen.blit(background, (0,0))
    pygame.display.flip()

    ## set-up screen in these lines above ##

    button = pygame.image.load('Pictures/cards/stand.png').convert_alpha()
    screen.blit(button,(300,200))
    pygame.display.flip()

    ## does button need to be 'pygame.sprite.Sprite for this? ##
    ## I use 'get_rect() ##
    button = button.get_rect()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.mouse.get_pressed():
                ## if mouse is pressed get position of cursor ##
                pos = pygame.mouse.get_pos()
                ## check if cursor is on button ##
                if button.collidepoint(pos):
                    ## exit ##
                    return

我在Google上遇到了人们正在使用的页面,或建议对图像使用pygame.sprite.Sprite类,因此我认为这是我的问题所在.我检查了pygames文档,方法之间并没有太大的联系,恕我直言.我显然缺少一些简单的东西,但是我想get_rect将使pygames中的图像能够在按下时检查鼠标位置是否在其上方?

I have come across pages on google where people are using or are recommended to use a pygame.sprite.Sprite class for the images and I'm thinking that this is where my problem is from. I have checked the pygames docs and there isn't much cohesion between methods, imho. I am obviously missing something simple but, I thought get_rect would make an image in pygames be able to check if the mouse position is over it when pressed?

我在想我需要调用pygame.sprite.Sprite方法来使图像/矩形互动吗?

I'm thinking I need to call the pygame.sprite.Sprite method to make the images/rects interactive?

推荐答案

好吧,如果有人有兴趣或遇到类似的问题,这就是我需要改变的地方.

Well, if anyone is interested or is having a similar issue, this is what I needed to change.

首先,删除:

button = button.get_rect()

然后:

screen.blit(button, (300, 200))

应该是:

b = screen.blit(button, (300, 200))

这将创建按钮在屏幕上所在区域的Rect.

This to create a Rect of the area of where the button is located on the screen.

转到:

if event.type == pygame.mouse.get_pressed()

我更改为:

if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:

pygame.mouse.get_pressed()获取所有三个鼠标按钮(MOUSEBUTTONDOWN,MOUSEBUTTONUP或MOUSEMOTION)的状态.我还需要添加event.button == 1来指定这是被按下的鼠标左键".

The pygame.mouse.get_pressed() gets the state of all three mouse buttons (MOUSEBUTTONDOWN, MOUSEBUTTONUP, or MOUSEMOTION). I also needed to add in event.button == 1 to specify that this was the 'left-mouse' button being pressed.

最后:

`if button.collidepoint(pos):`

收件人:

`if b.collidepoint(pos):`

使用Rect b的碰撞点方法

这篇关于当鼠标“点击" .rect时,pygame动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:17