Python/PyGame-健康栏被卡住;-=运算符似乎没有工作

Python/PyGame-健康栏被卡住;-=运算符似乎没有工作,python,pygame,Python,Pygame,我无法找到错误的来源,“运行状况”下降一次,但无法持续更新。故障似乎起源于第105行,“-=”运算符似乎不起作用 import pygame, random, funks class screen_attributes(): def __init__(self): global screen self.WIDTH=screen.get_width() self.HEIGHT=screen.get_height() self.

我无法找到错误的来源,“运行状况”下降一次,但无法持续更新。故障似乎起源于第105行,“-=”运算符似乎不起作用

import pygame, random, funks
class screen_attributes():
    def __init__(self):
        global screen
        self.WIDTH=screen.get_width()
        self.HEIGHT=screen.get_height()
        self.CAPTION='space_race'
pygame.init()
s_info = pygame.display.Info()
screen = pygame.display.set_mode((s_info.current_w,s_info.current_h),pygame.FULLSCREEN)
main=screen_attributes()
pygame.display.set_caption(main.CAPTION)
def __init__():
    global main
    global exited
    exited = False
    global objects
    objects=[]
    for i in range(0, 50):
        objects.append(star())
    for i in range(0, 5):
        objects.append(battery())
    objects+=[ship()]
    for i in range(-1*(main.WIDTH-10),2*(main.WIDTH-10), 320):
        objects.append(asteroid(i,main.HEIGHT*-1))
        objects.append(asteroid(i,main.HEIGHT*2))
    for i in range(-1*(main.HEIGHT-10),2*(main.HEIGHT-10), 256):
        objects.append(asteroid(main.WIDTH*-1,i))
        objects.append(asteroid(main.WIDTH*2,i))
    global ui
    ui=[health_bar()]
    global flow
    flow='up'

class ship():
    def __init__(self):
        global main
        self.x= main.WIDTH/2-75
        self.y= main.HEIGHT/2-200
        self.direction= 'up'
        self.directions={'up':0 , 'down':180 , 'right':270 , 'left':90 ,}
        self.original_image=pygame.image.load('ship.png')
        self.image=self.original_image
        self.rects=self.image.get_rect()
        self.type='ship'
    def update(self):
        global flow
        global ui
        if pygame.key.get_pressed()[pygame.K_UP]:
            flow='up'
        if pygame.key.get_pressed()[pygame.K_DOWN]:
            flow='down'
        if pygame.key.get_pressed()[pygame.K_RIGHT]:
            flow='right'
        if pygame.key.get_pressed()[pygame.K_LEFT]:
            flow='left'
        self.image=pygame.transform.rotate(self.original_image, self.directions[flow])
        for item in objects:
            if funks.collided(self, item):
                if item.type=='battery':
                    ui[0].health=1.00
class star():
    def __init__(self):
        global main
        self.image=pygame.image.load('star.png')
        self.x=random.randint(-1*(main.WIDTH-10),2*(main.WIDTH-10))
        self.y=random.randint(-1*(main.HEIGHT-10),2*(main.HEIGHT-10))
        self.type='star'
    def update(self):
        if flow=='up':
            self.y+=1
        if flow=='down':
            self.y-=1
        if flow=='right':
            self.x-=1
        if flow=='left':
            self.x+=1
class asteroid():
    def __init__(self,x,y):
        self.image=pygame.image.load('asteroid.png')
        self.x=x
        self.y=y
        self.rects=self.image.get_rect()
        self.type='asteroid'
    def update(self):
        if flow=='up':
            self.y+=1
        if flow=='down':
            self.y-=1
        if flow=='right':
            self.x-=1
        if flow=='left':
            self.x+=1
class health_bar():
    def __init__(self):
        global main
        self.health=1.00
        self.image=pygame.image.load('health.png')
        self.y=20
        self.x=main.WIDTH*0.8
        self.height=20
        self.width=200
    def update(self):
        if self.health > 0.00:
            self.health-=0.0001
            print self.health
        self.image=pygame.transform.scale(self.image, (int(self.width*self.health), self.height))
class battery():
    def __init__(self):
        self.x=random.randint(-1*(main.WIDTH-10),2*(main.WIDTH-10))
        self.y=random.randint(-1*(main.HEIGHT-10),2*(main.HEIGHT-10))
        self.image=pygame.image.load('battery.png')
        self.rects=self.image.get_rect()
        self.type='battery'
    def update(self):
        if flow=='up':
            self.y+=1
        if flow=='down':
            self.y-=1
        if flow=='right':
            self.x-=1
        if flow=='left':
            self.x+=1












__init__()
while not exited:
    if pygame.key.get_pressed()[pygame.K_ESCAPE]:
        pygame.quit()
    pygame.event.get()
    screen.fill((0,0,0))
    for item in objects:
        item.update()
        screen.blit(item.image, [item.x, item.y])
    for item in ui:
        item.update()
        screen.blit(item.image, [item.x, item.y])
    pygame.display.update()
导入的“funks.py”文件只是检测碰撞

def collided(ob1, ob2):
    try:
        if ob1.x <= (ob2.x + (ob2.rect[0])) and (ob1.x + (ob1.rect[0])) >= ob2.x:
             if ob1.y <= (ob2.y + (ob2.rect[1])) and (ob1.y + (ob1.rect[1])) >= ob2.y:
                return True
    except:
        return 'no rects'
工作很好

class health_bar():
    def __init__(self):
        #global main
        self.health=1.00
        #self.image=pygame.image.load('health.png')
        self.y=20
        self.x=20
        self.height=20
        self.width=200
    def update(self):
        if self.health > 0.00:
            self.health-=0.0001
            print self.health

a = health_bar()
a.update()
a.update() 
a.update()

我自己发现了这个问题。感谢那些给出了富有成效的答案的人,但是我想告诉大家,错误源于代码中似乎不完全相关或“最小”的部分。在“宇宙飞船”更新功能中,它检查飞船是否与任何“电池”碰撞。这段代码总是返回真值,此后总是将运行状况更改回100%。

您能给出一个最小的完整示例吗?请参阅--问题应包括可能重现相同问题的最小代码片段。(通常,分离这样一个片段的过程会导致在不需要提问的情况下找到问题)。@TessellingEckler你应该把这个作为答案,因为我很确定你已经准确地找到了OP的答案problem@JoranBeasley除了我的数学很糟糕。它不会有10000次更新,只需要500次更新就可以再次更改-以每秒30帧的速度更改,这将是16秒,而不是5分钟。仍然可能是问题所在-您等待更改的时间有多长,@ChrisReal?多久调用一次更新?此时,没有实际的预期时间,只是针对功能,这就是我感到困惑的原因。您可以尝试打印
id(self)
我猜您是在重新关联健康栏或其他东西,或者这是一个逻辑问题。。。请参阅@Tessellingheckler对原始问题的评论
x=5
x-=1
print x
class health_bar():
    def __init__(self):
        #global main
        self.health=1.00
        #self.image=pygame.image.load('health.png')
        self.y=20
        self.x=20
        self.height=20
        self.width=200
    def update(self):
        if self.health > 0.00:
            self.health-=0.0001
            print self.health

a = health_bar()
a.update()
a.update() 
a.update()