TypeError: Rect argument is invalid

753 Views Asked by At

Ok, so I'm new to Python, and programming in general. But lately I've been making progress and decided to try out the 2D Minecraft tutorial on http://www.usingpython.com - When I go to run the code, which is incomplete because it is at the begining of the tutorial, it gives me this error: TypeError: Rect argument is invalid

The tutorial says that I should see a window with my multi-colored 2D array, but in stead it is a black window that disappears after a few seconds and shows me said error..

Here's what I have, what is wrong with the "rect"? I believe, if im not missing anything, that it's a perfect copy of the code he used to teach.. Frustrated, help? Thanks!!

import pygame
from pygame.locals import*

#Color link to constants
BLACK = (0, 0, 0)
BROWN = (153, 76, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#Constants, Same as variables but never changing - they're constant!
DIRT = 0
GRASS = 1
WATER = 2
COAL = 3

#Dictionary Linking Resources to colors
colors = {
          DIRT : BROWN,
          GRASS :GREEN,
          WATER : BLUE,
          COAL : BLACK
}

#THE 2D ARRAY
tilemap = [
           [GRASS, COAL, DIRT],
           [WATER, WATER, GRASS],
           [COAL, GRASS, WATER],
           [DIRT, GRASS, COAL],
           [GRASS, WATER, DIRT]
]

#Useful Game Dimensions
TILESIZE = 40
MAPWIDTH = 3
MAPHEIGHT = 5

#Set up the display for PYGAME


pygame.init()
DISPLAYSURF =  pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    for row in range(MAPHEIGHT):
        for column in range(MAPWIDTH):
         pygame.draw.rect(DISPLAYSURF, colors[tilemap[row][column]],     (column*TILESIZE,TILESIZE,TILESIZE))    
    pygame.display.update() 
1

There are 1 best solutions below

1
On

You're missing a pygame.Rect argument here:

pygame.draw.rect(DISPLAYSURF, colors[tilemap[row][column]],     (column*TILESIZE,TILESIZE,TILESIZE)) 

the 3rd argument has to be a pygame.Rect object, you have to build it using 4 integer parameters (there are other possibilities too)

class Rect(builtins.object) | Rect(left, top, width, height) -> Rect

That would be syntaxically correct and as quapka noted an argument got lost from the original link so that should be ok:

pygame.draw.rect(DISPLAYSURF, colors[tilemap[row][column]], pygame.Rect(column*TILESIZE,row*TILESIZE, TILESIZE,TILESIZE)) 

Edit: I just checked and you don't need to pass pygame.Rect explicitly as long as you provide 4 parameters:

pygame.draw.rect(DISPLAYSURF, colors[tilemap[row][column]], (column*TILESIZE,row*TILESIZE, TILESIZE,TILESIZE)) 

You got the error since you only provided 3 parameters by mistake.