简体   繁体   English

当我继续点击方块时并不总是检测到它

[英]When I keep clicking the square not always detects it

I don't know what happend with my code but sometimes detects that I´m clicking something and sometimes doesn't, here is some code我不知道我的代码发生了什么,但有时会检测到我正在单击某些东西,有时却没有,这是一些代码

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

You can copy it if you want and If you htterclick the pink button you will see that in some occasions doesn't work correctly, thank you您可以根据需要复制它,如果您点击粉红色按钮,您会看到在某些情况下无法正常工作,谢谢

[...] sometimes detects that I´m clicking something and sometimes doesn't [...] [...] 有时会检测到我正在点击某些东西,但有时却没有 [...]

This is cause by the multiple event loops in your code.这是由代码中的多个事件循环引起的。 Note, pygame.event.get() gets all the messages and removes them from the queue.注意, pygame.event.get()获取所有消息并将它们从队列中删除。 So one of the event loops randomly gets the events and the other loops miss it.因此,其中一个事件循环随机获取事件,而其他循环错过了它。 Never all the event loops will get all the events.永远不会所有的事件循环都会得到所有的事件。 That causes that some events seems to be missed.这导致某些事件似乎被错过了。

Get the list of events in the main loop and pass them to the functions, to solve the issue:获取主循环中的事件列表并将它们传递给函数,以解决问题:

while True:

    # [...]

    # get the list of events
    events = pygame.event.get()

    # pass the vents to ipp
    ipp(events)            

    # handle quit event
    for event in events :
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
def hi_th_sprites(events):

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

    # [...]

def ipp(events):
    fill()
    hi_th_sprites(events)
class hitbox_things():
    def __init__(self, events, X, Y,width, height):

        # [...]

        if rectangulo.colliderect(self.hitbox):

            for event in events: # <---- use events 

                if event.type==pygame.MOUSEBUTTONDOWN:
                    if event.button==1:
                        num_dados=num_dados+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Turtle:单击时更改正方形的笔色 - Python Turtle: change pencolor of square when clicking 如何在热图中保持单元格方形? - How can I keep cells square in heatmap? 如何在可见时保持单击元素-SELENIUM - How to keep clicking element when visible - SELENIUM 如何通过单击画布上的另一个正方形来使我的移动正方形改变颜色? - How would I make my movebal square change colors by clicking another square on my canvas? 平方总是求幂法 - square always exponentiation method 如何保持和运行程序总是形成最后一个值,所以当我再次打开程序时,程序从最后一个量开始 - How to keep and run the program always form the last value, so when I open again the program starts from the last amount CloudFlare 检测到我正在使用 python selenium - CloudFlare Detects that i'm using python selenium 当我将方括号与 sqlalchemy 查询一起使用时,使用的查询是什么? - What is the query that is used in when I use square brackets with an sqlalchemy query? 当我循环时,为什么方阵会改变? - Why does the square matrix change when I loop through it? 当点进入正方形时,如何使点变色? - how can i make dots change color when they enter a square
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM