小编Ard*_*tun的帖子

pygame按钮单击

我在pygame中制作了用于单击事件的按钮,但是存在问题。当我单击鼠标按钮并在按钮边界之间移动鼠标时,单击事件会重复发生。我只想单击一下,直到释放鼠标按钮。我怎样才能做到这一点?

import pygame,time
pygame.init()
x,y = (200,300)
pencere = pygame.display.set_mode((x,y))
pygame.display.set_caption("Click")

white = (255,255,255)
black = (0,0,0)
black2 = (30,30,30)

class Counter:
    count = 0
    def click(self):
        self.count += 1

number = Counter()
def text_objects(text, font, color):
    textSurface = font.render(text, True, color)
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,c,ic,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    pygame.draw.rect(pencere, c,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf",20)
    textSurf, textRect = text_objects(msg, smallText, white)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    pencere.blit(textSurf, textRect)

    if x+w > mouse[0] > x and y+h …
Run Code Online (Sandbox Code Playgroud)

python mouse pygame click button

2
推荐指数
1
解决办法
6299
查看次数

标签 统计

button ×1

click ×1

mouse ×1

pygame ×1

python ×1