Monday, April 7, 2014

Rect

     Look, you may know how to draw circles, but that's not all you can draw in Python. You can also draw square shapes, A.K.A. rectangles. Of course, there is a function for this. This function is called rect (short for rectangle, of course). You define the rect by the area, starting from the top-left corner, in this order: left, top, width, height. This list defines the location and and size. Something like this: rect1=Rect(250, 150, 300, 200). 250 pixels to the right (starting from the top-left of the screen), 150 down, and the size is 300x200 pixels. Take the program that draws the circle and replace line 6 (I'm counting the blank line, so you should be on the line that actually draws the circle) with the code that I stated that draws the rectangle. The program should look like this.

import pygame, sys
pygame.init()
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
pygame.draw.rect(screen, [255, 0, 0], [250, 150, 300, 200] 0)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

     And when you run it, you get a blank screen with a big, red block slapped on.

     Ta-da! That's how you make a rectangle. Hey, I might as well give you the steps to making a rectangle like what I did for doing a shape in general, in case you forget something or the order. Here is the list:

Location - Left-Right
This is the distance the rectangle will be from the left side of the screen.
Location - Top-Bottom
This is the distance the rectangle will be from the top of the screen.
Size - Width
How wide the rectangle will be.
Size - Height
How tall the rectangle will be.

     That's the end of this little bit on rectangles. Until next time!