Attribute handling

June 27, 2010

One new aspect I’ve already mentioned in another post is that common drawing options like width, aa, rounded corners or color (for some pens) are stored as attributes of pen objects to keep parameter lists of shape methods short. But now, you need a few lines each time you want to draw a new shape with other options:

pen = SolidPen(color=pygame2.Color(20, 210, 50))
pen.width = 5
pen.rect(screen, any_rect)
pen.color = pygame2.Color(120, 250, 250)
pen.width = 3
pen.rect(screen, any_other_rect)

Ok, this can be really annoying. Therefore I added a way to use these options (which are still object attributes) also as keyword parameters:

pen = SolidPen(color=pygame2.Color(20, 210, 50))
pen.width = 5
print pen.width # output: 5
pen.rect.screen, any_rect) # draws rect with width=5
pen.rect(screen, any_other_rect, width=3) # draws rect with width=3
print pen.width # output: 5
# set pen.width = 1 (the actual attribute) and then draw the rect
pen.rect(screen, a_third_rect, width=1, apply=True)
print pen.width # output: 1

Thus, you can use keyword parameters to change options, but you don’t have to.  To draw some shapes all in one color you set it once and then don’t need to repeat yourself over and over again.


One comment

  1. really good idea to cascade the attributes!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: