Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';布尔';对象不可调用?_Python_Python 2.7_Pygame - Fatal编程技术网

Python 类型错误:';布尔';对象不可调用?

Python 类型错误:';布尔';对象不可调用?,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在使用Python2.7和pygame我正在尝试制作一个太空入侵者游戏,但是我得到了这个错误,并且说这是一个线路问题 screen.blit(font.render(str(hits),True(255,255,255)),(400,320)) 有人能告诉我这意味着什么以及如何修复它吗?完整的代码是 import pygame, sys, random, time, math from pygame.locals import * pygame.init() clock = pygame.

我正在使用Python2.7和pygame我正在尝试制作一个太空入侵者游戏,但是我得到了这个错误,并且说这是一个线路问题

screen.blit(font.render(str(hits),True(255,255,255)),(400,320))
有人能告诉我这意味着什么以及如何修复它吗?完整的代码是

import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption("Space Invaders")
screen = pygame.display.set_mode((640,650))
badguy_image = pygame.image.load("images/badguy.png").convert()
badguy_image.set_colorkey((0,0,0))
fighter_image = pygame.image.load("images/fighter.png").convert()
fighter_image.set_colorkey((255,255,255))
GAME_OVER = pygame.image.load("images/gameover.png").convert()
font = pygame.font.Font(None,20)
last_badguy_spawn_time = 0
score = 0
shots = 0
hits = 0
misses = 0

class Badguy:

    def __init__(self):
        self.x = random.randint(0,570)
        self.y = -100
        d=(math.pi/2)*random.random()-(math.pi/4)
        speed = random.randint(2,6)
        self.dx=math.sin(d)*speed
        self.dy=math.cos(d)*speed

    def move(self):            
        self.x += self.dx
        self.y += self.dy

    def bounce(self):
        if self.x < 0 or self.x > 570:
            self.dx *= -1

    def off_screen(self):
        return self.y > 640

    def touching(self,missile):
        return (self.x+35-missile.x)**2+(self.y+22-missile.y)**2 < 1225

    def score(self):
        global score
        score+=100

    def draw(self):
        screen.blit(badguy_image,(self.x,self.y))    

class Fighter:
    def __init__(self):
        self.x = 320

    def move(self):
        if pressed_keys[K_LEFT] and self.x > 0:
            self.x -=3
        if pressed_keys[K_RIGHT] and self.x < 540:
            self.x +=3             

    def fire(self):
        global shots
        shots+=1
        missiles.append(Missile(self.x+50))

    def hit_by(self,badguy):
        return (
                badguy.y > 585 and
                badguy.x > self.x - 55 and
                badguy.x < self.x + 85
                )

    def draw(self):
        screen.blit(fighter_image,(self.x,591))            

class Missile:
    def __init__(self,x):
        self.x = x
        self.y = 591

    def move(self):
        self.y -= 5

    def off_screen(self):
        return self.y < -8

    def draw(self):
        pygame.draw.line(screen,(255,0,0),(self.x,self.y),(self.x,self.y+8),1)

badguys = []
fighter = Fighter()
missiles = []

while 1:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN and event.key == K_SPACE:
            fighter.fire()
    pressed_keys = pygame.key.get_pressed()

    if time.time() - last_badguy_spawn_time > 0.5:
        badguys.append(Badguy())
        last_badguy_spawn_time = time.time()

    screen.fill((0,0,0))
    fighter.move()
    fighter.draw()

    i = 0
    while i < len(badguys):
        badguys[i].move()
        badguys[i].bounce()
        badguys[i].draw()
        if badguys[i].off_screen():
            del badguys[i]
            i -= 1
        i += 1

    i = 0
    while i < len(missiles):
        missiles[i].move()
        missiles[i].draw()
        if missiles[i].off_screen():
            del missiles[i]
            misses += 1
            i -= 1
        i += 1

    i = 0
    while i < len(badguys):
        j = 0
        while j < len(missiles):
            if badguys[i].touching(missiles[j]):
                badguys[i].score()
                hits += 1
                del badguys[i]
                del missiles[j]
                i -= 1
                break
            j += 1
        i += 1

    screen.blit(font.render("Score: "+str(score),True,(255,255,255)),(5,5))

    for badguy in badguys:
        if fighter.hit_by(badguy):
            screen.blit(GAME_OVER,(170,200))            
            screen.blit(font.render(str(shots),True,(255,255,255)),(266,320))
            screen.blit(font.render(str(score),True,(255,255,255)),(266,348))
            screen.blit(font.render(str(hits),True(255,255,255)),(400,320))
            screen.blit(font.render(str(misses),True,(255,255,255)),(400,377))
            if shots == 0:
                screen.blit(font.render("--",True,(255,255,255)),(400,357))
            else:
                screen.blit(font.render(str((1000*hits/shots)/10.)+"%",True,(255,255,255)),(400,357))            
            while 1:
                for event in pygame.event.get():
                    if event.type == QUIT:
                        sys.exit()
                pygame.display.update()

    pygame.display.update()    
导入pygame、sys、random、time、math
从pygame.locals导入*
pygame.init()
clock=pygame.time.clock()
pygame.display.set_标题(“太空入侵者”)
screen=pygame.display.set_模式((640650))
badguy_image=pygame.image.load(“images/badguy.png”).convert()
badguy_图像。设置颜色键((0,0,0))
fighter_image=pygame.image.load(“images/fighter.png”).convert()
战斗机图像。设置彩色键((255255))
GAME_OVER=pygame.image.load(“images/gameover.png”).convert()
font=pygame.font.font(无,20)
最后一个坏蛋产卵时间=0
分数=0
镜头=0
点击率=0
未命中=0
班上的坏蛋:
定义初始化(自):
self.x=random.randint(0570)
self.y=-100
d=(math.pi/2)*random.random()-(math.pi/4)
速度=随机。随机数(2,6)
self.dx=数学sin(d)*速度
self.dy=数学cos(d)*速度
def移动(自我):
self.x+=self.dx
self.y+=self.dy
def反弹(自我):
如果self.x<0或self.x>570:
self.dx*=-1
def关闭屏幕(自身):
返回self.y>640
def触摸(自身、导弹):
返回(self.x+35导弹.x)**2+(self.y+22导弹.y)**2<1225
def分数(自我):
全球得分
分数+=100
def牵引(自):
blit(badguy_图像,(self.x,self.y))
职业战斗机:
定义初始化(自):
self.x=320
def移动(自我):
如果按下[K_LEFT]键和self.x>0:
self.x-=3
如果按下[K_RIGHT]键和self.x<540:
self.x+=3
def火灾(自):
全局快照
快照+=1
导弹.附加(导弹(自身x+50))
def被(赛尔夫,坏蛋)击中:
返回(
坏家伙,y>585和
badguy.x>self.x-55和
坏人.x0.5:
Badguy.append(Badguy())
last_badguy_spawn_time=time.time()
屏幕填充((0,0,0))
格斗者移动
战斗机
i=0
而我(坏人):
坏人[i].move()
坏人[i].bounce()
坏人[i].draw()
如果坏人[i]。离开屏幕()
德尔巴德[i]
i-=1
i+=1
i=0
而我(导弹):
导弹[i].move()
导弹[i].draw()
如果[i]。离开屏幕():
导弹[一]
未命中+=1
i-=1
i+=1
i=0
而我(坏人):
j=0
而j
您忘记了逗号
True
之后-所以现在您尝试调用函数
True()


但是
True
bool
对象,不是函数-所以它不是
“可调用的”
-你会出错。

你忘记了逗号
之后是
True
-所以现在你试着
True()
我怎样才能避免将来错过这样的事情呢?我多次查看该代码,从未注意到您无法避免的丢失的命令,但现在您知道了此错误的含义以及问题所在-因此下次您可以轻松解决此问题。顺便说一句:如果您使用
IDE
PyCharm
PyDev
,等等)相反,
notepad++
,它可以警告您预期的问题,但我敢肯定。