2016-07-19 4 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이 function 대신 boolean으로 수정되었습니다 (함수 정의는 해당 범위에 전역 변수 이름을 만듭니다.). 함수와 변수의 이름을 따로 지정하십시오.

관련 문제