并非所有代码 - 只是播放器类,如果需要更多细节,请告诉我
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface((32,32))
self.lives = 3
self.image = stand
self.rect = Rect(x, y, 64, 64)
def update(self, up, down, left, right, running, platforms, enemygroup,
attack):
if attack:
self.image = attack
if up: #in air
if self.onGround: self.yvel -= 10 #if player hits the ground
then move up
if self.image == walk1:
self.image = jump1
if self.image == walk_flip:
self.image = jump_flip
if down:
pass #do nothing
if running:
self.xvel = 12
if left:
self.xvel = -8 #differennt movement velocities
self.image = walk_flip
if right:
self.xvel = 8
self.image = walk1
if not self.onGround:
self.yvel += 0.3
if self.yvel > 100: self.yvel = 100 #can't fall faster than 100
pixels
if not(left or right):
self.xvel = 0 #no movement
self.rect.left += self.xvel
self.collide(self.xvel, 0, platforms, enemygroup)
self.rect.top += self.yvel
self.onGround = False;
self.collide(0, self.yvel, platforms, enemygroup)
#self.updatecharacter(walk1)
def collide(self, xvel, yvel, platforms, enemygroup):
for p in platforms:
if pygame.sprite.collide_rect(self, p): #compares rect value of
player and platform
if isinstance(p, ExitBlock):
pygame.event.post(pygame.event.Event(QUIT))
if xvel > 0:
self.rect.right = p.rect.left #right of the playr
collides left of platform
print ("collide right")
if xvel < 0:
self.rect.left = p.rect.right #left of player collide right of platform
print ("collide left")
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
这是我的代码的碰撞部分,用于播放器和与enemys的碰撞。我们的想法是从3到0开始移除生命。当达到0个生命时,介绍会再次播放作为重启。但是,一旦玩家与敌人发生碰撞,所有生命都会立即失去。
for i in enemygroup:
if pygame.sprite.collide_rect(self, i):
boom = self.rect.bottom - i.rect.top
if enemy.destroyed == False:
enemy.destroyed = True
self.lives = self.lives - 1
enemy.destroyed = True
if boom <= 8 and enemy.destroyed == False:
self.yvel = - 8
enemy.destroyed = False
if self.lives == 0:
game_intro()
答案 0 :(得分:0)
您的代码应该已经有效:播放器的生命取决于enemy.destroyed
的状态。当你的玩家与敌人发生碰撞并失去生命后,enemy.destroyed
成为现实。之后,enemy
实例将对玩家的生命没有影响(假设这是敌人击落玩家的唯一方法)。注意:您将enemy.destroyed
重新分配两次至True
,这是不必要的。
enemy.destroyed = False
conditions = True # conditions can include collision with enemy
def statements():
pass # just your statements that you want to run
while True:
if not enemy.destroyed and conditions: # no need for == True/False
enemy.destroyed = True
print("Enemy destroyed!")
statements()
在这里,我有点模仿你的情况。 statements()
函数只运行一次,因为条件只有一次。如果其中一些没有意义或不完全正常,请告诉我,可能包含更多代码(enemy
是什么以及enemy.destroyed
的真正目的是什么)。