#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Having problems with classes and drawing


    I have used simple pygame functions to draw a house and am now trying to create a class called House; I can get it to draw the house, but without the windows - I am have problems passing x and y parameters to draw the windows for example. Is it possible to do this within a class ? Thanks in advance. Here is my program :

    import pygame, sys
    #from pygame.locals import *
    from pygame import*
    pygame.init()

    # set up the window
    DISPLAYSURF = pygame.display.set_mode((400, 300))
    display.set_caption('Drawing')

    # set up the colors
    BROWN = (51,25,0)
    WHITE = (255, 255, 255)
    BLUE = (0, 0, 255)
    BLACK = (0,0,0)
    YELLOW = (255,255,0)
    RED = (255,0,0)
    PINK = (255,105,180)
    # draw on the surface object
    DISPLAYSURF.fill(WHITE)


    class House:
    def __init__(self,x,y,):
    self.x = x
    self.y = y

    def window(x,y):
    #window
    pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, 40, 40))
    #frame
    pygame.draw.line (DISPLAYSURF, BLACK, [x,y+20], [x+40,y+20], 1)
    pygame.draw.line (DISPLAYSURF, BLACK, [x+20,y], [x+20,y+40], 1)
    #left curtains
    pygame.draw.polygon(DISPLAYSURF, PINK, [[x, y], [x+7, 170], [x+3, y+20],[x+7, y+40],[x, y+40]], 0)
    #right curtains
    pygame.draw.polygon(DISPLAYSURF, PINK, [[x+40, y], [x+33, y], [x+37, y+20],[x+33, y+40],[x+40, y+40]], 0)
    def roof(self):
    pygame.draw.polygon(DISPLAYSURF, BROWN, [[100, 150], [200, 50], [300, 150]], 0)
    def walls(self):
    pygame.draw.rect(DISPLAYSURF, RED, (100, 150, 200, 300))
    def door(self):
    pygame.draw.rect(DISPLAYSURF, YELLOW, (170, 220, 60, 80))

    # pygame.draw.circle(surface, color, center_point, radius, width)
    # run the game loop
    while True:
    for event in pygame.event.get():
    if event.type == QUIT:
    pygame.quit()
    sys.exit()
    newhouse = House(150,150)
    newhouse.roof()

    newhouse.walls()

    newhouse.door()
    newhouse.window()
    #leftwindow(120,170)
    #rightwindow(240,170)
    #window(120,170)
    #window(240,170)
    #leftframe()
    #leftleftcurtains()
    #leftrightcurtains()
    #rightwindow()
    #rightframe()
    #completewindow(120,170)

    pygame.display.update()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    The point of making the house a class is so you can use it generically. You should be able to pop down multiple houses just by creating a house instance at different coordinates.

    Here is something to start with. You can further expand it so you can change colors by passing in arguments. You could also make the different elements of the house classes too so that you could make different looking houses.

    python Code:
    import sys
    import pygame as pg
     
     
    BROWN = (51,25,0)
    WHITE = (255, 255, 255)
    BLUE  = (0, 0, 255)
    BLACK = (0,0,0)
    YELLOW = (255,255,0)
    RED = (255,0,0)
    PINK = (255,105,180)
     
     
    class House(object):
        def __init__(self, x, y):
            self.image = self.make_image()
            self.rect = self.image.get_rect(topleft=(x,y))
     
        def make_image(self):
            image = pg.Surface((200,300)).convert_alpha()
            image.fill((0,0,0,0))
            self.draw_roof(image)
            self.draw_walls(image)
            self.draw_window(image)
            self.draw_door(image)
            return image
     
        def draw_walls(self,surface,color=RED):
            surface.fill(color,(0,100,200,200))
     
        def draw_roof(self,surface,color=BROWN):
            points = ([0,100], [100,0], [200,100])
            pg.draw.polygon(surface, BROWN, points)
     
        def draw_window(self,surface):
            surface.fill(WHITE,(50,100,40,40))
            pg.draw.line (surface, BLACK, [50,120], [90,120],1)
            pg.draw.line (surface, BLACK, [70,100], [70,140], 1)
            curtain_points_1 = ([50,100], [57,120], [53,120],[57,140],[50,140])
            pg.draw.polygon(surface, PINK, curtain_points_1, 0)
            curtain_points_2 = ([90,100], [83,100], [87,120],[83,140],[90,140])
            pg.draw.polygon(surface, PINK, curtain_points_2, 0)
     
        def draw_door(self, surface, color=YELLOW):
            surface.fill(color, (60,220,70,80))
     
        def draw(self,surface):
            surface.blit(self.image, self.rect)
     
     
    def main():
        pg.init()
        screen = pg.display.set_mode((500, 500))
        pg.display.set_caption('Drawing')
        clock = pg.time.Clock()
        screen.fill(WHITE)
        one_house = House(20,200)
        another_house = House(300,200)
        while True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    pg.quit()
                    sys.exit()
            one_house.draw(screen)
            another_house.draw(screen)
            clock.tick(60.0)
            pg.display.update()
     
     
    if __name__ == "__main__":
        main()

    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    Thanks for your detailed reply. I can see that I have a lot to learn about using classes; but I can see from your code, where I have been going wrong. This has helped me immensely. Hope to help others when I have become experienced with Python

IMN logo majestic logo threadwatch logo seochat tools logo