Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. import math
  2.  
  3. MAX_ANG_VELOCITY = 1
  4. MAX_THRUST = 2
  5. TIME_STEP = 0.5
  6. GRAVITY = 1
  7. FIELD_WIDTH = 30
  8.  
  9. rockets = []
  10. missiles = []
  11.  
  12.  
  13. class Rocket(object):
  14.     """Base class for rockets.
  15.  
  16.    Subclass this for your custom rocket objects.
  17.    """
  18.  
  19.     def __init__(self, x):
  20.         """Create a new rocket.
  21.  
  22.        Rockets are created on the ground, facing up.
  23.        """
  24.         self.x = x
  25.         self.y = 0
  26.         self.rotation = 0
  27.         self.velocity = [0, 0]
  28.         self.fuel = 10
  29.         self.exploded = False
  30.  
  31.     def update(self, nearby):
  32.         """Override this with custom update function."""
  33.         return 0, 0
  34.  
  35.     def distance(self, rocket):
  36.         """Distance from this rocket to another one."""
  37.         return math.sqrt((self.x - rocket.x)**2 + (self.y - rocket.y)**2)
  38.  
  39.     def explode(self):
  40.         """Mark as exploded.
  41.  
  42.        Exploded rockets are cleaned up at the end of the round.
  43.        """
  44.         self.exploded = True
  45.  
  46.  
  47. class Missile(object):
  48.     """Missiles are fired by rockets and blow up other nearby rockets."""
  49.  
  50.     def __init__(self, x, y, vx, vy, parent):
  51.         """Create a new rocket.
  52.  
  53.        Parent is stored to avoid blowing up the rocket that launched it.
  54.        """
  55.         self.x = x
  56.         self.y = y
  57.         self.velocity = [vx, vy]
  58.         self.parent = parent
  59.         self.exploded = False
  60.  
  61.     def update(self, rockets):
  62.         """Check for other nearby rockets and blow up any found."""
  63.         self.x = (self.x + self.velocity[0] * TIME_STEP) % FIELD_WIDTH
  64.         self.y += self.velocity[1] * TIME_STEP
  65.         for rocket in rockets:
  66.             if rocket.distance(self) < 1 and rocket is not self.parent:
  67.                 rocket.explode()
  68.                 self.explode()
  69.  
  70.     def explode(self):
  71.         """Mark as exploded.
  72.  
  73.        Exploded missiles are cleaned up at the end of the round.
  74.        """
  75.         self.exploded = True
  76.  
  77.  
  78. def turn():
  79.     """Handle a single turn."""
  80.     global rockets
  81.     global missiles
  82.     for rocket in rockets:
  83.         try:
  84.             nearby = [i for i in rockets if i.distance(rocket) < 10]
  85.             angular_velocity, thrust, firing = rocket.update(nearby)
  86.             if MAX_ANG_VELOCITY < angular_velocity or angular_velocity < -MAX_ANG_VELOCITY:
  87.                 angular_velocity = 0
  88.             if MAX_THRUST < thrust or thrust < 0 or rocket.fuel <= 0:
  89.                 thrust = 0
  90.         except:
  91.             angular_velocity, thrust, firing = 0, 0, False
  92.         rocket.rotation += angular_velocity * TIME_STEP
  93.         rocket.velocity[0] += thrust * math.sin(rocket.rotation)
  94.         rocket.velocity[1] += thrust * math.cos(rocket.rotation)
  95.         rocket.x = (rocket.x + rocket.velocity[0] * TIME_STEP) % FIELD_WIDTH
  96.         rocket.y += rocket.velocity[1] * TIME_STEP
  97.         # require soft landings
  98.         if rocket.y <= 0 and math.sqrt(rocket.velocity[0]**2 + rocket.velocity[1]**2) > 4:
  99.             rocket.explode()
  100.         elif rocket.y <= 0:
  101.             rocket.y = 0
  102.             rocket.velocity = [0, 0]
  103.             rocket.rotation = 0
  104.             rocket.fuel = 10
  105.         rocket.fuel -= rocket.thrust * TIME_STEP
  106.         if firing:
  107.             missile = Missile(
  108.                 rocket.x,
  109.                 rocket.y,
  110.                 rocket.velocity[0] + 5 * math.sin(rocket.rotation),
  111.                 rocket.velocity[1] + 5 * math.cos(rocket.rotation),
  112.                 rocket)
  113.             missiles.append(missile)
  114.     for missile in missiles:
  115.         missile.update(rockets)
  116.  
  117.     # Clean up any debris
  118.     rockets = [rocket for rocket in rockets if not rocket.exploded]
  119.     missiles = [missile for missile in missiles if not missile.exploded]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement