class TheX(object): def __init__(self, rect, game): self.game = game self.resetRect = pygame.Rect(rect) self.rect = rect self.body = self.game.world.add.sensorBox(rect, self.onHit) self.body.GetUserData()['type'] = "TheX" img = loadSVG("x.svg") self.sprite = SVGSprite(svg=img, size=(rect.width, rect.height)) self.sprite.rect.center = rect.center self.sprite.add(game.componentSprites) def onHit(self, c): bodies = (c.shape1.GetBody().GetUserData(), c.shape2.GetBody().GetUserData()) for d in bodies: if d.has_key('type'): if d['type'] == "TheO": self.game.world.run_physics = False break def update(self): c = self.body.GetPosition() self.rect.center = (c.x * self.game.world.ppm, self.game.screen.get_size()[1] - (c.y * self.game.world.ppm)) self.sprite.rect.center = self.rect.center
class FanTool(Tool): # button properties toolTip = "Fan" icon = "fan" name = "fan" order = 30 toolBar = "Insert" # tool properties caption = "Fans push objects up!" def __init__(self): self.component = components.Fan img = loadSVG("fan.svg") self.sprite = SVGSprite(svg=img, size=(100, 0)) def handleEvent(self, event): if event.type == MOUSEBUTTONDOWN: if event.button == 1: if self.component.amountLeft > 0: Globals.game.userComponents.append( self.component(event.pos)) self.component.amountLeft -= 1 def draw(self): if self.component.amountLeft > 0: if not self.sprite.alive(): self.sprite.add(Globals.game.allSprites) self.sprite.rect.center = pygame.mouse.get_pos() def cancel(self): self.sprite.kill()
class TheO(object): def __init__(self, rect, game): self.game = game self.rect = rect self.resetRect = pygame.Rect(rect) self.body = self.game.world.add.ball(rect.center, self.rect.width / 2, dynamic=True, density=2.0) self.body.GetUserData()['visible'] = False self.body.GetUserData()['type'] = "TheO" img = loadSVG("o.svg") self.sprite = SVGSprite(svg=img, size=(rect.width, rect.height)) self.sprite.rect.center = rect.center self.sprite.add(game.componentSprites) def update(self): c = self.body.GetPosition() self.rect.center = (c.x * self.game.world.ppm, self.game.screen.get_size()[1] - (c.y * self.game.world.ppm)) self.sprite.rect.center = self.rect.center
class Fan(Component): name = "Fan" def __init__(self, pt): Component.__init__(self, pt) center = toWorld(pt) self.body = Globals.game.world.addBody(center, self) boxDef = box2d.b2PolygonDef() width = 50 / PPM height = 13 / PPM boxDef.SetAsBox(width, height) boxDef.density = 0 boxDef.restitution = 0.15 boxDef.friction = 0.5 self.baseShape = self.body.CreateShape(boxDef) self.baseShape = self.baseShape.asPolygon() # sprite representation img = loadSVG("fan.svg") self.sprite = SVGSprite(svg=img, size=(100, 0)) self.sprite.add(Globals.game.allSprites) boxDef = box2d.b2PolygonDef() boxDef.SetAsBox(50 / PPM, 200 / PPM, box2d.b2Vec2(0, 200 / PPM), 0) boxDef.density = 0 boxDef.isSensor = True self.sensorShape = self.body.CreateShape(boxDef) self.sensorShape = self.sensorShape.asPolygon() self.body.SetMassFromShapes() Globals.game.world.contactListener.connect(self.body, self.handleContact) #self.channel = None #self.active = False def draw(self): self.sprite.rect.center = toScreen(self.body.GetWorldCenter()) """ if self.active: if not self.channel: self.channel = Globals.game.allSounds['fan.wav'].play(-1) if not self.channel.get_busy(): self.channel.play(Globals.game.allSounds['fan.wav'],-1) else: if self.channel: self.channel.stop() """ def handleContact(self, contactType, point): #self.active = False if contactType == "Add" or contactType == "Persist": if point.shape1.GetBody().GetUserData( )['id'] == self.body.GetUserData()['id']: body = point.shape2.GetBody() else: body = point.shape1.GetBody() force = (1 - ((point.position.y - self.body.GetWorldCenter().y) / (400 / PPM))) * 200 force = box2d.b2Vec2(0.0, force) body.ApplyForce(force, point.position) #self.active = True def moveBy(self, vec): self.args[0] = movePt(self.args[0], vec) self.reset() def reset(self): pt = toWorld(self.args[0]) self.body.SetXForm(box2d.b2Vec2(*pt), 0) stopBody(self.body) def kill(self): ud = self.body.GetUserData() returnID(ud['id']) Globals.game.world.contactListener.disconnect(self.body) Globals.game.world.world.DestroyBody(self.body) self.sprite.kill() return 1
class X(Component): name = "X" def __init__(self, pt): Component.__init__(self, pt) center = toWorld(pt) self.body = Globals.game.world.addBody(center, self) boxDef = box2d.b2PolygonDef() width = 50 / PPM height = 50 / PPM boxDef.SetAsBox(width, height) boxDef.density = 0 boxDef.restitution = 0.15 boxDef.friction = 0.5 self.shape = self.body.CreateShape(boxDef) self.shape = self.shape.asPolygon() self.body.SetMassFromShapes() # sprite representation img = loadSVG("x.svg") self.sprite = SVGSprite(svg=img, size=(100, 100)) self.sprite.add(Globals.game.allSprites) #Add the callback Globals.game.world.contactListener.connect(self.body, self.handleContact) def draw(self): self.sprite.rect.center = toScreen(self.body.GetWorldCenter()) def handleContact(self, contactType, point): if contactType == "Add": b1 = point.shape1.GetBody().GetUserData() b2 = point.shape2.GetBody().GetUserData() o = None if b1['parent'].__class__ == O: o = b1['parent'] elif b2['parent'].__class__ == O: o = b2['parent'] if o: Globals.game.world.run = False x, y = toScreen(self.body.GetWorldCenter()) o.destination = (x, y - 75) Globals.game.allSounds['ouch.wav'].play() def moveBy(self, vec): pt = self.args[0] x, y = pt mx, my = vec x += mx y += my self.args = [(x, y)] self.reset() def reset(self): pt = toWorld(self.args[0]) self.body.SetXForm(box2d.b2Vec2(*pt), 0) stopBody(self.body) def kill(self): ud = self.body.GetUserData() Globals.game.world.contactListener.disconnect(self.body) returnID(ud['id']) Globals.game.world.world.DestroyBody(self.body) self.sprite.kill() return None