当你在pygame中点击它时,我正试图让这个对象移动;并且它在您第一次单击它时起作用但在此之后它给了我这个错误:
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
这是我的代码
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)
您在clicked
功能中将全局clicked
设置为布尔值:
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
为布尔值使用不同的全局名称,或重命名clicked
函数。函数也只是全局变量。
答案 1 :(得分:0)
clicked
存在名称冲突。在函数clicked
内部,变量具有相同的名称(clicked = False
),并且还链接到全局范围(global clicked
)。因此,当执行该函数时,clicked
已被修改为boolean,而不是函数(函数定义在其范围内创建变量名,global here)。请分别命名函数和变量。