我无法找到错误的来源,“健康”一度下降,但无法持续更新。失败似乎来自第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=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))
答案 0 :(得分:1)
我自己发现了这个问题。感谢那些给出了富有成效的答案的人,但是我想告诉所有人,错误源于代码的一部分似乎并不完全相关或“最小”。在“太空船”更新功能中,它会检查船是否与任何“电池”发生碰撞。这段代码总是返回True,以后总是将生命值恢复到100%。
答案 1 :(得分:0)
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()