Friday, February 3, 2017

Ex01. 4 Rectangles

Four rectangles are drawn, 2 in each of 2 rows. We use a Rect object with width of w and height of h. It is copied 4 times and translated to different positions.


They are separated by 2*padx in x-direction and 2*pady in the y-direction.



# Ex01.py 
# 4 Rectangles

import pygame as pg

white = pg.Color('white')
blue = pg.Color('blue')
red = pg.Color('red')
green = pg.Color('green')
purple = pg.Color('purple')

pg.init()

screen = pg.display.set_mode((640, 480))
pg.display.set_caption('Ex01. 4 Rectangles')

w,h = 250,200 # dimensions of each rectangle
# top row - blue, red
# bottom row - green, purple

padx,pady = 20,10

rect = pg.Rect(0,0,w,h)

rect1 = rect.copy()
rect1.bottomright=(320-padx,240-pady)

rect2 = rect.copy()
rect2.bottomleft=(320+padx,240-pady)

rect3 = rect.copy()
rect3.topright=(320-padx,240+pady)

rect4 = rect.copy()
rect4.topleft=(320+padx,240+pady)


screen.fill(white)
pg.draw.rect(screen,blue,rect1)
pg.draw.rect(screen,red,rect2)
pg.draw.rect(screen,green,rect3)
pg.draw.rect(screen,purple,rect4)

pg.display.update()

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
         done = True
            
pg.quit()

This is the output:


No comments:

Post a Comment