Saturday, February 4, 2017

Ex03. Polygon

Two five sided polygons are drawn, one on top of another Each gets five coordinates in a for loop.


Since we did not specify a width, it is filled with the color, either cmap[0]=red, or cmap[2]=blue for the two polygons.



# Ex03.py 
# Polygon

import pygame as pg
from math import sin,cos,radians

colors = ['red','green','blue','cyan',
    'purple','brown','magenta','orange']
cmap = tuple(map(pg.Color,colors))


pg.init()

screen = pg.display.set_mode((640, 480))
pg.display.set_caption('Ex03. Polygon')

center = 300,240
radius = 200

screen.fill(pg.Color("white"))

poly1 = []

for i in range(5):
 angDeg = 45*i
 angRad = radians(angDeg)
 poly1.append((center[0]+radius*cos(angRad),
       center[1]+radius*sin(angRad) ) )

poly2 = []

for i in range(4,9):
 angDeg = 45*i
 angRad = radians(angDeg)
 poly2.append((center[0]+radius*cos(angRad),
       center[1]+radius*sin(angRad) ) )

pg.draw.polygon(screen,cmap[0],poly1)
pg.draw.polygon(screen,cmap[2],poly2)

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:


Friday, February 3, 2017

Ex02. 180 Radial Lines

180 lines are draw from 300, 240 in different directions.


All the lines have a length of 200. They have a width of 5 and have the 8 colors defined in the colors list. It is converted to pygame colors in cmap tuple.



# Ex02.py 
# 180 Radial Lines

import pygame as pg
from math import sin,cos,radians

colors = ['red','green','blue','cyan',
    'purple','brown','magenta','orange']
cmap = tuple(map(pg.Color,colors))


pg.init()

screen = pg.display.set_mode((640, 480))
pg.display.set_caption('Ex02. 180 Radial Lines')

center = 300,240
radius = 200

screen.fill(pg.Color("white"))

for i in range(180):
 angDeg = 2*i
 angRad = radians(angDeg)
 pos = ( center[0]+radius*cos(angRad),
   center[1]+radius*sin(angRad)  )
 pg.draw.line(screen,cmap[i%8],center,pos,5)

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:


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: