-1

The title is pretty self explanatory. I'm trying to find out how to make a rectangle with arrays for this tetris game in python.

Here's the code:

screen = pygame.display.set_mode((400,800))

#Rectangle Variables
x = 200
y = 0
width = 50
height = 50
thickness = 5
speed = 1
#Colors
red = (255,0,0)
white = (255,255, 255)
green = (0,255,0)
blue = (0,0,255)
while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit (); sys.exit ();
    #These lines ^ make the user able to exit out of the game window
    y = y+1
    pygame.draw.rect((screen) , red, (x,y,width,height), thickness)
    pygame.display.update() 
furas
  • 134,197
  • 12
  • 106
  • 148

2 Answers2

0

If all you want to do is add rectangles to an array you could just do:

rectangles = []
while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit (); sys.exit ();
    #These lines ^ make the user able to exit out of the game window
    y = y+1
    rectangles.append(pygame.draw.rect((screen) , red, (x,y,width,height), thickness))
    pygame.display.update() 
0

If you have list with positions then use for loop to draw it.

Here position is in pixels

# --- constants --- (UPPER_CASE_NAMES)

WIDTH = 50
HEIGHT = 50 
RED = (255,0,0)

# --- main ---

rectangles_XY = [ (0, 0), (50, 0), (100, 0) ]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # PLEASE, don't put all in one line 
            # it makes code less readable.
            pygame.quit()
            sys.exit ()

    for x, y in rectangles_XY:
        pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0)

    pygame.display.update() 

Here position is in cell positon (column, row)

# --- constants --- (UPPER_CASE_NAMES)

WIDTH = 50
HEIGHT = 50 
RED = (255,0,0)

# --- main ---

rectangles = [ (0, 0), (1, 0), (2, 0) ]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # PLEASE, don't put all in one line 
            # it makes code less readable.
            pygame.quit()
            sys.exit ()

    for column, row in rectangles:
        x = column * WIDTH
        y = row * HEIGHT
        pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0)

    pygame.display.update() 
furas
  • 134,197
  • 12
  • 106
  • 148