1

I was just trying to make a rectangle in pygame as a variable, but it showed an error. I really don't know how to fix it. Can anyone help me? The code:

import pygame as py

screen = py.display.set_mode((1200, 700))
screen.fill((0, 0, 0))
rocket = py.draw.rect((screen), (225, 225, 225), (600, 350))
py.display.flip()

The error:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Python\Project files\shooter_game.py", line 24, in <module>
    rocket = py.draw.rect((screen), (225, 225, 225), (600, 350))
TypeError: Rect argument is invalid

2 Answers2

0
import pygame
screen=pygame.display.set_mode([1200, 700])
screen.fill([0, 0, 0])
pygame.draw.rect(screen, [255, 0, 0], [50, 50, 90, 90], 0)
pygame.display.flip()
  • 2
    Please don't post only code as an answer, but include an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Mar 23 '20 at 10:52
0

The 3rd parameter of pygame.draw.rect() has to be a tuple with a size of 4. The tuple specifies a rectangle with the position and size:

py.draw.rect((screen), (225, 225, 225), (0, 0, 600, 350))

py.draw.rect((screen), (225, 225, 225), (600, 350, 20, 20)) 

pygame.draw.rect() does not generate a pygame.Surface object. The operation draw a rectangle to a surface and returns a pygame.Rect object.
If you want to generate a rectangular object which can be blit to a surface, then you have to generate a pygame.Surface and fill() it with a uniform color:

rocket = py.Surface((20, 20))
rocket.fill((225, 225, 225))
screen.blit(rocket, (600, 350))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174