当我不断单击方块时,并不总是能检测到它

问题描述 投票:0回答:1

我不知道我的代码发生了什么,但是有时会检测到我在点击某些东西,有时却没有,这是一些代码

import pygame
from pygame.locals import *
import sys

pygame.init()

modeX=500
modeY=600

rectangulo=pygame.Rect(1,1,2,2)


num_dados=0

ven=pygame.display.set_mode((modeX, modeY))

fps=pygame.time.Clock()

def fill():     
    ven.fill((0,0,0))

def text(txt, x, y, size,font, color):  
    myfont=pygame.font.SysFont(font,size)
    myText=myfont.render(txt,0,(color))
    ven.blit(myText,(x,y))

class hitbox_things():
    def __init__(self, X, Y,width, height):
        global escena, num_dados

        self.hitbox=pygame.Rect(X,Y,width,height)   

        pygame.draw.rect(ven, (255,0,255), self.hitbox)

        if rectangulo.colliderect(self.hitbox):

            for event in pygame.event.get():  

                if event.type==pygame.MOUSEBUTTONDOWN:

                    if event.button==1:
                        num_dados=num_dados+1

def hi_th_sprites():

    hitbox_things(180,30,30,30)
    hitbox_things(40,30,30,30)


    text(str(int(fps.get_fps())), 2, 22, 40, "Fixedsys", (255,255,255))
    text(str(num_dados), 100, 22, 40, "Fixedsys", (255,255,255))

def ipp():
    fill()
    hi_th_sprites()

################### UPDATE ##########################
class update:
    def __init__(self):

        while True:

            FPS=fps.tick(60)

            rectangulo.left, rectangulo.top=pygame.mouse.get_pos()

            ipp()

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

            pygame.display.flip()

ven=pygame.display.set_mode((modeX, modeY))

update()

如果需要,您可以复制它,如果您单击粉色按钮,您会发现在某些情况下无法正常工作,谢谢]

python pygame
1个回答
0
投票

[...]有时会检测到我在点击某些东西,有时却没有[...]

© www.soinside.com 2019 - 2024. All rights reserved.