示例#1
0
def create(screen_width_px, screen_height_px, tile_size_px):
        joystick_props = controls.get_joystick_properties()
        origin_xy = (0, joystick_props["h"] * screen_height_px)
        state = factory.create(screen_width_px, screen_height_px, tile_size_px, origin_xy)
        controls.add_joystick(state)
        controls.add_buttons(state, (("Fire", None, "right", "big"),))

        n_x = (screen_width_px / 256) + 1
        n_y = (screen_height_px / 256) + 2
        for x in xrange(n_x):
                for y in xrange(n_y):
                        entity_name = star_name(x, y)
                        textures_tuple = ("assets/img/sprites/stars.png",)
                        if DEBUG_MODE:
                                color = (float(x) / n_x, float(y) / n_y, 0)
                                textures_tuple = (("rectangle", 2, 2, color[0], color[1], color[2]),)
                        entities.insert(state, entity_name, {
                                        "*": {
                                                "textures": textures_tuple,
                                                "speed": 0,
                                        },
                                }, ((x * 2), (y * 2), 0)
                                )

        entities.insert(state, ENT_SHIP,
                {
                        "*idle": {
                                "textures": (
                                        "assets/img/sprites/ship_idle/0.png",
                                        "assets/img/sprites/ship_idle/1.png",
                                        ),
                                "speed": 100,
                        },
                        "thrust": {
                                "textures": (
                                        "assets/img/sprites/ship_thrust/0.png",
                                        "assets/img/sprites/ship_thrust/1.png",
                                        ),
                                "speed" : 50,
                        },
                }, (1, 1, 0)
                )

        start_stars_movement(state, None)

        controlled_mover.add(state,
                             ENT_SHIP,
                             "joystick",
                             0.03,
                             (0, 0, 3.25, 1.0),
                             ("*idle", "thrust"),
                             False
                             )

        return state
示例#2
0
def create(screen_width_px, screen_height_px, tile_size_px):
    joystick_props = controls.get_joystick_properties()
    origin_xy = (0, joystick_props["h"] * screen_height_px)
    state = factory.create(screen_width_px, screen_height_px, tile_size_px,
                           origin_xy)
    controls.add_joystick(state)
    controls.add_buttons(state, (("Fire", None, "right", "big"), ))

    n_x = (screen_width_px / 256) + 1
    n_y = (screen_height_px / 256) + 2
    for x in xrange(n_x):
        for y in xrange(n_y):
            entity_name = star_name(x, y)
            textures_tuple = ("assets/img/sprites/stars.png", )
            if DEBUG_MODE:
                color = (float(x) / n_x, float(y) / n_y, 0)
                textures_tuple = (("rectangle", 2, 2, color[0], color[1],
                                   color[2]), )
            entities.insert(state, entity_name, {
                "*": {
                    "textures": textures_tuple,
                    "speed": 0,
                },
            }, ((x * 2), (y * 2), 0))

    entities.insert(
        state, ENT_SHIP, {
            "*idle": {
                "textures": (
                    "assets/img/sprites/ship_idle/0.png",
                    "assets/img/sprites/ship_idle/1.png",
                ),
                "speed":
                100,
            },
            "thrust": {
                "textures": (
                    "assets/img/sprites/ship_thrust/0.png",
                    "assets/img/sprites/ship_thrust/1.png",
                ),
                "speed":
                50,
            },
        }, (1, 1, 0))

    start_stars_movement(state, None)

    controlled_mover.add(state, ENT_SHIP, "joystick", 0.03, (0, 0, 3.25, 1.0),
                         ("*idle", "thrust"), False)

    return state
示例#3
0
def create(screen_width, screen_height, tile_size):
    ENT_PADDLE = "500_paddle"
    ENT_BALL = "500_ball"

    PADDLE_WIDTH = (1.0 / 2.0)
    PADDLE_HEIGHT = (1.0 / 8.0)
    PADDLE_Y = 2.0
    BOTTOM_Y = 1.5
    BORDER_THICKNESS = 2.0
    BORDER_OFFSET = 0.1

    BALL_MOVE_SPEED = 100.0
    BALL_ANIM_SPEED = 3.0
    BALL_START_POS = (1.0, (2.0 + 0.5), 0)
    BALL_VXY = 2.0
    BLOCK_WIDTH = (1.78 / 3.5)
    BLOCK_HEIGHT = (1.0 / 3.5)
    BLOCK_X = 0.1
    BLOCK_Y = 4.5

    state = factory.create(screen_width, screen_height, tile_size)

    tiles.add_tile_def(state, ".", ("assets/img/tiles/grid_double.png", ))
    tiles.set_area(state, [["." for x in xrange(10)] for x in xrange(10)])

    entities_helpers.create_screen_wall(state,
                                        "000_screenbox",
                                        BORDER_THICKNESS,
                                        BORDER_OFFSET,
                                        BOTTOM_Y,
                                        color=(0, 0.15, 1))

    for row in xrange(5):
        for col in xrange(7):
            fix_row = row
            fix_col = col

            block_entity_name = ENT_BLOCK_BASE + "_%d_%d" % (col, row)
            color = (0.5, 0.2, 1) if ((row + col) % 2 == 0) else (0, 1, 0)
            entities.insert(state,
                            block_entity_name, {
                                "*": {
                                    "textures":
                                    (("rectangle", BLOCK_WIDTH, BLOCK_HEIGHT,
                                      color[0], color[1], color[2]), ),
                                },
                            }, (BLOCK_X + (fix_col * BLOCK_WIDTH), BLOCK_Y +
                                (fix_row * BLOCK_HEIGHT), 0),
                            collision=((("rectangle", 0, 0, BLOCK_WIDTH,
                                         BLOCK_HEIGHT), )))

    entities.insert(
        state,
        ENT_PADDLE, {
            "*": {
                "textures":
                (("rectangle", PADDLE_WIDTH, PADDLE_HEIGHT, 1, 1, 1), ),
            },
        }, (1.75, PADDLE_Y, 0),
        collision=((("rectangle", 0, 0, PADDLE_WIDTH, PADDLE_HEIGHT), )))

    entities.insert(
        state,
        ENT_BALL, {
            "*": {
                "textures":
                (("ellipse", PADDLE_HEIGHT, PADDLE_HEIGHT, 1, 1, 1), ),
            },
        },
        BALL_START_POS,
        collision=((("circle", (PADDLE_HEIGHT / 2.0), (PADDLE_HEIGHT / 2.0),
                     (PADDLE_HEIGHT / 2.0)), )))

    physical_mover.add(
        state,
        ENT_BALL,
        1,
        BALL_VXY,
        BALL_VXY,
        0,
        0,
        1,
        1,
        0,
        0,
        1,
        0,
    )

    collisions.set_handler(state, collision_handler)

    controls.add_joystick(state)

    controlled_mover.add(state, ENT_PADDLE, "joystick", 0.1, [
        BORDER_OFFSET, PADDLE_Y,
        (float(screen_width) / tile_size) - PADDLE_WIDTH - BORDER_OFFSET,
        PADDLE_Y
    ])

    return state
示例#4
0
def create(screen_width_px, screen_height_px, tile_size_px):
        # If there is an on-screen joystick
        # (there may not be one needed for non-mobile or other platforms)
        # set the drawing origin to above it (WORK IN PROGRESS, this needs a
        # cleaner solution).
        joystick_props = controls.get_joystick_properties()
        origin_xy = (0, joystick_props["h"] * screen_height_px)

        # Create the actual game state object that stores all information that yapyg needs.
        state = factory.create(screen_width_px, screen_height_px, tile_size_px, origin_xy)

        # Enable the joystick.
        controls.add_joystick(state)

        # Request buttons
        controls.add_buttons(state, (("Fire", on_fire_button, "right", "big"),))

        # Create some tiles to use for our game area. Individual tiles are referred to by strings.
        # Each tile can be composed by layering several images over each other
        if not DEBUG_MODE:
                floor_tile = "assets/img/tiles/dirtysquares.png"
        else:
                floor_tile = "assets/img/tiles/grid_double.png"

        tiles.add_tile_def(state, "x", (floor_tile,))
        if DEBUG_MODE:
                TILE_SIZE = 1.0
                tiles.add_tile_def(state, "#", ("assets/img/tiles/plain.png",), (("rectangle", 0, 0, TILE_SIZE, TILE_SIZE),))

        # Special wall tile import helper.
        if not DEBUG_MODE:
                tiles_helpers.load_walls(state, "", floor_tile, "assets/img/tiles/bricks_walls.png")
        else:
                tiles_helpers.load_walls(state, "", floor_tile, "assets/img/tiles/grid_quad_walls.png")

        # The tile map is made as a list of lists.
        tiles.set_area(state,
                [ ['<', '-', '-', '>', '<', '-', '-', '-', '-', '>'],
                  ['(', '.', ',', ')', '(', '.', '_', '_', ',', ')'],
                  ['(', ')', '(', ')', '(', ')', '<', '>', '(', ')'],
                  ['[', ']', '(', ')', '(', ')', '(', ')', '[', ']'],
                  ['<', '-', ';', ')', '(', ')', '(', ':', '-', '>'],
                  ['(', 'x', 'x', ')', '(', ')', '(', '.', '_', ']'],
                  ['(', 'x', 'x', ':', ';', ')', '(', ':', '-', '>'],
                  ['(', '.', '_', '_', ',', ')', '(', 'x', 'x', ')'],
                  ['(', ':', '-', '-', ';', ':', ';', 'x', 'x', ')'],
                  ['[', '_', '_', '_', '_', '_', '_', '_', '_', ']']]
                  )

        collisions.set_handler(state, collision_handler)

        # Font is 16x32 px size
        text.load_font(state, FONT_NAME, "assets/img/fonts/%s.png" % FONT_NAME, 16, 32)

        user.set_data(state, USERDATA_SCORE, 0)
        text.create_text_entity(state, ENT_TEXT_SCORE, get_score_text(state), FONT_NAME, (0, 0, 0), screen_relative=True)

        user.set_data(state, USERDATA_HEALTH, 100)
        text.create_text_entity(state, ENT_TEXT_HEALTH, get_health_text(state), FONT_NAME,
                (screen_width_px / tile_size_px / 2.0, 0, 0), screen_relative=True)

        # We create the ENT_MAN entity which has 2 different sprite representations: standing and walking.
        # The idle sprite is the default sprite, since it starts with an asterisk (*).

        # The animation of the sprites is defined by a list of images that will be played in order,
        # then repeated, where the playback speed is defined individually for the sprite as well.

        # We define the starting coordinates (in "map coordinates", which are relative to tile size, not pixels!), here [1,1],
        # the rotation amount of the entity (0 here) and an offset for drawing the sprite to the actual position,
        # here [0.25, 0.25].
        MAN_SIZE = 1.0 / 2.0 # 64 px = 128 px tile / 2
        MAN_RADIUS = MAN_SIZE / 2.0
        MAN_OFFSET = -MAN_RADIUS # entity position defines the center of the sprite (sprite drawn left+lower of position)
        if not DEBUG_MODE:
                man_idle_textures = (
                                     "assets/img/sprites/man_idle/0.png",
                                     "assets/img/sprites/man_idle/1.png",
                                     "assets/img/sprites/man_idle/2.png",
                                     "assets/img/sprites/man_idle/3.png",
                                     "assets/img/sprites/man_idle/1.png",
                                     "assets/img/sprites/man_idle/0.png",
                                     "assets/img/sprites/man_idle/3.png",
                                     "assets/img/sprites/man_idle/2.png",
                                     )
                man_walk_textures = (
                                     "assets/img/sprites/man_walk/1.png",
                                     "assets/img/sprites/man_walk/2.png",
                                     "assets/img/sprites/man_walk/3.png",
                                     )
        else:
                man_idle_textures = (
                                     "assets/img/sprites/half_ball.png",
                                     )
                man_walk_textures = (
                                     "assets/img/sprites/half_ball.png",
                                     )

        entities.insert(state,
                        ENT_MAN,
                        {
                         "*idle": {
                                   "textures": man_idle_textures,
                                   "speed": 333.0,
                                   },
                         "walk": {
                                  "textures": man_walk_textures,
                                  "speed" : 150.0,
                                  },
                         },
                        (1.0, 1.0, 0),
                        (MAN_OFFSET, MAN_OFFSET),
                        collision=(("circle", MAN_RADIUS, MAN_RADIUS, MAN_RADIUS,),),
                        )

        # A pile of coins
        COINS_SIZE = 1.0 / 2.0 # 64 px = 128 px tile / 2
        COINS_RADIUS = COINS_SIZE / 2.0
        COINS_OFFSET = -COINS_RADIUS
        if not DEBUG_MODE:
                coin_textures = ("assets/img/sprites/coins/0.png",
                                 "assets/img/sprites/coins/1.png",
                                 "assets/img/sprites/coins/2.png",
                                 "assets/img/sprites/coins/1.png",
                                 )
        else:
                coin_textures = ("assets/img/sprites/half_ball_2.png",)
        entities.insert(state,
                        "%s_1" % (ENT_PREFIX_COINS),
                        {
                         "*": {
                               "textures": coin_textures,
                               "speed": 150.0,
                               },
                         },
                        (1.0, 7.0, 0),
                        (COINS_OFFSET, COINS_OFFSET),
                        collision=(("circle", COINS_RADIUS, COINS_RADIUS, COINS_RADIUS,),),
                        )

        GHOST_SIZE = 1.0
        GHOST_RADIUS = GHOST_SIZE / 2.0
        GHOST_OFFSET = -(GHOST_RADIUS)
        if not DEBUG_MODE:
                ghost_textures = (
                                  "assets/img/sprites/ghost/1.png",
                                  "assets/img/sprites/ghost/2.png",
                                  "assets/img/sprites/ghost/3.png",
                                  "assets/img/sprites/ghost/2.png",
                                )
        else:
                ghost_textures = ("assets/img/sprites/full_ball.png",)

        entities.insert(state,
                        ENT_GHOST,
                        {
                         "*": {
                                   "textures": ghost_textures,
                                   "speed": 200.0,
                                   },
                         },
                        (1.0, 3.0, 0),
                        (GHOST_OFFSET, GHOST_OFFSET), # sprite appears lower and more left of actual entity position
                        collision=(("circle", GHOST_RADIUS, GHOST_RADIUS, GHOST_RADIUS,),),
                        )

        # We add a mover that will translate joystick movement to moving the man around the area.
        # This particular mover needs the source of control, a factor for the strength of the movement,
        # the allowed range for the movement, what sprites to use for idle and moving state and if to
        # rotate the entity to the movement direction.
        controlled_mover.add(state,
                             ENT_MAN,
                             "joystick",
                             0.03,
                             (0, 0, 10.0, 10.0),
                             ("*idle", "walk"),
                             True
                             )

        view.set_viewer(state, relative_viewer.create(state, ENT_MAN))

        start_ghost_movement(state, None)

        # The state object is finished.
        return state
示例#5
0
def create(screen_width, screen_height, tile_size):
        ENT_PADDLE = "500_paddle"
        ENT_BALL = "500_ball"

        PADDLE_WIDTH = (1.0 / 2.0)
        PADDLE_HEIGHT = (1.0 / 8.0)
        PADDLE_Y = 2.0
        BOTTOM_Y = 1.5
        BORDER_THICKNESS = 2.0
        BORDER_OFFSET = 0.1

        BALL_MOVE_SPEED = 100.0
        BALL_ANIM_SPEED = 3.0
        BALL_START_POS = (1.0, (2.0 + 0.5), 0)
        BALL_VXY = 2.0
        BLOCK_WIDTH = (1.78 / 3.5)
        BLOCK_HEIGHT = (1.0 / 3.5)
        BLOCK_X = 0.1
        BLOCK_Y = 4.5

        state = factory.create(screen_width, screen_height, tile_size)

        tiles.add_tile_def(state, ".", ("assets/img/tiles/grid_double.png",))
        tiles.set_area(state, [["." for x in xrange(10)] for x in xrange(10)])

        entities_helpers.create_screen_wall(state, "000_screenbox", BORDER_THICKNESS, BORDER_OFFSET, BOTTOM_Y, color=(0, 0.15, 1))

        for row in xrange(5):
                for col in xrange(7):
                        fix_row = row
                        fix_col = col

                        block_entity_name = ENT_BLOCK_BASE + "_%d_%d" % (col, row)
                        color = (0.5, 0.2, 1) if ((row + col) % 2 == 0) else (0, 1, 0)
                        entities.insert(state,
                                block_entity_name,
                                {
                                        "*": {
                                                "textures": (("rectangle", BLOCK_WIDTH, BLOCK_HEIGHT, color[0], color[1], color[2]),),
                                        },
                                },
                                (BLOCK_X + (fix_col * BLOCK_WIDTH), BLOCK_Y + (fix_row * BLOCK_HEIGHT), 0),
                                collision=((("rectangle", 0, 0, BLOCK_WIDTH, BLOCK_HEIGHT),)))

        entities.insert(state,
                ENT_PADDLE,
                {
                        "*": {
                                "textures": (("rectangle", PADDLE_WIDTH, PADDLE_HEIGHT, 1, 1, 1),),
                        },
                },
                (1.75, PADDLE_Y, 0),
                collision=((("rectangle", 0, 0, PADDLE_WIDTH, PADDLE_HEIGHT),)))

        entities.insert(state,
                ENT_BALL,
                {
                        "*": {
                                "textures": (("ellipse", PADDLE_HEIGHT, PADDLE_HEIGHT, 1, 1, 1),),
                        },
                },
                BALL_START_POS,
                collision=(((
                        "circle",
                        (PADDLE_HEIGHT / 2.0),
                        (PADDLE_HEIGHT / 2.0),
                        (PADDLE_HEIGHT / 2.0)),))
                )

        physical_mover.add(state,
                ENT_BALL,
                1,
                BALL_VXY,
                BALL_VXY,
                0,
                0,
                1,
                1,
                0,
                0,
                1,
                0,
                )

        collisions.set_handler(state, collision_handler)

        controls.add_joystick(state)

        controlled_mover.add(state,
                ENT_PADDLE,
                "joystick",
                0.1,
                [BORDER_OFFSET, PADDLE_Y,
                (float(screen_width) / tile_size) - PADDLE_WIDTH - BORDER_OFFSET, PADDLE_Y]
                )

        return state