Skip to content

Grid Using Sprites v2 example: margin=0 works differently in arcade==2.5.7 vs arcade==2.6.* #959

@LJArendse

Description

@LJArendse

Documentation/Bug request:

I recently updated to the latest release version of arcade (arcade==2.6.2) from arcade==2.5.7. I am busy building a grid based game and noticed a change in the way the grid is 'rendering' from version arcade==2.5.7. In short, in the Grid Using Sprites v2 example setting margin=0 works differently in arcade==2.5.7 vs arcade==2.6.* (see https://api.arcade.academy/en/latest/examples/array_backed_grid_sprites_2.html#array-backed-grid-sprites-2).

When setting margin=0 in the Grid Using Sprites v2 example, in arcade==2.5.7, the grid is rendered smoothly as expected (i.e. there is no margin between the grid blocks/cells). However, when setting margin=0 in the Grid Using Sprites v2 example, in arcade==2.6.2, the grid is rendered with a margin (i.e. there is a margin between the grid blocks/cells).

The question I have is: How can I modify the code in Grid Using Sprites v2 example such that I can render a smooth grid with no margin, in arcade==2.6.2?

Actual behavior in arcade==2.6.2:

Setting the MARGIN=0 in Grid Using Sprites v2 example results in:

Screenshot 2021-09-19 at 11 00 15

Expected behavior in arcade==2.6.2:

Setting the MARGIN=0 in Grid Using Sprites v2 example should, ideally for me, result in:

Screenshot 2021-09-19 at 10 58 13

Steps to reproduce/example code:

  1. Install arcade==2.5.7
  2. Download the code from Grid Using Sprites v2 example (https://api.arcade.academy/en/latest/examples/array_backed_grid_sprites_2.html#array-backed-grid-sprites-2)
  3. Set the MARGIN=0, in line 24
MARGIN = 0
  1. Run the Grid Using Sprites v2 example code with the margin set to 0, using arcade==2.5.7. You should notice that the margin is not being rendered (we get a smooth grid world).
  2. Install arcade==2.6.2
  3. Set the MARGIN=0, in line 24 (i.e. ensure you have done step 3)
MARGIN = 0
  1. Run the Grid Using Sprites v2 example code with the margin set to 0, using arcade==2.6.2. You should notice that the margin is now being rendered regardless of margin value.

or you can run the below code respectively in arcade==2.5.7 and arcade==2.6.2, which already has the margin=0:

"""
Array Backed Grid Shown By Sprites

Show how to use a two-dimensional list/array to back the display of a
grid on-screen.

This version makes a grid of sprites instead of numbers

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.array_backed_grid_sprites_2
"""
import arcade

# Set how many rows and columns we will have
ROW_COUNT = 15
COLUMN_COUNT = 15

# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 30
HEIGHT = 30

# This sets the margin between each cell
# and on the edges of the screen.
MARGIN = 0

# Do the math to figure out our screen dimensions
SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
SCREEN_TITLE = "Array Backed Grid Buffered Example"


class MyGame(arcade.Window):
    """
    Main application class.
    """

    def __init__(self, width, height, title):
        """
        Set up the application.
        """
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.BLACK)

        # One dimensional list of all sprites in the two-dimensional sprite list
        self.grid_sprite_list = arcade.SpriteList()

        # This will be a two-dimensional grid of sprites to mirror the two
        # dimensional grid of numbers. This points to the SAME sprites that are
        # in grid_sprite_list, just in a 2d manner.
        self.grid_sprites = []

        # Create a list of solid-color sprites to represent each grid location
        for row in range(ROW_COUNT):
            self.grid_sprites.append([])
            for column in range(COLUMN_COUNT):
                x = column * (WIDTH + MARGIN) + (WIDTH / 2 + MARGIN)
                y = row * (HEIGHT + MARGIN) + (HEIGHT / 2 + MARGIN)
                sprite = arcade.SpriteSolidColor(WIDTH, HEIGHT, arcade.color.WHITE)
                sprite.center_x = x
                sprite.center_y = y
                self.grid_sprite_list.append(sprite)
                self.grid_sprites[row].append(sprite)

    def on_draw(self):
        """
        Render the screen.
        """

        # This command has to happen before we start drawing
        arcade.start_render()

        self.grid_sprite_list.draw()

    def on_mouse_press(self, x, y, button, modifiers):
        """
        Called when the user presses a mouse button.
        """

        # Change the x/y screen coordinates to grid coordinates
        column = int(x // (WIDTH + MARGIN))
        row = int(y // (HEIGHT + MARGIN))

        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")

        # Make sure we are on-grid. It is possible to click in the upper right
        # corner in the margin and go to a grid location that doesn't exist
        if row < ROW_COUNT and column < COLUMN_COUNT:

            # Flip the location between 1 and 0.
            if self.grid_sprites[row][column].color == arcade.color.WHITE:
                self.grid_sprites[row][column].color = arcade.color.GREEN
            else:
                self.grid_sprites[row][column].color = arcade.color.WHITE


def main():
    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions