def initialize_player(self, x, y): """ initializes a player entity at x, y """ position = Position(x, y) player_rect = pg.Rect(position.x, position.y, 32, 32) collider = Collider( layer=0b00000001, mask=0b11111110, rect=player_rect, callbacks={ # floor 0b00000010: self.player_floor, }, debug=DEBUG ) sprite = Sprite( base_image=self.images[0], anims=[], rect=player_rect, ) physics = Physics( gravity=75, air_friction=0, ground_friction=1, ) player_id = self.create_entity(position) self.add_component(player_id, collider) self.add_component(player_id, sprite) self.add_component(player_id, Input()) self.add_component(player_id, physics)
def make_enemy(game): return GameObject(game, components = [ Sprite('brick.png', size = Vector2(32, 32)), Follow(target=game.player), Wiggle(1), Physics(debug_color = (255,0,0,50)) ])
def create_field(world, pworld, position_x, position_y, field_renderable): """create field without physics with image from paramteres on given position""" field = world.create_entity() field_body = pworld.CreateStaticBody(position=(position_x, position_y)) field_body.userData = field world.add_component(field, Physics(body=field_body)) world.add_component(field, field_renderable)
def create_static_field(world, pworld, position_x, position_y, field_size, field_renderable): """draw static field with image from parameters on given position""" field = world.create_entity() field_body = pworld.CreateStaticBody(position=(position_x, position_y), userData=field) field_body.userData = field field_body.CreatePolygonFixture(box=(field_size / 2, field_size / 2), density=234, friction=0.3) world.add_component(field, Physics(body=field_body)) world.add_component(field, field_renderable)
def _spawn_bomb(self, source, source_physics): bomb = self.world.create_entity() bomb_image = pygame.image.load("assets/tnt_barrel.png") bomb_renderable = Renderable(image=bomb_image) bomb_body = self.world.pworld.CreateDynamicBody( position=source_physics.body.position) bomb_body.userData = bomb bomb_body.CreatePolygonFixture( box=(bomb_renderable.w / self.world.PPM / 2, bomb_renderable.h / self.world.PPM / 2), density=1, friction=0.3) self.world.add_component(bomb, Physics(body=bomb_body)) self.world.add_component(bomb, bomb_renderable) self.world.add_component( bomb, Explodable(explosion_time=time.time() + 1, planter=source))
def create_field_with_physics(world, pworld, position_x, position_y, field_size, field_renderable): """Draw field with physics with image from parameters on given position""" field = world.create_entity() field_body = pworld.CreateDynamicBody(position=(position_x, position_y), userData=field) field_body.userData = field field_body.fixedRotation = True field_body.CreatePolygonFixture(box=(field_size / 2 - 0.1, field_size / 2 - 0.1), density=1000000, friction=6000) world.add_component(field, Physics(body=field_body)) world.add_component(field, field_renderable) world.add_component(field, Health(hp=2)) world.add_component(field, Treasure(chance=0.5))
def process(self): for msg in self.world.msg_bus.get_by_type(MessageType.INTENT): if msg.intent == Intent.PLACE_BONUS: selected = random.choice(self.bonuses) source_pos = self.world.component_for_entity( msg.source, Physics).body.position bonus = self.world.create_entity() bonus_renderable = Renderable(image=selected[1]) bonus_body = self.world.pworld.CreateStaticBody( position=source_pos) bonus_body.userData = bonus bonus_body.fixedRotation = True bonus_body.CreatePolygonFixture( box=(bonus_renderable.w / self.world.PPM / 2, bonus_renderable.h / self.world.PPM / 2), density=1, friction=0.3) self.world.add_component(bonus, Bonus(on_pickup=selected[0])) self.world.add_component(bonus, bonus_renderable) self.world.add_component(bonus, Physics(body=bonus_body))
def _setup_player(world): """Create new player with all its components and add it to world""" player = world.create_entity() shift = 3 * 40 / 2 # change 40 to field_size world.player = player player_image = pygame.image.load("assets/player.png") player_renderable = Renderable(image=player_image) player_body = world.pworld.CreateDynamicBody(position=(shift / PPM, shift / PPM)) player_body.userData = player player_body.fixedRotation = True player_body.CreatePolygonFixture( box=(player_renderable.w / world.PPM / 2 - 0.2, player_renderable.h / world.PPM / 2 - 0.2), density=1, friction=0.3) world.add_component(player, Physics(body=player_body)) world.add_component(player, Velocity(x=0, y=0)) world.add_component(player, player_renderable) world.add_component(player, Bomber(max=3, cooldown=0.5)) world.add_component(player, Health(hp=5)) return player
def draw_door_field(world, pworld, PPM, x, y): """draw wooden wall with hidden door on given position""" field_size = int(40 / PPM) source_pos = (x * field_size + field_size / 2, y * field_size + field_size / 2) bonus = world.create_entity() field_image = pygame.image.load("assets/door.png") bonus_renderable = Renderable(image=field_image) bonus_body = world.pworld.CreateStaticBody(position=source_pos) bonus_body.userData = bonus bonus_body.fixedRotation = True bonus_body.CreatePolygonFixture(box=(0.01, 0.01), density=0, friction=0.3) world.add_component(bonus, Bonus(on_pickup=_start_next_level)) world.add_component(bonus, bonus_renderable) world.add_component(bonus, Physics(body=bonus_body)) field_image = pygame.image.load("assets/fields/woden_big_lines.png") field_renderable = Renderable(image=field_image) field_size = field_renderable.w field_size = int(field_size / PPM) create_field_with_physics(world, pworld, x * field_size + field_size / 2, y * field_size + field_size / 2, field_size, field_renderable)
def _setup_enemy(world): """Create new enemy with all its components and add it to world""" enemy = world.create_entity() world.enemy = enemy enemy_image = pygame.image.load("assets/enemy.png") enemy_renderable = Renderable(image=enemy_image) shift = 3 * 40 / 2 # change 40 to field_size enemy_body = world.pworld.CreateDynamicBody( position=((world.RESOLUTION[0] - shift) / world.PPM, (world.RESOLUTION[1] - 40 - shift) / world.PPM)) enemy_body.userData = enemy enemy_body.fixedRotation = True enemy_body.CreatePolygonFixture( box=(enemy_renderable.w / world.PPM / 2 - 0.2, enemy_renderable.h / world.PPM / 2 - 0.2), density=1, friction=0.3) enemy_body.userData = enemy world.add_component(enemy, Physics(body=enemy_body)) world.add_component(enemy, enemy_renderable) world.add_component(enemy, AIControllable()) world.add_component(enemy, Bomber(max=100, cooldown=4)) world.add_component(enemy, Velocity(x=0, y=0)) return enemy
def make_camera(game): return GameObject(game, components = [ Physics(debug_color = (255, 0, 255, 50), size = Vector2(10,10), is_collidable=False), Follow(target=game.player, follow_type=FollowType.EXPONENTIAL_DECAY), ])
def make_wall(game, pos): return GameObject(game, components = [ Sprite('block.png'), Physics(pos = pos, debug_color = (150,150,255,50), is_dynamic=False) ])
def make_player(game): return GameObject(game, components = [ Sprite('tank_0.png', size = Vector2(32, 32)), WASDControlled(200), Physics(debug_color = (0,255,0,50)) ])