0

My Code

Hello, I am currently doing an assignment for class and can't seem to be getting my code to work.

The objective of this assignment is to repeatedly draw rectangles on the screen that are each smaller than the last one by 10 pixels and are also 10 pixels apart. It is supposed to repeat until the squares are too small.

Yes, I know that pygame is not imported properly. This is just a recreation on my personal computer, my actual code is at school.

I believe I did something wrong with drawing the rectangle as the error says 'rect is not defined'. I'm new to coding so this is probably a really simple mistake that is easily fixed.

Any feedback would be appreciated. Thank you!

Here is the code in text format:

import pygame
from pygame.locals import *

def drawSquares(window):
    # Create variables to keep track of size and position
    size = 100
    position = 0
    # Use a loop to draw squares until the size is too small
    while (size>0):
        pygame.draw.rect(window,(0,0,255),position, position, size, size)
        size -= 10
        position += size+10
Ayumo
  • 1
  • 2
    What's the exact error, and a [mcve] of the problem? I can't see this code causing that error. – Carcigenicate Mar 07 '21 at 14:56
  • @Rabbid76 Are you sure that's a good close? I wouldn't expect what appears to be a name error from providing too many arguments. – Carcigenicate Mar 07 '21 at 15:13
  • @Carcigenicate I don't understand what do you mean? The issue is about drawing rectangles. Do you think the question should reopened? – Rabbid76 Mar 07 '21 at 15:14
  • @Rabbid76 You could be right about the close. I just meant I wouldn't expect an "rect is not defined" error from a cause like that. They could have the error wrong too ¯\\_(ツ)_/¯. – Carcigenicate Mar 07 '21 at 15:33
  • @Carcigenicate I agree, the title doesn't hit the nail on the head. However, the 4 answers to the "duplicate" cover the issue. May be there is another question which can be added to the duplicates. – Rabbid76 Mar 07 '21 at 15:56

1 Answers1

0

The last argument of pygame.draw.rect is a tuple with 4 components (x, y, width, height):

pygame.draw.rect(window,(0,0,255), position, position, size, size)

pygame.draw.rect(window,(0,0,255), (position, position, size, size))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174