繁体   English   中英

Pygame.mouse.get_pressed() 不会检测到我的大部分点击

[英]Pygame.mouse.get_pressed() wont detect most of my clicks

所以这是我的代码:

import pygame
import pygame as pg

pygame.init()

displayWidth = 800
displayHeight = 600

### colour codes ###
bgColour = (245, 230, 255)
grey = (150, 150, 150)
darkGrey = (100, 100, 100)
darkBlack = (0,0,0)

clock = pygame.time.Clock()

mouse_clicked = False

display = pygame.display.set_mode((displayWidth, displayHeight))#sets height and width of screen
pygame.display.set_caption('Fashion!')

def screenDisplay():                                                #subroutine to display screens
    display = pygame.display.set_mode((displayWidth, displayHeight))#sets height and width of screen
    pygame.display.set_caption('Fashion!')                          #sets screen title
    display.fill(bgColour)

def text_objects(text, font):                       #font colour
    textSurface = font.render(text, True, darkBlack)
    return textSurface, textSurface.get_rect()

def textDisplay(s,t,x,y):                           #subroutine for displaying text on screen
    smallText = pygame.font.SysFont("",s)           #creates front and font size
    textSurf, textRect = text_objects(t, smallText) #inputs text
    textRect.center = (x,y)                         #centres text
    display.blit(textSurf, textRect)                #displays test


def button(msg,s,x,y,w,h,ic,ac,action=None):    #format for button
    mouse = pygame.mouse.get_pos()                  #gets mouse position (tracks cursor)
    click = pygame.mouse.get_pressed()              #gets status of mouse (tracks click)
    print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:   #draws rectangle for button
        pygame.draw.rect(display, ac,(x,y,w,h))     #draws rectangle after colour if mouse is on area

        if click[0] == 1 and action != None:        #performs function on click
            action()      
    else:
        pygame.draw.rect(display,ic,(x,y,w,h))      #draws rectangle initial colour if mouse isnt on area

    smallText = pygame.font.SysFont("",s)           #adds text to button
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    display.blit(textSurf, textRect)


    smallText = pygame.font.SysFont("",s)           #adds text to button
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    display.blit(textSurf, textRect)

def menuDisplay():
    intro = True

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

        button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
        button("OUTFIT",  60,200,250,400,100,grey,darkGrey,page)
        button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
        button("QUIT",    40,300,530,200,50,grey,darkGrey,page) 

        pygame.display.update()
        clock.tick(30)

def page():
        intro = True

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

            pygame.display.update()
            clock.tick(30)


menuDisplay()

它打印 get press 但它只返回 [0,0,0] 直到随机时间(有时直接有时在 10 次点击后有时在 50 次点击后)它注册 [1,0,0]。 它甚至从未在其他计算机上工作过。 当我没有时钟时它也不起作用,所以没有改变任何东西。

我很伤心压力,请帮助💔🙏

问题是在screenDisplay调用pygame.display.set_mode 请注意, pygame.display.set_mode重新初始化窗口并导致丢失所有鼠标事件状态。
screenDisplay在主应用程序循环中被调用。 在每一帧中初始化显示是一种性能浪费和糟糕的风格。

不要在主应用循环中调用screenDisplay ,只需通过display.fill(bgColour)清除显示即可解决问题:

def menuDisplay():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        # screenDisplay() <-- DELETE
        display.fill(bgColour)

        button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
        button("OUTFIT",  60,200,250,400,100,grey,darkGrey,page)
        button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
        button("QUIT",    40,300,530,200,50,grey,darkGrey,page) 

        pygame.display.update()
        clock.tick(30)

page()做同样的事情

def page():
        intro = True

        while intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            # screenDisplay() <-- DELETE
            display.fill(bgColour)

            pygame.display.update()
            clock.tick(30)

我改变了一些东西,这有效:

import pygame

pygame.init()

displayWidth = 800
displayHeight = 600

### colour codes ###
bgColour = (245, 230, 255)
grey = (150, 150, 150)
darkGrey = (100, 100, 100)
darkBlack = (0,0,0)

smallText = pygame.font.SysFont("",32)

display = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Fashion!')

def text_objects(text, font):                       #font colour
    textSurface = font.render(text, True, darkBlack)
    return textSurface, textSurface.get_rect()

def button(msg,s,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    print(click)

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(display, ac,(x,y,w,h)) 
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(display, ic,(x,y,w,h))

    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    display.blit(textSurf, textRect)

def menuDisplay():
    display.fill(bgColour)
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
        button("OUTFIT",  60,200,250,400,100,grey,darkGrey,page)
        button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
        button("QUIT",    40,300,530,200,50,grey,darkGrey,page) 

        pygame.display.update()

def page():
    display.fill(bgColour)
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        pygame.draw.rect(display,grey,(50,50,50,50) )   
        pygame.display.update()



menuDisplay()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM