0
\$\begingroup\$
screen=pygame.display.set_mode((500, 700))
Shape=['.....',
       '..O..',
       '.OOO.',
       '.....',
       '.....']
ShapeProperites={'x': 250,
                 'y': 50,
                 'Color':Red}

How can I convert this array in Pygame to a shape drawn on a screen with the properties from the dictionary? The list is meant to represent a tetromino. The 0's should be drawn as squares on the screen.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I'm not exactly sure what you want your characters to represent, or where you want X and Y to be located, but something like the following should get you started.

First of all, you want to declare the color: Red = (255, 0, 0)

Then, in your draw loop you want to render the shape with the following code:

x = ShapeProperties["x"]
y = ShapeProperties["y"]
for line in Shape:
    for character in line:
        if character == "O":
            pygame.draw.rect(screen, ShapeProperties["Color"], pygame.Rect(x, y, Size, Size))
        x += Size
    y += Size
    x = ShapeProperties["x"]

It starts at the position you've specified in the properties. It then iterates over the lines in the shape, and then over the characters that denote the actual shape. Each character increments X by the Size, which you can set to be whatever size you want your shape blocks to be. Once then end of a line is reached, it resets X and increments Y by the size, moving onto the next row.

As an optimization you might want to consider rendering this into a surface of a Shape class, and just blitting that surface each frame, instead of re-rendering the whole shape, but this should be enough to get you going.

\$\endgroup\$
3
  • \$\begingroup\$ When I run the code, this error is generated. Sorry, I am new to programming so please do help. Traceback (most recent call last): File "....PROJECT\Shapes.py", line 60, in <module> y = ShapeProperties['y'] NameError: name 'ShapeProperties' is not defined \$\endgroup\$
    – noob96
    Feb 11, 2017 at 19:02
  • \$\begingroup\$ Yes, that's a very basic variable naming error, as the error message states. I fixed a typo in your code. \$\endgroup\$
    – user35344
    Feb 11, 2017 at 19:03
  • \$\begingroup\$ You need to rename your ShapeProperites to ShapeProperties. \$\endgroup\$
    – user35344
    Feb 11, 2017 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .