Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- MAX_ANG_VELOCITY = 1
- MAX_THRUST = 2
- TIME_STEP = 0.5
- GRAVITY = 1
- FIELD_WIDTH = 30
- rockets = []
- missiles = []
- class Rocket(object):
- """Base class for rockets.
- Subclass this for your custom rocket objects.
- """
- def __init__(self, x):
- """Create a new rocket.
- Rockets are created on the ground, facing up.
- """
- self.x = x
- self.y = 0
- self.rotation = 0
- self.velocity = [0, 0]
- self.fuel = 10
- self.exploded = False
- def update(self, nearby):
- """Override this with custom update function."""
- return 0, 0
- def distance(self, rocket):
- """Distance from this rocket to another one."""
- return math.sqrt((self.x - rocket.x)**2 + (self.y - rocket.y)**2)
- def explode(self):
- """Mark as exploded.
- Exploded rockets are cleaned up at the end of the round.
- """
- self.exploded = True
- class Missile(object):
- """Missiles are fired by rockets and blow up other nearby rockets."""
- def __init__(self, x, y, vx, vy, parent):
- """Create a new rocket.
- Parent is stored to avoid blowing up the rocket that launched it.
- """
- self.x = x
- self.y = y
- self.velocity = [vx, vy]
- self.parent = parent
- self.exploded = False
- def update(self, rockets):
- """Check for other nearby rockets and blow up any found."""
- self.x = (self.x + self.velocity[0] * TIME_STEP) % FIELD_WIDTH
- self.y += self.velocity[1] * TIME_STEP
- for rocket in rockets:
- if rocket.distance(self) < 1 and rocket is not self.parent:
- rocket.explode()
- self.explode()
- def explode(self):
- """Mark as exploded.
- Exploded missiles are cleaned up at the end of the round.
- """
- self.exploded = True
- def turn():
- """Handle a single turn."""
- global rockets
- global missiles
- for rocket in rockets:
- try:
- nearby = [i for i in rockets if i.distance(rocket) < 10]
- angular_velocity, thrust, firing = rocket.update(nearby)
- if MAX_ANG_VELOCITY < angular_velocity or angular_velocity < -MAX_ANG_VELOCITY:
- angular_velocity = 0
- if MAX_THRUST < thrust or thrust < 0 or rocket.fuel <= 0:
- thrust = 0
- except:
- angular_velocity, thrust, firing = 0, 0, False
- rocket.rotation += angular_velocity * TIME_STEP
- rocket.velocity[0] += thrust * math.sin(rocket.rotation)
- rocket.velocity[1] += thrust * math.cos(rocket.rotation)
- rocket.x = (rocket.x + rocket.velocity[0] * TIME_STEP) % FIELD_WIDTH
- rocket.y += rocket.velocity[1] * TIME_STEP
- # require soft landings
- if rocket.y <= 0 and math.sqrt(rocket.velocity[0]**2 + rocket.velocity[1]**2) > 4:
- rocket.explode()
- elif rocket.y <= 0:
- rocket.y = 0
- rocket.velocity = [0, 0]
- rocket.rotation = 0
- rocket.fuel = 10
- rocket.fuel -= rocket.thrust * TIME_STEP
- if firing:
- missile = Missile(
- rocket.x,
- rocket.y,
- rocket.velocity[0] + 5 * math.sin(rocket.rotation),
- rocket.velocity[1] + 5 * math.cos(rocket.rotation),
- rocket)
- missiles.append(missile)
- for missile in missiles:
- missile.update(rockets)
- # Clean up any debris
- rockets = [rocket for rocket in rockets if not rocket.exploded]
- missiles = [missile for missile in missiles if not missile.exploded]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement