按下按钮时Pygame Bool错误

时间:2018-08-04 13:02:01

标签: 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()

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

  

第50行,在按钮中
      动作()
  TypeError:“布尔”对象不可调用

如果有人可以帮助我,那将是非常棒的,我对代码的任何改进也将很棒。

1 个答案:

答案 0 :(得分:1)

您的hel函数中有一个本地intro变量(布尔值),该变量被传递给button函数而不是全局hel函数。只需删除这一行hel = False,以便Python可以找到全局hel函数并将其传递给button

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

此外,您还将在屏幕外显示按钮。纠正他们的坐标。