Python class attribute initialization

I have the 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 the square attribute of the Shape class using init , but I am getting the following error. NameError: name "color" is undefined. How to initialize a square attribute.

+3


source to share


3 answers


I suggest rewriting the code this way: just store the color pygame.Rect

in the class Shape

and give it a method draw

. It doesn't make sense to have your main loop inside a class Shape

. Now you can create as many shapes as you like and draw them in a 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 must be written in snake_case

(lowercase with underscores). Also, use sys.exit()

to exit the game.

+2


source


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

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

      



The init method is then executed with the arguments you specified as parameters in that method.

When your code is written as is, you never execute that particular line. Try to isolate the while loop so that it's not in the class definition. Or, just to test this, try initializing the class the same way as a test, by running this particular line, shape = Shape (white, x, y, w, h), separately.

+1


source


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()

      

+1


source







All Articles