2016-07-19 28 views
0

私はこのオブジェクトをpygameでクリックすると移動させようとしています。そしてそれはあなたがそれをクリックしてください最初の時間を動作しますが、その後、それは私に、このエラーを与える:python/pygameエラーTypeError: 'bool'オブジェクトが呼び出し可能ではありません

game_loop() 
    File "C:\Users\MadsK_000\Desktop\spil\Python\spiltest\Test spil.py", line 57, in game_loop 
    Clicked_ = clicked(x,y,width,height,mouse_pos) 
TypeError: 'bool' object is not callable 

は、ここであなたがclicked機能内で、ブール値にグローバルclickedを設定する私のコード

import pygame 
import time 
pygame.init() 

display_width = 800 
display_height = 600 

black = (0,0,0) 
white = (255,255,255) 
red = (255,0,0) 
gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption("test") 
clock = pygame.time.Clock() 
mainthingImg = pygame.image.load("mainthing.PNG") 
width = 88 
height = 85 
x = 100 
y = 100 
mouse_pos = pygame.mouse.get_pos() 
def mainthing(x,y): 
    gameDisplay.blit(mainthingImg, (x,y)) 

def clicked(x,y,width,height,mouse_pos): 
    clicked = False 
    if mouse_pos[0] > x and x + width > mouse_pos[0] and mouse_pos[1] > y and y + height > mouse_pos[1]: 
     clicked = True 
     global clicked 

    return clicked 

def text_objects(text, font): 
    textSurface = font.render(text, True, white) 
    return textSurface, textSurface.get_rect() 

def ptd(text): 
    stortext = pygame.font.Font("freesansbold.ttf", 40) 
    TextSurf, TextRect = text_objects(text,stortext) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 
    pygame.display.update() 
    time.sleep(2) 
    game_loop() 


def game_loop(): 



    gameExit = False 
    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_pos = pygame.mouse.get_pos() 
       Clicked_ = clicked(x,y,width,height,mouse_pos) 
       if Clicked_ == True: 
        x += 100 
        y += 100 
        global x 
        global y 
     gameDisplay.fill(red) 
     mainthing(x,y) 
     pygame.display.update() 
     clock.tick(60) 
ptd("Wellcome") 
pygame.display.update() 
game_loop() 
pygame.quit() 
quit() 
+0

質問に加えて、おそらくゲームロジックをクラスに、グローバル変数を属性に入れてください。グローバル変数のそれは、本当に悪いスタイルです。 –

答えて

3

です:

def clicked(x,y,width,height,mouse_pos): 
    clicked = False 
    if mouse_pos[0] > x and x + width > mouse_pos[0] and mouse_pos[1] > y and y + height > mouse_pos[1]: 
     clicked = True 
     global clicked 

    return clicked 

異なるグローバルNAを使用してください私のブール値の場合は、またはclicked関数の名前を変更します。関数は単なるグローバルでもあります。

0

clickedの名前の競合があります。関数clickedの中には同じ名前の変数(clicked = False)があり、これもグローバルスコープ(global clicked)にリンクされています。したがって、関数が実行されると、clickedが関数ではなくブール値に変更されました(関数定義では、スコープ内に変数名が作成されます)。関数と変数を別々に指定してください。

関連する問題