|
|
|
@ -0,0 +1,45 @@
|
|
|
|
|
import pygame
|
|
|
|
|
from pygame.sprite import Sprite
|
|
|
|
|
|
|
|
|
|
class Man(Sprite):
|
|
|
|
|
|
|
|
|
|
def __init__(self, ai_settings, screen):
|
|
|
|
|
"""初始化人,并设置其起始位置"""
|
|
|
|
|
super(Man, self).__init__()
|
|
|
|
|
self.screen = screen
|
|
|
|
|
self.ai_settings = ai_settings
|
|
|
|
|
|
|
|
|
|
# 加载人的图像,并获取其rect
|
|
|
|
|
self.image = pygame.image.load('images/man.png')
|
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
|
self.screen_rect = screen.get_rect()
|
|
|
|
|
|
|
|
|
|
# 在屏幕底部中央启动
|
|
|
|
|
self.rect.centerx = self.screen_rect.centerx
|
|
|
|
|
self.rect.bottom = self.screen_rect.bottom
|
|
|
|
|
|
|
|
|
|
# 存储人中心的十进制值
|
|
|
|
|
self.center = float(self.rect.centerx)
|
|
|
|
|
|
|
|
|
|
# 运动
|
|
|
|
|
self.moving_right = False
|
|
|
|
|
self.moving_left = False
|
|
|
|
|
|
|
|
|
|
def center_man(self):
|
|
|
|
|
"""在窗口上使人居中"""
|
|
|
|
|
self.center = self.screen_rect.centerx
|
|
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
"""根据移动标志更新人位置"""
|
|
|
|
|
# 更新人的中心值,而不是rect
|
|
|
|
|
if self.moving_right and self.rect.right < self.screen_rect.right:
|
|
|
|
|
self.center += self.ai_settings.man_speed_factor
|
|
|
|
|
if self.moving_left and self.rect.left > 0:
|
|
|
|
|
self.center -= self.ai_settings.man_speed_factor
|
|
|
|
|
|
|
|
|
|
# 从self.center更新rect对象
|
|
|
|
|
self.rect.centerx = self.center
|
|
|
|
|
|
|
|
|
|
def blitme(self):
|
|
|
|
|
"""在当前位置绘制人"""
|
|
|
|
|
self.screen.blit(self.image, self.rect)
|