Andranik Manvelyan
Andranik Manvelyan

Reputation: 65

Initializing class attribute Python

I have a following program with Pygame:

import pygame
import time
pygame.init()
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((600,800))
gameExit = False
x=0
y=0
w=25
h=25
class Shape:
    square = pygame.draw.rect(gameDisplay,color,[x,y,w,h])
    def __init__(self,color,x,y,w,h):
        self.color = color
        self.x = x
        self.y = y
        self.w = w
        self.h = h
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        shape = Shape(white,x,y,w,h)
        pygame.display.update()
        clock.tick(60)
pygame.quit()
quit()

I want to initialize square attribute of Shape class with init,but I get the following error. NameError: name 'color' is not defined. How can I initialize square attribute.

Upvotes: 3

Views: 370

Answers (3)

skrx
skrx

Reputation: 20438

I suggest to rewrite the code in this way: Just store the color and a pygame.Rect in the Shape class and give it a draw method. It makes no sense to have your main loop inside of the Shape class. Now you can create as many shapes as you want and draw them in the while loop.

import sys
import pygame


pygame.init()
WHITE = pygame.Color('white')


class Shape:

    def __init__(self, color, x, y, w, h):
        self.color = color
        self.rect = pygame.Rect(x, y, w, h)

    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rect)


def main():
    game_display = pygame.display.set_mode((600, 800))
    shape = Shape(WHITE, 0, 0, 25, 25)
    shape2 = Shape(pygame.Color('sienna1'), 100, 100, 25, 25)
    clock = pygame.time.Clock()
    game_exit = False

    while not game_exit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_exit = True

        game_display.fill((40, 40, 40))
        shape.draw(game_display)
        shape2.draw(game_display)
        pygame.display.update()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pygame.quit()
    sys.exit()

As a side note, variable names in Python should be written in snake_case (lower case with underscores). Also, use sys.exit() to quit the game.

Upvotes: 2

maverick
maverick

Reputation: 59

Try the following code. Hope this helps..

import pygame
import time

class Shape:

    def __init__(self,color,x,y,w,h):
        self.color = color
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def draw_rect(self, gameDisplay):
        square = pygame.draw.rect(gameDisplay,self.color,
                                  [self.x,self.y,self.w,self.h]
                                  )
        while not gameExit:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()              
                    quit()

        gameDisplay.fill(white)
        pygame.display.update()
        clock.tick(60)

def main():

    pygame.init()
    white = (255,255,255)
    red = (255,0,0)

    gameDisplay = pygame.display.set_mode((600,800))
    gameExit = False

    x=0
    y=0
    w=25
    h=25

    sobj = shape(white,0,0,25,25)
    sobj.draw_rect(gameDisplay)

if __name__ == '__main__':
    main()

Upvotes: 1

De Novo
De Novo

Reputation: 7610

your init method runs whenever you create an object of type Shape, e.g.:

shape = Shape(white,x,y,w,h)

Then the init method runs with the arguments you've specified passed as parameters in that method.

With your code written as it is, you're not ever executing that particular line. Try dedenting the while loop so it's not in the class definition. Or, just to test it out, try initializing the class just as a test by running that particular line, shape = Shape(white, x, y, w, h), separately.

Upvotes: 1

Related Questions