Sign in to follow this  
Polantaris

Rect

Recommended Posts

Geeze, I'm asking a lot of questions, I hope that doesn't mean anything bad...anyway: I was looking at the draw class for Pygame, because I wanted to make a rectangle as a buttion...I guess that's a logical way to do it. Since I'm used to working with basic buttons and features already built in, I'm trying to get my own working for the first time. Anyway, even if it's not a good idea to use as a buttion, I want to know how it works anyway. pygame.draw.rect(Surface, color, Rect, width=0): return Rect Surface is the page, correct?...the window = what I mean by the page. color = ? How do I get colors? as in, what are the names for the colors Rect = ? I have no idea what this does, or how to fill it. width is the size of the box, no? I dunno, the info on it was alittle confusing. I think all the Python stuff made by the developers was done very incorrectly, and is very confusing at times...well, most of the time actually. Anyway, I also want to know how I set it's location once its made, since I didn't see an X/Y parameter or anything there. Thanks for all the help in advance ~Polantaris.

Share this post


Link to post
Share on other sites
Hi, I'm also learning Python/PyGame. For color, you would put in values for red, green, and blue. For instance, to make the box white, you'd put in 255, 255, 255. You're right about width, it's for the size of the box, but you should also put height (at least I think).

Share this post


Link to post
Share on other sites
A couple of seconds of Googling gives this:

Quote:

Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.

Keep in mind the Surface.fill - fill Surface with a solid color method works just as well for drawing filled rectangles. In fact the Surface.fill - fill Surface with a solid color can be hardware accelerated on some platforms with both software and hardware display modes.


(You should also be able to get that information by opening up the interpreter and doing 'help(pygame.draw.rect)'.)

The 'Rect' parameter is, therefore, where you specify the location and size of the rectangle, and the width is an optional border thickness (if you leave it alone or set it to zero, you get a filled rectangle; otherwise you get a rectangle-shaped outline). Meanwhile, if you always want filled rectangles, you might want to use Surface.fill() instead:

Quote:

Fill the Surface with a solid color. If no rect argument is given the entire Surface will be filled. The rect argument will limit the fill to a specific area. The fill will also be contained by the Surface clip area.

The color argument can be either an RGB sequence or a mapped color index.

This will return the affected Surface area.


This also tells you how colour arguments are specified in general, as well: as some sequence of RGB values like '(255, 255, 0)' (that is yellow, BTW), or as an index into the colour palette that you are using. The Draw documentation confirms that:

Quote:

Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently. The color argument can also be an integer pixel value that is already mapped to the Surface's pixel format.


The Rect parameter, meanwhile, would be an instance of pygame.Rect.

Share this post


Link to post
Share on other sites
Zahlman, how do you think I came up with this function in the first place? I was wondering how to fill in the Rect parameter, I figured out everything else. The Rect was the only one I was confused about, because the description gives no link what so ever to a referance to Python.rect, therefore I was confused.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this