来自 class 的函数的 Return 不适用于 pygame 的按钮

Return from a function from class isn't working for button for pygame

出于某种原因,我的 return 无法正常工作。这是来自一个教程。当我下载文件并从那里编辑它时,它可以工作,但如果我从确切的文件复制并粘贴它,它就不起作用。抱歉,我是初学者 - 欢迎任何建议

这是我使用的教程: https://www.youtube.com/watch?v=G8MYGDf_9ho

代码:

import pygame
import sys

pygame.init()

WinHeight = 600
WinWidth = 900
Window = pygame.display.set_mode((WinWidth,WinHeight))
#button class
class Button():
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False

    def draw(self, surface):
        action = False
        #get mouse position
        pos = pygame.mouse.get_pos()

        #check mouseover and clicked conditions
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False

        #draw button on screen
        surface.blit(self.image, (self.rect.x, self.rect.y))

        return action



Good_ball_img = pygame.image.load("GoodBall.png")
Bad_ball_img = pygame.image.load("BadBall.png")


#Button instances
Good_ball = Button(100,100,Good_ball_img,2)
Bad_ball = Button(200,200,Bad_ball_img,3)

def drawwin():
    Window.fill((202,241,208))
    Good_ball.draw(Window)
    Bad_ball.draw(Window)



   pygame.display.update()




def Main():
    run = True
    while run:
        if Good_ball.draw(Window):
            print("green clicked")
        if Bad_ball.draw(Window)=="t":
            print("red clicked")
        for event in pygame.event.get():
                #quit game
                if event.type == pygame.QUIT:
                        run = False
                        pygame.quit()
                        sys.exit()
        drawwin()
        checkpress()


            
    



if __name__ == "__main__":
    Main()

如果我更改顺序,代码对我有用 - 首先处理所有事件,然后检查鼠标位置。

问题可能是因为 PyGame 仅当您 运行 pygame.event.get() 时才更新 pygame.mousepygame.key 中的值 - 所以它可能需要 pygame.event.get() 或至少 pygame.event.pump() 在其他函数之前。

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

        if Good_ball.draw(Window):
            print("green clicked")
        if Bad_ball.draw(Window):
            print("red clicked")

编辑:

pygame.event 的文档中有

To get the state of various input devices, you can forego the event queue and 
access the input devices directly with their appropriate modules: `pygame.mouse`, 
`pygame.key` and `pygame.joystick`. If you use this method, remember 
that pygame requires some form of communication with the system window manager 
and other parts of the platform. To keep pygame in sync with the system, 
you will need to call `pygame.event.pump()` to keep everything current. 

最少的工作代码 - 使用表面而不是图像,因此每个人都可以简单地复制和 运行 它

import pygame
import sys

# --- constants ---  # PEP8: `UPPER_CASE_NAMES`

WINDOW_WIDTH = 900
WINDOW_HEIGHT = 600

# --- classes ---  # PEP8: `CamelCaseNames`

class Button():
    
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False

    def check_click(self):
        action = False

        # get mouse
        pos = pygame.mouse.get_pos()
        left_button = pygame.mouse.get_pressed()[0]

        # check mouseover and clicked conditions

        if left_button:
            if self.rect.collidepoint(pos) and not self.clicked:
                self.clicked = True
                action = True
        else:
            self.clicked = False

        return action

    def draw(self, surface):
        # draw button on screen
        surface.blit(self.image, self.rect)

# --- functions ---  # PEP8: `lower_case_names`

def draw_window(window):
    window.fill((202, 241, 208))
    good_ball.draw(window)
    bad_ball.draw(window)

    pygame.display.update()

# --- main --- 

pygame.init()

window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

# button class
#good_ball_img = pygame.image.load("GoodBall.png")
good_ball_img = pygame.Surface((100, 50))
good_ball_img.fill((0, 255, 0))

#bad_ball_img = pygame.image.load("BadBall.png")
bad_ball_img = pygame.Surface((100, 50))
bad_ball_img.fill((255,0,0))

# Button instances
good_ball = Button(100, 100, good_ball_img, 2)  # 
bad_ball  = Button(200, 200, bad_ball_img, 3)

run = True

while run:

    # - events -
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            sys.exit()

    # - checks and updates -
    
    if good_ball.check_click():
        print("green clicked")
    if bad_ball.check_click():
        print("red clicked")

    #checkpress()

    # - draws -
    
    draw_window(window)

PEP 8 -- Style Guide for Python Code