Python 敌人赢了';运行状况达到0后无法显示-如何修复?
在我的主循环中,我为敌人增加生命值。如果我继续攻击敌人,生命值下降到0,敌人应该消失(Python 敌人赢了';运行状况达到0后无法显示-如何修复?,python,pygame,Python,Pygame,在我的主循环中,我为敌人增加生命值。如果我继续攻击敌人,生命值下降到0,敌人应该消失(enemys1.visible=False) 我在我的敌人类上运行了可视变量,但由于某些原因,敌人不会隐身,当其生命值达到0时仍然会出现 这是我游戏中的一个例子 对于项目符号中的项目符号: 如果bullet.rect.collide rect(敌人1.hitbox): 子弹.弹孔(子弹.索引(子弹)) 如果enemys1.health>0: 敌人1.健康-=1 其他: 敌人1.visible=False#如果我
enemys1.visible=False
)
我在我的敌人类上运行了可视变量,但由于某些原因,敌人不会隐身,当其生命值达到0时仍然会出现
这是我游戏中的一个例子
对于项目符号中的项目符号:
如果bullet.rect.collide rect(敌人1.hitbox):
子弹.弹孔(子弹.索引(子弹))
如果enemys1.health>0:
敌人1.健康-=1
其他:
敌人1.visible=False#如果我的生命值达到0,敌人应该消失,但不会消失
打印('hit')
我的敌人阶级:
class enemys:
def __init__(self,x,y,height,width,end):
self.x = x
self.y =y
self.esright = [pygame.image.load("esright1.png"),
pygame.image.load("esright1.png"),
pygame.image.load("esright2.png"),
pygame.image.load("esright3.png"),
pygame.image.load("esright4.png"),
pygame.image.load("esright5.png"),
pygame.image.load("esright6.png"),
pygame.image.load("esright7.png"),
pygame.image.load("esright8.png"),
pygame.image.load("esright9.png"),
pygame.image.load("esright10.png"),
pygame.image.load("esright11.png"),
pygame.image.load("esright12.png"),
pygame.image.load("esright13.png"),
pygame.image.load("esright14.png"),
pygame.image.load("esright15.png"),
pygame.image.load("esright16.png"),
pygame.image.load("esright17.png"),
]
self.esleft = [pygame.image.load("esleft1.png"),
pygame.image.load("esleft1.png"),
pygame.image.load("esleft2.png"),
pygame.image.load("esleft3.png"),
pygame.image.load("esleft4.png"),
pygame.image.load("esleft5.png"),
pygame.image.load("esleft6.png"),
pygame.image.load("esleft7.png"),
pygame.image.load("esleft8.png"),
pygame.image.load("esleft9.png"),
pygame.image.load("esleft10.png"),
pygame.image.load("esleft11.png"),
pygame.image.load("esleft12.png"),
pygame.image.load("esleft13.png"),
pygame.image.load("esleft14.png"),
pygame.image.load("esleft15.png"),
pygame.image.load("esleft16.png"),
pygame.image.load("esleft17.png"),
]
self.esright = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esright]
self.esleft = [pygame.transform.scale(image,(image.get_width()//3,image.get_height()//3)) for image in self.esleft]
self.height = height
self.width = width
self.anim_index = 0
self.distance = 80
self.speed = 8
self.vel = 3
self.path = [x,end]
self.Walking_index = 0
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.rect = pygame.Rect(x,y,height,width)
# enemys health
self.health = 10
self.visible = True
# this makes the enemy move right and left
def draw(self,window):
self.move()
if self.Walking_index + 1 >= 33:
self.Walking_index = 0
if self.vel > 0:
window.blit(self.esright[self.Walking_index//3], (self.x,self.y))
self.Walking_index += 1
else:
window.blit(self.esleft[self.Walking_index//3], (self.x,self.y))
self.Walking_index += 1
# this moves the enemy left and right
def move(self):
if self.visible:
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking_index = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking_index = 0
# the hit box for the enemy the health
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 70, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 70 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 47, self.y + 31, 50, 72)
# THIS PART MAKES the enemy not scroll with the player
def scroll(self,sx, sy):
self.x += sx
self.y += sy
self.path[0] += sx
self.path[1] += sx
class敌人:
定义初始(自、x、y、高度、宽度、终点):
self.x=x
self.y=y
self.esright=[pygame.image.load(“esright1.png”),
pygame.image.load(“esright1.png”),
pygame.image.load(“esright2.png”),
pygame.image.load(“esright3.png”),
pygame.image.load(“esright4.png”),
pygame.image.load(“esright5.png”),
pygame.image.load(“esright6.png”),
pygame.image.load(“esright7.png”),
pygame.image.load(“esright8.png”),
pygame.image.load(“esright9.png”),
pygame.image.load(“esright10.png”),
pygame.image.load(“esright11.png”),
pygame.image.load(“esright12.png”),
pygame.image.load(“esright13.png”),
pygame.image.load(“esright14.png”),
pygame.image.load(“esright15.png”),
pygame.image.load(“esright16.png”),
pygame.image.load(“esright17.png”),
]
self.esleft=[pygame.image.load(“esleft1.png”),
pygame.image.load(“esleft1.png”),
pygame.image.load(“esleft2.png”),
pygame.image.load(“esleft3.png”),
pygame.image.load(“esleft4.png”),
pygame.image.load(“esleft5.png”),
pygame.image.load(“esleft6.png”),
pygame.image.load(“esleft7.png”),
pygame.image.load(“esleft8.png”),
pygame.image.load(“esleft9.png”),
pygame.image.load(“esleft10.png”),
pygame.image.load(“esleft11.png”),
pygame.image.load(“esleft12.png”),
pygame.image.load(“esleft13.png”),
pygame.image.load(“esleft14.png”),
pygame.image.load(“esleft15.png”),
pygame.image.load(“esleft16.png”),
pygame.image.load(“esleft17.png”),
]
self.esright=[pygame.transform.scale(image,(image.get_-width()//3,image.get_-height()//3]),用于self.esright中的图像]
self.esleft=[pygame.transform.scale(image,(image.get_-width()//3,image.get_-height()//3)],用于self.esleft中的图像]
自我高度=高度
self.width=宽度
self.anim_索引=0
自我距离=80
自身速度=8
self.vel=3
self.path=[x,end]
self.Walking_索引=0
self.hitbox=(self.x+17,self.y+2,31,57)
self.rect=pygame.rect(x,y,高度,宽度)
#敌人的健康
自我健康=10
self.visible=True
#这使敌人左右移动
def绘图(自,窗口):
self.move()
如果self.Walking_index+1>=33:
self.Walking_索引=0
如果self.vel>0:
blit(self.esright[self.Walking\u index//3],(self.x,self.y))
自行走指数+=1
其他:
blit(self.esleft[self.Walking\u index//3],(self.x,self.y))
自行走指数+=1
#这使敌人左右移动
def移动(自我):
如果自可见:
如果self.vel>0:
如果self.x+self.velself.path[0]:
self.x+=self.vel
其他:
self.vel=self.vel*-1
self.Walking_索引=0
#敌人的杀手锏是健康
pygame.draw.rect(窗口,(255,0,0),(self.hitbox[0],self.hitbox[1]-20,70,10))#新
pygame.draw.rect(窗口,(0255,0),(self.hitbox[0],self.hitbox[1]-20,70-(5*(10-self.health)),10))
self.hitbox=(self.x+47,self.y+31,50,72)
#此部分使敌人不能与玩家一起滚动
def滚动(自、sx、sy):
self.x+=sx
self.y+=sy
self.path[0]+=sx
self.path[1]+=sx
for bullet in bullets: # never mind I fixed it
if bullet.rect.colliderect(enemys1.hitbox):
bullets.pop(bullets.index(bullet))
if enemys1.health > 0:
enemys1.health -= 1
else:
for one in range(len(enemying)-1,-1,-1):
del enemying[one]