def __init__(self, screen): Globals.game = self self.screen = screen # get everything set up self.clock = pygame.time.Clock() self.canvas = olpcgames.ACTIVITY.canvas self.font = pygame.font.Font(None, 40) # font object self.infoBarRect = pygame.Rect(0, 0, self.screen.get_size()[0], 40) self.allSprites = pygame.sprite.Group() self.toolList = {} for c in tools.allTools: self.toolList[c.name] = c() self.componentList = {} self.saveQuantities = {} for c in components.allComponents: self.componentList[c.name] = c self.saveQuantities[c.name] = 0 self.currentTool = self.toolList["RampTool"] self.currentSaveComp = None self.world = physics.PhysicsSimulator() self.world.run = False self.levelData = {} self.userComponents = [] self.levelComponents = [] # load all the sounds for use throughout the game self.allSounds = {"ouch.wav": loadSound("sounds/ouch.wav")}
def test_reshape_early(): w = World() w.sim = physics.PhysicsSimulator( timestep = None ) a = QuadBlock(32) b = QuadBlock(35) thing = physics.Thing( w, BlockStructure(a).create_collision_shape(), 1.0, 1.0 ) assert thing.abstract_shape.area() == a.area() thing.reshape( BlockStructure(b).create_collision_shape() ) assert thing.abstract_shape.area() == b.area() w.sim.perform_removals_and_additions()
def setup_game(self, player_ship_data=None): self.sim = physics.PhysicsSimulator(timestep=None) # self.player = create_ship_thing( self, self.main_layer, (500,500), shape = "small", hp = 5 ) if not player_ship_data: self.player = Ship.load_file("current_garage_ship.yaml", self) else: self.player = Ship.load_data(player_ship_data, self) self.player.position = (300, 300) self.player.invulnerable = False self.enemy = create_ship_thing(self, (500, 500), shape="small", hp=10) self.enemy.invulnerable = False self.enemy.body.angular_velocity_limit = degrees_to_radians(144 * 2) self.enemy2 = create_ship_thing(self, (0, 500), shape="small", hp=10) self.enemy2.invulnerable = False self.enemy2.body.angular_velocity_limit = degrees_to_radians(144 * 2) self.enemy.angle_degrees = random.random() * 360.0 self.enemy2.angle_degrees = random.random() * 360.0 self.batch = cocos.batch.BatchNode() self.main_layer.cocos_layer.add(self.batch) self.physics_objects = [] self.things = [] self.psys_managed_things = [] for i in range(200): cols = "red", "purple", "grey", "blue", "green", "yellow" sq = create_square_thing(self, None, (100, 0), None) sq.position = (random.random() - 0.5) * 4000, (random.random() - 0.5) * 4000 sq.angle_radians = random.random() * math.pi * 2 sq.mylabel = sq.position sq.velocity = (300, 10) kw = {} name = "polygon_normals.4.generated" kw["size"] = Vec2d(106.6666666, 106.6666666) kw["texture_coordinates"] = self.atlas.texcoords(name) kw["texture_size"] = self.atlas.texsize(name) z = random.random() * 6.28 r = 0.1 + random.random() * 10.0 pos = r * math.cos(z), r * math.sin(z) # need to translate from pixel space # [0,self. # to # [-1.3333,1.3333] x [-1,1] p = sq.position p = p + self.main_layer.cocos_layer.position p = Vec2d(p) / Vec2d(self.window.width, self.window.height) p = (p * 2) - Vec2d(1, 1) p = p * Vec2d(self.window.width / float(self.window.height), 1) kw["position"] = p kw["angle"] = sq.angle_radians kw["colour"] = 1.0, 0.5, 0.5, 1.0 index = self.object_psys.add(**kw) self.psys_managed_things.append((sq, index)) self.things.append(sq) def draw_psys(): # CMSDTv # T = translate by self.main_layer.cocos_layer.position # # M = v -> v - (1,1) # C = v -> v * (w/h, 1) w = float(self.window.width) h = float(self.window.height) x, y = self.main_layer.cocos_layer.position mat = (2.0 / w, 0.0, 0.0, (2 * x / w - 1), 0.0, 2.0 / h, 0.0, (2 * y / h - 1), 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0) self.object_psys.set_transformation_matrix(mat) glEnable(GL_SCISSOR_TEST) glScissor(0, 0, self.window.width + 1 - self.hud_width, self.window.height) self.object_psys.draw() glDisable(GL_SCISSOR_TEST) s = 0.3 sx, sy = 500 + self.sim._t * 100, 200 sx, sy = self.window.width - self.hud_width * 0.5, self.window.height - self.hud_width * 0.5 mat = (0.0, s * 2.0 / w, 0.0, (2 * sx / w - 1), s * 2.0 / h, 0, 0.0, (2 * sy / h - 1), 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0) self.hud_psys.set_transformation_matrix(mat) self.hud_psys.draw() graphics.Layer(self.scene, cocos_layer=graphics.FunctionCocosLayer(draw_psys)) self.sim.space.add_collision_handler(physics.CollisionTypes["main"], physics.CollisionTypes["bullet"], self.collide_general_with_bullet)
def __init__(self, window, continuation_with_ship=None, **kwargs): super(GarageWorld, self).__init__(**kwargs) self.window = window self.scene = graphics.Scene(self.window) self.sim = physics.PhysicsSimulator(timestep=None) self.input_layer = gameinput.CocosInputLayer() layer = graphics.Layer(self.scene, cocos.layer.ColorLayer(128, 0, 128, 255)) graphics.Layer(self.scene, cocos_layer=graphics.FunctionCocosLayer(self.draw_psys)) layer.cocos_layer.position = 0, 0 graphics.Layer(self.scene, self.input_layer) self.atlas = Atlas("atlas.generated") self.object_psys = bsgl.System( texture_id=self.atlas.sheet.get_texture().id) self.input_layer.mouse_motion_hooks.append(self.on_mouse_motion) self.main_layer = graphics.Layer(self.scene) self.root_node = cocos.cocosnode.CocosNode() self.main_layer.cocos_layer.add(self.root_node) self.continuation_with_ship = continuation_with_ship root = PolygonBlock.load_file("blocks/poly5.yaml") self.root_node.scale = 1 # for i in range(7): # a = s.attach((0,i), blocks.QuadBlock(32), 0) # b = s.attach((a,2), blocks.QuadBlock(32), 0) # c = s.attach((b,2), blocks.QuadBlock(32), 0) # d = s.attach((c,1), blocks.QuadBlock(32), 3) # self.sprite_structure = None self.garage_ship = None self.block_shapes = map( lambda n: (lambda: PolygonBlock.load_file("blocks/poly{}.yaml".format(n))), (3, 4, 5, 6, 8)) self.block_decorators = [ decorate_block_armour, decorate_block_battery, decorate_block_generator ] self.block_gun_mounter = decorate_block_with_guns_within self.block_engine_mounter = decorate_block_with_engines_within self.current_block_shape = self.block_shapes[0] self.current_block_decorator = self.block_decorators[0] self.restart_block_structure() self.mouse_index = None self.current_rotation = 0.0 self.current_position = (0, 0) self.set_current_block() self.input_layer.mouse_scroll_hooks.add_anonymous_hook( self.on_mouse_scroll) if self.continuation_with_ship: self.input_layer.set_key_press_hook(key.ENTER, self.on_continue_with_ship) self.input_layer.set_key_press_hook(key.SPACE, self.on_place_block) self.input_layer.set_key_press_hook(key.UP, self.on_next_shape) self.input_layer.set_key_press_hook(key.DOWN, self.on_previous_shape) self.input_layer.set_key_press_hook(key.RIGHT, self.on_next_decorator) self.input_layer.set_key_press_hook(key.LEFT, self.on_previous_decorator) self.input_layer.set_key_press_hook(key.R, self.on_restart_with_current) self.input_layer.set_key_press_hook(key.P, self.on_save_ship) self.input_layer.set_key_press_hook(key.L, self.on_load_ship) self.input_layer.mouse_press_hooks[mouse.RIGHT] = self.on_next_shape self.input_layer.mouse_press_hooks[mouse.LEFT] = self.on_place_block self.refresh_garage_ship() self.physics.add_anonymous_hook(self.sim.tick) self.idle_time = 0.0 self.currently_idle = False