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


Python Ship.get_center_x方法代码示例

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


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

示例1: GameWidget

# 需要导入模块: from ship import Ship [as 别名]
# 或者: from ship.Ship import get_center_x [as 别名]
class GameWidget(Widget):

  def __init__(self, root_widget, **kw):
    super(GameWidget, self).__init__(**kw)
    self.root_widget = root_widget
    self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
    self._keyboard.bind(on_key_down=self._on_keyboard_down)
    self._keyboard.bind(on_key_up=self._on_keyboard_up)
    self.ship = Ship()
    with self.canvas.before:
      self.rect_bg1 = Image(source='img/background-blue.png', allow_stretch=True, keep_ratio=False)
      self.rect_bg2 = Image(source='img/background-blue.png', allow_stretch=True, keep_ratio=False) # , allow_stretch=True, keep_ratio=False
    self.bind(size=self._update_rect)
    self.bind(pos=self._update_rect)
    self.counter = 0
    self.level = 0
    self.drones = []
    self.saucers = []
    Clock.schedule_interval(self.update_game, 1./60)
    self.sfx_laser = SoundPygame(source='sfx/laser.wav')
    self.sfx_explo = SoundPygame(source='sfx/explosion.wav')
    # self.sfx_laser = SoundLoader.load('sfx/laser.wav')
    # self.sfx_explo = SoundLoader.load('sfx/explosion.wav')
    self.img_explo = []
    with self.canvas.after:
      Color(.8, .2, .2, 1)
      self.lifebar = Rectangle()
    self.score = 0
    self.accel = 0
    self._initialized = False
    
  def _update_rect(self, *args):
    # self.rect_bg1.pos = self.width/2 - self.height/2, 0
    # self.rect_bg1.size = self.height, self.height * 1
    # self.rect_bg2.pos = self.width/2 - self.height/2, self.height
    # self.rect_bg2.size = self.height, self.height * 1
    self.rect_bg1.pos = self.pos
    self.rect_bg1.size = self.size
    self.rect_bg2.pos = 0, self.height
    self.rect_bg2.size = self.size
    if not self._initialized:
      self.initialize()
    
  def initialize(self):
    self.add_widget(self.ship)
    self.ship.pos= self.center_x - self.ship.width/2, self.y
    self.update_health()
    self._initialized = True

  def _keyboard_closed(self):
    self._keyboard.unbind(on_key_down=self._on_keyboard_down)
    self._keyboard.unbind(on_key_up=self._on_keyboard_up)
    self._keyboard = None

  def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
    if keycode[1] == 'escape':
      self.root_widget.close_app()
    if keycode[1] == 'left':
      self.ship.steer_x(-20)
    elif keycode[1] == 'right':
      self.ship.steer_x(20)
    elif keycode[1] == 'up':
      self.ship.steer_y(20)
    elif keycode[1] == 'down':
      self.ship.steer_y(-20)
    elif keycode[1] == 'f' and not self.ship.is_shielded and self.ship.cooldown == 0:
      self.ship.cooldown = 25
      missile = Projectile('missile', 0, 10, 'human')
      missile.score_bonus = 5
      missile.size = 75, 75
      missile.x = self.ship.get_center_x() - missile.width / 2
      missile.y = self.ship.y + self.ship.height
      self.add_widget(missile)
    elif keycode[1] == 's' and not self.ship.is_shielded and self.ship.cooldown == 0:
      # if self.sfx_laser.state == 'play':
        # self.sfx_laser.stop()
      self.ship.cooldown = 5   
      self.sfx_laser.play()
      laser1 = Projectile('laser', 0, 20, 'human')
      laser1.x = self.ship.x + self.ship.width * .35
      laser1.y = self.ship.y + self.ship.height / 10
      self.add_widget(laser1)
      laser2 = Projectile('laser', 0, 20, 'human')
      laser2.x = self.ship.x - self.ship.width * .35
      laser2.y = self.ship.y + self.ship.height / 10
      self.add_widget(laser2)
    elif keycode[1] == 'd':
      self.ship.toggle_shield()
    # elif keycode[1] == 'q':
      # self.ship.shield('green')
    return True
    
  def _on_keyboard_up(self, keyboard, keycode):
    if keycode[1] == 'left':
      self.ship.steer_x(0)
    elif keycode[1] == 'right':
      self.ship.steer_x(0)
    elif keycode[1] == 'up':
      self.ship.steer_y(0)
    elif keycode[1] == 'down':
#.........这里部分代码省略.........
开发者ID:victor-rene,项目名称:ld-shooter,代码行数:103,代码来源:gamewidget.py


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