通常游戏中的角色都有所谓的生命值,而且头顶上会有一个血条显示。生命值无非就是一个属性而已,很容易在Player.py类中增加,头顶上的血条其实就是绘制二个矩形,叠加在一起。
以上节的Player.py为例:
import pygame
# 主角
class Player(object):
def __init__(self, x, y, width, height, img_base_path):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = 5
self.left = False
self.right = True
self.isJump = False
self.walkCount = 0
self.t = 10
self.speed = 5
# 生命值
self.life = 10
self.char = pygame.image.load(img_base_path + 'standing.png')
self.walkRight = [pygame.image.load(img_base_path + 'actor/R1.png'),
pygame.image.load(img_base_path + 'actor/R2.png'),
pygame.image.load(img_base_path + 'actor/R3.png'),
pygame.image.load(img_base_path + 'actor/R4.png'),
pygame.image.load(img_base_path + 'actor/R5.png'),
pygame.image.load(img_base_path + 'actor/R6.png'),
pygame.image.load(img_base_path + 'actor/R7.png'),
pygame.image.load(img_base_path + 'actor/R8.png'),
pygame.image.load(img_base_path + 'actor/R9.png')]
self.walkLeft = [pygame.image.load(img_base_path + 'actor/L1.png'),
pygame.image.load(img_base_path + 'actor/L2.png'),
pygame.image.load(img_base_path + 'actor/L3.png'),
pygame.image.load(img_base_path + 'actor/L4.png'),
pygame.image.load(img_base_path + 'actor/L5.png'),
pygame.image.load(img_base_p