Skip to content
Advertisement

Getting Math domain Error but it is not because of negative values

So I am trying to make a tower defense game and I tried to make a back button and before it worked but now when I try it gives me a math domain error. I’ve seen online it may come if I take something like the root of -1 but that’s not possible because the value is being squared. It happens in CheckClicked function under Class Back_Button. Can anybody figure it out?

import pygame, sys, math
from Colors import get
def main():
    global run, back
    pygame.init()

    width, height = 800,600
    win = pygame.display.set_mode((width, height))

    class Button:
        def __init__(self, pos, text, size, tx_col, bg_col, rectangle = True):
            self.rectangle = rectangle
            self.bg_col = bg_col
            self.tx_col = tx_col
            self.x = pos[0]
            self.y = pos[1]
            font = pygame.font.SysFont('timesnewroman', size)
            self.text = font.render(text, True, get(self.tx_col), get(self.bg_col))

        def Draw_Button(self):
            if self.rectangle:
                pygame.draw.rect(win, get(self.tx_col), [(self.x - self.text.get_width()//2, self.y-self.text.get_height()//2), (self.text.get_width(), self.text.get_height())])
                win.blit(self.text, (self.x - self.text.get_width()//2, self.y-self.text.get_height()//2))
            else:
                pygame.draw.circle(win, get(self.bg_col), (self.x - self.text.get_width()//2, self.y-self.text.get_height()//2), self.text.get_width())
                win.blit(self.text, (self.x - self.text.get_width(), self.y-self.text.get_height()))

    class Start_Button(Button):
        def CheckClicked(self, pos):
            global run
            if (self.x-self.text.get_width()//2<=pos[0]<=(self.x+self.text.get_width()//2)) and (self.y-self.text.get_height()//2<=pos[1]<=(self.y+self.text.get_height()//2)):
                run = False

    class Back_Button(Button):
        def CheckClicked(self, pos):
            global back, run
            if math.sqrt((pos[0]- (self.x - self.text.get_width()))**2 + (pos[1]- (self.y - self.text.get_height())**2))<=self.text.get_width():
                back, run = True, False

    class Logo:
        def __init__(self, x, y, time, img):
            self.x = x
            self.y = y
            self.time = time
            self.img = pygame.image.load(img).convert_alpha()
            self.img.set_alpha(0)
        
        def Transper(self):
            clock = pygame.time.Clock()
            for i in range(226):
                self.img.set_alpha(i)
                win.blit(self.img, [self.x, self.y])
                clock.tick(self.time)
                print(self.img.get_alpha())
                pygame.display.update()

    class Ballon:
        def __init__(self, path_points, health, speed):
            self.path_points = path_points
            self.health = health
            self.speed = speed
            self.img =  pygame.image.load(f'{self.health}.png')           

    class Round:
        def __init__(self, list_of_ballons):
            self.list_of_ballons = list_of_ballons
        def Start_Round(self):
            li = []
            for i in self.list_of_ballons:
                li.append(Ballon(11, 22, 33))

    class Game:
        def __init__(self, rounds, curr_map, enem_cord):
            self.rounds = rounds
            self.map = pygame.image.load(curr_map)
            self.enem_cord = enem_cord

        def display_map(self):
            win.blit(pygame.transform.scale(self.map, (width, height)), (0,0))

    #Intro
    #Logo_Pic = Logo(width//2, height//2, 60, 'Screenshot2020-10-24.png')
    #Logo_Pic.Transper()

    New_Button = Start_Button([400, 300], 'New Game', 25, 'red', 'white')
    BackButton = Back_Button([35, 585], '<-', 25, 'red', 'white', False)
    running = True
    back = False

    while running:
        back = False

    # Main Menu
        run = True
        while run:
            win.fill(get('black'))
            New_Button.Draw_Button()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    New_Button.CheckClicked(pos)

            pygame.display.update()
        #Choose Map
        run = True
        GameA = Game(40, 'MonkeyMeadow_No_UI.png', [])
        while run:
            win.fill(get('black'))
            
            GameA.display_map()
            BackButton.Draw_Button()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    BackButton.CheckClicked(pos)

            pygame.display.update()
        if back:
            continue

main()

Advertisement

Answer

It looks like you misplaced a parenthesis in the second squared term. This is what you have now:

(pos[1]- (self.y - self.text.get_height())**2)

Instead of that, try moving the closing parenthesis to before the exponential:

(pos[1]- (self.y - self.text.get_height()))**2
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement