Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pygame 名称错误:名称';事件';没有定义_Pygame - Fatal编程技术网

Pygame 名称错误:名称';事件';没有定义

Pygame 名称错误:名称';事件';没有定义,pygame,Pygame,我试着运行我为一个游戏学校项目编写的代码,当我试图使用pygame_widgets dictionary在屏幕上放置一些按钮时,我试着将创建按钮的整个过程放在一个函数中,但该函数需要读取事件日志,以了解按钮是否被按下或悬停。。。而编译器一直在崩溃 import pygame import pygame_widgets pygame.init() WIDTH = 600 HEIGHT = 600 font_face_name = 'Merriweather-Regular.ttf' SCREE

我试着运行我为一个游戏学校项目编写的代码,当我试图使用pygame_widgets dictionary在屏幕上放置一些按钮时,我试着将创建按钮的整个过程放在一个函数中,但该函数需要读取事件日志,以了解按钮是否被按下或悬停。。。而编译器一直在崩溃

import pygame
import pygame_widgets

pygame.init()

WIDTH = 600
HEIGHT = 600
font_face_name = 'Merriweather-Regular.ttf'
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('SUDOKU game')
WHITE = (0, 0, 0)
BLACK = (255, 255, 255)
LOGO = pygame.transform.scale(pygame.image.load('assets/sudoku].png'), (150, 150))
background_image = pygame.transform.scale(pygame.image.load('assets/2.jpg'), (600, 600))


def write_on_SCREEN(surface, font_face, font_weight, text, color, x, y):
    my_font = pygame.font.SysFont(font_face, font_weight)
    logo_label = my_font.render(text, True, color)
    surface.blit(logo_label, (x, y))
    pygame.display.update()


def button_on_SCREEN(surface, position_x, position_y, button_width, button_height, button_label, label_size,
                     button_margin, color_inactive, color_pressed, color_hover, button_radius, on_click_function):

    button = pygame_widgets.button.Button(surface, position_x, position_y, button_width, button_height,
                                          text=button_label, fontSize=label_size, margin=button_margin,
                                          inactiveColour=color_inactive, pressedColour=color_pressed,
                                          hoverColour=color_hover, radius=button_radius, onClick=on_click_function)
    button.listen(event)
    button.draw()


def draw_on_SCREEN():
    SCREEN.blit(background_image, (0, 0))
    SCREEN.blit(LOGO, (225, 80))
    write_on_SCREEN(SCREEN, font_face_name, 60, 'sudoku game', WHITE, 170, 20)


def main():
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        draw_on_SCREEN()
        button_on_SCREEN(SCREEN, 250, 400, 200, 100, 'PLAY', 40, 30, (226, 175, 255), (222, 170, 255), (192, 253, 255),
                         20,
                         lambda: print('clicked'))
        pygame.display.update()
    pygame.quit()


if __name__ == '__main__':
    main()

在屏幕上的
def按钮
,行
按钮。侦听(事件)
,未定义事件。您需要将此部分放入pygame.event.get()中事件的循环
。避免两次获取事件,并将事件循环移动到屏幕上的
def button_
@D_00我尝试过这样做,但没有成功,我删除了屏幕上的函数def button_,只是在def main()中声明每个按钮,在while循环中,我声明了按钮。侦听(事件)和按钮。draw()虽然文本和边距的颜色不起作用,但它工作得很好,即使geving values我刚刚看到您的错误是未定义
事件
。我在代码中搜索,发现在屏幕上的
def button_
,有一个名为
event
的变量未定义。Idk关于其余的。关于文本的颜色,边距等,我想这是另一个问题。如果你愿意,你可以回答你的问题。