当前位置: 首页>>代码示例>>Python>>正文


Python Animation.draw方法代码示例

本文整理汇总了Python中animation.Animation.draw方法的典型用法代码示例。如果您正苦于以下问题:Python Animation.draw方法的具体用法?Python Animation.draw怎么用?Python Animation.draw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在animation.Animation的用法示例。


在下文中一共展示了Animation.draw方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from animation import Animation [as 别名]
# 或者: from animation.Animation import draw [as 别名]
class Character:

  def __init__(self, screen_w, screen_h, unit):
    self.unit = unit
    self.create_animations()
    image_rect = self.animation.get_current_frame().image.get_rect()
    self.center = image_rect.width/2
    self.position = [(screen_w - image_rect.width)/2, screen_h - image_rect.height]
    size_reduction = 10*unit
    self.collision_rect = pygame.Rect(image_rect.left + size_reduction, 
      image_rect.top + size_reduction,
      image_rect.width - 2*size_reduction, 
      image_rect.height - 4*size_reduction)
    self.screen_w = screen_w
    self.direction = [0, 0]
    
  def create_animations(self):
    image = pygame.image.load("assets/character.png")
    
    # create the frames for the "walking left" animation
    frames = [
      Frame(image = image, position = [0, 4*self.unit], duration = 4),
      Frame(image = image, position = [0, 0],           duration = 4),
    ]

    # and with that create the "walking left" animation
    self.animation = Animation(frames)
    
  def input(self, event):
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        self.direction[0] = -1
      elif event.key == pygame.K_RIGHT:
        self.direction[0] = 1
    if event.type == pygame.KEYUP:
      if (event.key == pygame.K_LEFT and self.direction[0] == -1) or\
        (event.key == pygame.K_RIGHT and self.direction[0] == 1):
        self.direction[0] = 0
  
  def update(self):
    if self.direction[0] != 0:
      self.animation.update()
    self.position[0] = (self.position[0] + self.direction[0]*8*self.unit + self.center)\
      %self.screen_w - self.center
  
  def draw(self, on_surface):
    self.animation.draw(on_surface, self.position)
   
  def collides_with(self, other):
    return self.get_absolute_rect().colliderect(other.get_absolute_rect())
  
  def get_absolute_rect(self):
    return self.collision_rect.move(self.position)
开发者ID:codelurker,项目名称:pygame-tutorial,代码行数:55,代码来源:character.py


注:本文中的animation.Animation.draw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。