[Tutor] Classes v2, thoughts/suggestions please

Paul Melvin paul at assured-networks.co.uk
Sat Jul 12 15:47:16 CEST 2008


Thanks to Alans insights i have come up with a working class for a square,
overcomplicated probably but it lays the groundwork for my polygon class
which will iterate over something to generate an n-sided polygon.
If anyone is interested in actually running/testing this you need to get
hold of pgylet 1.1, http://pyglet.org/
I would welcome any suggestions/improvements etc as my class knowledge is
still somewhat lacking :-) 
I was thinking about using *args to get maybe the colour information in, not
sure if i could do it with the points, is this a good idea/possible?
And how can i go about testing that i get appropriate values during the
testing/building phase (generally speaking), is it lots of print/return
statements and then remove them?
Thanks
paul
#!/usr/bin/env python

from pyglet.gl import *

class Point:
    '''This init contains the x,y co-ordinates
       and the colour/transparency objects
       cr is red, cg is green, cb is blue and ct is transparency
       a value or 1.0, 1.0, 1.0, 1.0 is white/0% transparent'''
    def __init__(self, x=0, y=0, cr=1.0, cg=1.0, cb=1.0, ct=1.0):
        self.x = x
        self.y = y
        self.cr = cr
        self.cg = cg
        self.cb = cb
        self.ct = ct
        self.col = (cr, cg, cb, ct)

class Square:
    '''First version, requires four points, an optional filled field
       and an optional line width field.
       The next version will iterate over a loop to allow the
       construction of polygons'''
    def __init__(self, p1, p2, p3, p4, filled=True, line_width=1):
        self.points = [p1,p2,p3,p4]
        self.filled = filled
        self.line_width = line_width
    def draw(self):
        draw_square(self.points[0].x, self.points[0].y, self.points[0].col,
self.points[1].x,self.points[1].y, self.points[1].col, self.points[2].x,
self.points[2].y, self.points[2].col, self.points[3].x, self.points[3].y,
self.points[3].col, self.filled, self.line_width)
        
def draw_square(x0, y0, col0, x1, y1, col1, x2, y2, col2, x3, y3, col3,
filled=True, line_width=1):
    
    if filled:
        glBegin(GL_QUADS)
    else:
        glLineWidth(line_width)
        glBegin(GL_LINE_LOOP)
    
    glColor4f(col0[0], col0[1], col0[2], col0[3])
    glVertex2i(int(x0), int(y0))
    glColor4f(col1[0], col1[1], col1[2], col1[3])
    glVertex2i(int(x1), int(y1))
    glColor4f(col2[0], col2[1], col2[2], col2[3])
    glVertex2i(int(x2), int(y2))
    glColor4f(col3[0], col3[1], col3[2], col3[3])
    glVertex2i(int(x3), int(y3))
    glEnd()
    if not filled and line_width != 1:  # reset to default
        glLineWidth(1)

if __name__ == '__main__':
    from pyglet import window
    
    window  = window.Window(250, 250)
    s1 = Square(Point(50,50,cg=0.0,cb=0.0), Point(200,50,cr=0.0,cb=0.0),
Point(200,200,cr=0.0,cg=0.0), Point(50,200), filled=False, line_width=2)
    
    @window.event
    def on_draw():
        window.clear()
        s1.draw()
        
    pyglet.app.run()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080712/84cb7fed/attachment-0001.htm>


More information about the Tutor mailing list