while True:
filename = argv[file_idx % len(argv)]
doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
- actions = [act([], 0.5) for act in doc.actions]
- source = bulletml.Bullet(
- x=150, y=150, target=target, actions=actions, rank=0.5)
+ source = bulletml.Bullet.FromDocument(
+ doc, x=150, y=150, target=target, rank=0.5)
active = set([source])
source.vanished = True
from bulletml import Bullet, BulletML
doc = Bulletml.BulletML.FromDocument(open("test.xml", "rU"))
+ player = ... # On your own here, but it needs x and y fields.
rank = 0.5 # Player difficulty, 0 to 1
- params = [] # Initial variable settings, usually empty
- actions = [a(params, rank) for a in doc.actions]
- bullet = Bullet(x, y, target=player, actions=actions, rank=rank)
+ bullet = Bullet.FromDocument(doc, x, y, target=player, rank=rank)
bullets = [bullet]
...
for bullet in bullets:
bullets.extend(bullet.step())
-
...
For drawing, you're on your own, but Bullet instances have a number of
self._actions = [Action(self, None, action, params, rank)
for action, params in actions]
+ @classmethod
+ def FromDocument(cls, doc, x=0, y=0, direction=0, speed=0, target=None,
+ params=(), rank=0.5, Action=Action):
+ """Construct a new Bullet from a loaded BulletML document."""
+ actions = [a(params, rank) for a in doc.actions]
+ return cls(x, y, direction, speed, target, actions, rank, Action)
+
def __repr__(self):
return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
"actions=%r, target=%r, vanished=%r)") % (
@property
def aim(self):
- """Angle to the target, in radians."""
- if self.target is None:
- return self.direction
+ """Angle to the target, in radians.
+
+ If the target does not exist or cannot be found, return 0.
+ """
+ try:
+ target_x = self.target.x
+ target_y = self.target.y
+ except AttributeError:
+ return 0
else:
- return math.atan2(self.target.x - self.x, self.y - self.target.y)
+ return math.atan2(target_x - self.x, self.y - target_y)
@property
def finished(self):