Home > database >  how can i fix NameError: name 'pos' is not defined?
how can i fix NameError: name 'pos' is not defined?

Time:10-29

I was wondering how I could fix this issue I'm having with my code‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

if anyone could help me, I would be very grateful:‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

class button():
    def __init__(self, color, x, y, width, height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, win, outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width   4, self.height   4), 0)

        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0, 0, 0))
            win.blit(text, (
            self.x   (self.width / 2 - text.get_width() / 2), self.y   (self.height / 2 - text.get_height() / 2)))

    def isOver(self, pos):
        # Pos is the mouse position or a tuple of (x,y) coordinates

        if pos[0] > self.x and pos[0] < self.x   self.width:
            if pos[1] > self.y and pos[1] < self.y   self.height:

                return True


        return False

    def isOver(self, pos):
        # Pos is the mouse position or a tuple of (x,y) coordinates

        if pos[0] > self.x and pos[0] < self.x   self.width:
            if pos[1] > self.y and pos[1] < self.y   self.height:
                return True

        return False

one = button((255,255,255),250,500, 90,90 ,'hi')

def DRAW_WINDOW():WIN.blit(bg,(0,0))one.draw(WIN, (0,0,0))pygame.display.update()

def main():run = True

while run:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION:
            if one.isOver(pos):
                one.color = (255,0,0)
            else:
                one.color = (255,255,255)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if one.isOver(pos):
                print('ow')
        if event.type == pygame.QUIT:
            run = False
    DRAW_WINDOW()


quit()

CodePudding user response:

You seem to use pos in the main method if one.isOver(pos): but you're not defining pos beforehand. Where are you getting the value of pos? It should be assigned before or calculated with the class values.

CodePudding user response:

You're using the pos variable in your code, but it needs to be defined and have a value first. So, for example, you would have to write pos = (0, 0) (for example after defining one) if your coordinates are 0 and 0.

  • Related