def __init__(self, width=400, height=400): self.width, self.height = width, height self.target = TargetArea() self.canon = Canon() self.missiles = [] self.obstacles = [] self.holes = [] self.trajectoryDict = TrajectoryDict() self.showTrajectories = False self.interaction = False self.nt = 1 self.dt = 1.
def initialize(self, world): for i in range(-5, 6): world.addObstacle( Circle(r=15, pos=[ world.width / 2 + i * world.width / 13, 0.36 * world.height ])) world.addObstacle( Circle(r=15, pos=[ world.width / 2 + i * world.width / 13, 0.2 * world.height ])) for i in range(-5, 5): world.addObstacle( Circle(r=15, pos=[ world.width / 2 + world.width / 39 + i * world.width / 13, 0.28 * world.height ])) world.addObstacle( Circle(r=15, pos=[ world.width / 2 + world.width / 39 + i * world.width / 13, 0.44 * world.height ])) world.canon = Canon(r=75, pos=[world.width / 2, world.height], theta=-np.pi / 2) world.target = TargetArea(pos=(world.width / 2, world.height * 0.05), width=world.width * 0.05, height=world.height * 0.05)
def initialize(self, world): world.addObstacle( Rectangle(world.width * 0.625, world.height * 0.33, world.width * 0.75, 50, 0)) world.addObstacle( Rectangle(world.width * 0.375, world.height * 0.66, world.width * 0.75, 50, 0)) world.target = TargetArea(pos=(world.width * 0.85, world.height * 0.15), width=world.width * 0.2, height=world.height * 0.2) world.canon = Canon(r=75, pos=[world.width * 0.5, world.height], theta=-np.pi / 2)
def initialize(self, world): world.addObstacle( Circle(r=150, pos=[world.width / 2, world.height / 2])) world.addObstacle( Circle(r=world.height, pos=[-world.height * np.sqrt(3) / 2, world.height / 2])) world.addObstacle( Circle(r=world.height, pos=[ world.width + world.height * np.sqrt(3) / 2, world.height / 2 ])) world.canon = Canon(r=75, pos=[world.width / 2, world.height], theta=-np.pi / 2) world.target = TargetArea(pos=(world.width / 2, world.height * 0.21), width=world.height * 0.04, height=world.height * 0.04)
def initialize(self, world): world.addObstacle( Line(world.width * 0.75, world.height / 2, world.width * 0.3, np.pi * 0.5)) world.addObstacle( Line(world.width * 0.6, world.height / 4, world.width * 0.3, 0)) world.addObstacle( Rectangle(world.width * 0.2, world.height * 0.42, world.width * 0.45, 30, 0)) world.addObstacle( Rectangle(world.width * 0.2, world.height * 0.58, world.width * 0.5, 35, 0)) world.addObstacle( Circle(r=20, pos=[world.width * 0.43, world.height * 0.42])) world.addObstacle( Circle(r=55, pos=[world.width * 0.46, world.height * 0.58])) world.canon = Canon(r=75, pos=[world.width / 2, world.height], theta=-np.pi / 2) world.target = TargetArea(pos=(world.width * 0.35, world.height * 0.5), width=world.height * 0.1, height=world.height * 0.1)
class World(object): def __init__(self, width=400, height=400): self.width, self.height = width, height self.target = TargetArea() self.canon = Canon() self.missiles = [] self.obstacles = [] self.holes = [] self.trajectoryDict = TrajectoryDict() self.showTrajectories = False self.interaction = False self.nt = 1 self.dt = 1. def addMissile(self): self.missiles += self.canon.getMissile() self.adjust() def addObstacle(self, obstacle): self.obstacles.append(obstacle) def addHole(self, hole): self.holes.append(hole) def adjust(self): if len(self.missiles) == 0: return vmax = np.max([m.vabs for m in self.missiles]) self.nt = 1 * int(np.ceil(vmax)) self.dt = 1. / self.nt def move(self, winCallback, loseCallback): self.adjust() self.trajectoryDict.appendTrajectory(self.missiles) for it in range(self.nt): for m in self.missiles: if (m.x + m.vx * self.dt >= self.width - m.r or m.x + m.vx * self.dt <= m.r): m.vx = -m.vx elif (m.y + m.vy * self.dt <= m.r or m.y + m.vy * self.dt >= self.height - m.r): m.vy = -m.vy for o in self.obstacles: o.reflectMissile(m, self.dt) for h in self.holes: if h.catch(m): loseCallback() if self.target.catch(m): winCallback() if self.interaction: for n in self.missiles: if not n is m: n.reflectMissile(m, self.dt) m.x, m.y = m.x + m.vx * self.dt, m.y + m.vy * self.dt def display(self, screen): if self.showTrajectories == True: self.trajectoryDict.display(screen) self.target.display(screen) for h in self.holes: h.display(screen) for o in self.obstacles: o.display(screen) for m in self.missiles: m.display(screen) self.canon.display(screen)