Exemplo n.º 1
0
class Game(DirectObject):
  def __init__(self):
    self.title_text = self.info((-1.32, 0.96), title)

    base.disableMouse()
    #base.camLens.setNear(0.0001)

    self.create_world()
    base.taskMgr.add(self.step, "physics-step")

    self.stars = Sprite("media/textures/asteroids/stars", depth = 100, scale = 146, transparency = False)

    self.static_body = pymunk.Body(pymunk.inf, pymunk.inf)
    self.static_lines = [pymunk.Segment(self.static_body, Vec2d(-SCREEN_X, -SCREEN_Y), Vec2d(-SCREEN_X, SCREEN_Y), 0.0),
                         pymunk.Segment(self.static_body, Vec2d(-SCREEN_X, -SCREEN_Y), Vec2d(SCREEN_X, -SCREEN_Y), 0.0),
                         pymunk.Segment(self.static_body, Vec2d(SCREEN_X, -SCREEN_Y), Vec2d(SCREEN_X, SCREEN_Y), 0.0),
                         pymunk.Segment(self.static_body, Vec2d(-SCREEN_X, SCREEN_Y), Vec2d(SCREEN_X, SCREEN_Y), 0.0)
                         ]
    for l in self.static_lines:
      l.friction = 0.3
      l.elasticity = 0.6
    self.world.add_static(self.static_lines)


    self.ship = Player()
    self.ship.activate()
    body, shape = self.ship.physical_presence()
    self.world.add(body, shape)

    # Initialize Cameras
    self.god_camera = GodCamera()

    # Activate Free Lopok
    self.god_camera.activate()

    self.accept("r", self.snapshot)
    self.accept("escape", sys.exit)

  def create_world(self):
    pymunk.init_pymunk()
    self.world = pymunk.Space()
    self.world._space.contents.elasticIterations = 10
    self.world.gravity = Vec2d(0.0, 0.0)

  def step(self, task):
    steps = 10
    for x in range(steps):
      self.world.step(1/60.0/steps)
    return Task.cont

  """take a snapshot"""
  def snapshot(self):
    base.screenshot("snapshot")

  """info"""
  def info(self, pos, msg):
    self.font = base.loader.loadFont(APP_PATH + 'media/fonts/OCR.otf')
    return OnscreenText(font = self.font, text = msg, style = 1, fg = (1, 1, 1, 1),
                        pos = pos, align = TextNode.ALeft, scale = .035,
                        mayChange = True)