Reputation: 91
#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()
This is how I make my button in my game. It works for all buttons other than the one which causes this error:
line 50, in button
action()
TypeError: 'bool' object is not callable
If anyone can help me, that would be great and any improvement on my code would be great as well.
Upvotes: 1
Views: 189
Reputation: 20438
You have a local hel
variable (a boolean) in your intro
function which gets passed to the button
function instead of the global hel
function. Just remove this line hel = False
, so that Python can find the global hel
function and pass it to button
.
Take a look at this question: Short Description of the Scoping Rules?
Also, you're blitting the buttons outside the screen. Correct their coordinates.
Upvotes: 1