kedar
kedar

Reputation: 19

Python and Pygame method can't be found when clearly defined?

I'm trying to create my own Python Code( With the help of Youtuber RootofTheNull) and I'm getting this error.

Can someone help?

import pygame
from colors import *


class Block (pygame.sprite.Sprite):

    def set_message(text):
        global message,previous_message
        message = font.render(text,True,black,white)
        previous_message=message

    def __init__(self, color =blue, width=64, height = 64):

        super( Block, self ).__init__()


        self.image = pygame.Surface((width, height))
        self.image.fill( color )
        self.rect=self.image.get_rect()
        self.sound=pygame.mixer.Sound("")
        self.origin_x = self.rect.centerx
        self.origin_y = self.rect.centery

    def set_properties(self):
        self.rect=self.image.get_rect()

        self.origin_x = self.rect.centerx
        self.origin_y = self.rect.centery

    def set_position (self, x, y):
        self.rect.x = x-self.origin_x
        self.rect.y = y-self.origin_y
    def set_image( self, filename = None ):
        if (filename !=None ):
            self.image = pygame.image.load (filename)
            self.set_properties()

    def play_sound(self):
        self.sound.play(-1)

if ( __name__ == "__main__"):
    pygame.init()
    window_size = window_width, window_height = 512, 384
    window = pygame.display.set_mode(window_size, pygame.RESIZABLE)

    pygame.display.set_caption( " Cloud Runner ")
    window.fill ( white )
    clock = pygame.time.Clock()
    frames_per_second = 60

    block_group = pygame.sprite.Group()
    a_block = Block()
    a_block.play_sound()
    a_block.set_image("beaten_brick_tiled.png")
    a_block.set_position( window_width/2, window_height/2)
    another_block = Block( red )
    another_block.set_position(100,100)
    block_group.add(a_block, another_block)


    font = pygame.font.SysFont("Times New Roman, Arial",30)

    message = previous_message = None
    set_message("Hello World!")


    running = True

    while ( running ):
        pygame.event.pump()
        for event in pygame.event.get():
            if (event.type == pygame.QUIT) or \
            (event.type ==pygame.KEYDOWN and  \
            (event.key == pygame.K_ESCAPE or event.key ==pygame.K_q)):
                running = False
            if (event.type == pygame.MOUSEMOTION):
                mouse_pos = pygame.mouse.get_pos()
                a_block.set_position(mouse_pos[0],mouse_pos[1])


        clock.tick (frames_per_second)
        window.fill ( white )
        if (message != previous_message):
            set_message(message)

        window.blit(message,(window_width/2 - message.get_rect().width/2,window_height/2-100))
        block_group.draw( window )
        pygame.display.update()

    pygame.quit()

The error:

 File "C:/Users/kedz0_000/Desktop/Python Crap/CloudRunner.py", line 64, in <module>
    set_message('Hello World!')
NameError: name 'set_message' is not defined

Upvotes: 2

Views: 88

Answers (1)

John La Rooy
John La Rooy

Reputation: 304463

You have set_message as a method of Block. It doesn't seem to belong there

def set_message(text):
    global message, previous_message
    message = font.render(text, True, black, white)
    previous_message = message

class Block (pygame.sprite.Sprite):

    def __init__(self, color=blue, width=64, height=64):

        super( Block, self ).__init__()

    ...

Upvotes: 2

Related Questions