python - 按下按钮时 Pygame Bool 错误

标签 python python-3.x pygame

#campaign module for blunt wars
import pygame
import time
import sys

pygame.init()

pygame.mixer.music.load("Library\Sound\Light.wav")

size = [500, 500]
width = size[0]
height = size[1]

#colors

black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
bright_red = (255,0,0)
bright_green = (0,255,0)
#end colors

screen = pygame.display.set_mode((size), pygame.RESIZABLE)
pygame.display.set_caption('Blunt Wars - Campaign')
clock = pygame.time.Clock()
Icon = pygame.image.load('Library\Image\Icon.png')
pygame.display.set_icon(Icon)
bg = pygame.image.load('bg.png')


text = pygame.font.SysFont('Arial', 30)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    # Create the text rect.
    textRect = textSurface.get_rect()
    # Return a tuple consisting of the surface and the rect.
    return textSurface, textRect

def button(msg,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(screen, ac,(x,y,w,h))

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

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    screen.blit(textSurf, textRect)

def intro():
    intro = True
    #screen.fill(white)
    #screen.blit(bg, (0,0))
    pygame.mixer.music.play(-1)
    hel = False
    while intro:
        screen.blit(bg, (0,0))
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                #quit()
                import Blunt_Wars
            if event.type == pygame.VIDEORESIZE:
                surface = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)

        #screen.fill(blue)
        largeText = pygame.font.SysFont('Castellar',50)
        textSurf, textRect = text_objects("Blunt Wars - Campaign", largeText)
        textRect.center = ((700),(100))
        screen.blit(textSurf, textRect)

        button("Start New",650,200,100,50,green,bright_green,game_loop)
        button("Load Save",650,300,100,50,green,bright_green,load_save)
        button("Help",650,400,100,50,green,bright_green,hel)
        button("Settings",650,500,100,50,green,bright_green,settings)
        button("Main Menu",650,600,100,50,red,bright_red,leave)

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


def leave():
    pygame.mixer.music.stop()
    pygame.quit()
    import Blunt_Wars

def settings():
    pygame.mixer.music.stop()
    print("settings loaded")
    time.sleep(4)
    pygame.quit()
    quit()

largeText = pygame.font.SysFont('Castellar',50)

def hel():
    intro = False
    help = True
    while help:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                #import Blunt_Wars
                quit()
        #pygame.mixer.music.stop()
        screen.fill(white)
        screen.blit(bg, (0,0))
        textSurf, textRect = text_objects("Help Page", largeText)
        textRect.center = ((700),(100))
        screen.blit(textSurf, textRect)
        pygame.display.update()



def load_save():
    pygame.mixer.music.stop()
    print("save loaded")
    time.sleep(2)
    pygame.quit()
    quit()


def game_loop():
    pygame.mixer.music.stop()
    print("game loop ran")
    time.sleep(10)
    pygame.quit()
    quit()

这就是我在游戏中制作按钮的方式。它适用于除导致此错误的按钮之外的所有按钮:

line 50, in button
action()
TypeError: 'bool' object is not callable

如果有人可以帮助我,那就太好了,对我的代码的任何改进也都很棒。

最佳答案

您有本地hel intro 中的变量( bool 值)函数被传递到 button函数而不是全局 hel功能。只需删除此行 hel = False ,这样Python就可以找到全局hel函数并将其传递给button .

看看这个问题:Short Description of the Scoping Rules?

此外,您正在将按钮复制到屏幕之外。修正它们的坐标。

关于python - 按下按钮时 Pygame Bool 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685860/

相关文章:

python - 为什么要返回元组?

python - 从函数中收集警告的大多数 pythonic 方式

python - 名称错误 'html' 未使用 beautifulsoup4 定义

python - 在 pygame 中翻转 Sprite

import - MacOSX python 导入错误

python - 每隔 x(在 pygame 中为毫秒

python - 使用 lxml xpath 解析

python - Sphinx 覆盖生成空的 python.txt 文件

Python Azure-Storage 0.33.0 与 Azure WebApp 发生冲突

python - 如何拆分数据框单元格中的数据并在拆分时执行 Pandas groupby?