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?
JavaScript
x
126
126
1
import pygame, sys, math
2
from Colors import get
3
def main():
4
global run, back
5
pygame.init()
6
7
width, height = 800,600
8
win = pygame.display.set_mode((width, height))
9
10
class Button:
11
def __init__(self, pos, text, size, tx_col, bg_col, rectangle = True):
12
self.rectangle = rectangle
13
self.bg_col = bg_col
14
self.tx_col = tx_col
15
self.x = pos[0]
16
self.y = pos[1]
17
font = pygame.font.SysFont('timesnewroman', size)
18
self.text = font.render(text, True, get(self.tx_col), get(self.bg_col))
19
20
def Draw_Button(self):
21
if self.rectangle:
22
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())])
23
win.blit(self.text, (self.x - self.text.get_width()//2, self.y-self.text.get_height()//2))
24
else:
25
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())
26
win.blit(self.text, (self.x - self.text.get_width(), self.y-self.text.get_height()))
27
28
class Start_Button(Button):
29
def CheckClicked(self, pos):
30
global run
31
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)):
32
run = False
33
34
class Back_Button(Button):
35
def CheckClicked(self, pos):
36
global back, run
37
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():
38
back, run = True, False
39
40
class Logo:
41
def __init__(self, x, y, time, img):
42
self.x = x
43
self.y = y
44
self.time = time
45
self.img = pygame.image.load(img).convert_alpha()
46
self.img.set_alpha(0)
47
48
def Transper(self):
49
clock = pygame.time.Clock()
50
for i in range(226):
51
self.img.set_alpha(i)
52
win.blit(self.img, [self.x, self.y])
53
clock.tick(self.time)
54
print(self.img.get_alpha())
55
pygame.display.update()
56
57
class Ballon:
58
def __init__(self, path_points, health, speed):
59
self.path_points = path_points
60
self.health = health
61
self.speed = speed
62
self.img = pygame.image.load(f'{self.health}.png')
63
64
class Round:
65
def __init__(self, list_of_ballons):
66
self.list_of_ballons = list_of_ballons
67
def Start_Round(self):
68
li = []
69
for i in self.list_of_ballons:
70
li.append(Ballon(11, 22, 33))
71
72
class Game:
73
def __init__(self, rounds, curr_map, enem_cord):
74
self.rounds = rounds
75
self.map = pygame.image.load(curr_map)
76
self.enem_cord = enem_cord
77
78
def display_map(self):
79
win.blit(pygame.transform.scale(self.map, (width, height)), (0,0))
80
81
#Intro
82
#Logo_Pic = Logo(width//2, height//2, 60, 'Screenshot2020-10-24.png')
83
#Logo_Pic.Transper()
84
85
New_Button = Start_Button([400, 300], 'New Game', 25, 'red', 'white')
86
BackButton = Back_Button([35, 585], '<-', 25, 'red', 'white', False)
87
running = True
88
back = False
89
90
while running:
91
back = False
92
93
# Main Menu
94
run = True
95
while run:
96
win.fill(get('black'))
97
New_Button.Draw_Button()
98
for event in pygame.event.get():
99
if event.type == pygame.QUIT:
100
sys.exit()
101
if event.type == pygame.MOUSEBUTTONDOWN:
102
pos = pygame.mouse.get_pos()
103
New_Button.CheckClicked(pos)
104
105
pygame.display.update()
106
#Choose Map
107
run = True
108
GameA = Game(40, 'MonkeyMeadow_No_UI.png', [])
109
while run:
110
win.fill(get('black'))
111
112
GameA.display_map()
113
BackButton.Draw_Button()
114
for event in pygame.event.get():
115
if event.type == pygame.QUIT:
116
sys.exit()
117
if event.type == pygame.MOUSEBUTTONDOWN:
118
pos = pygame.mouse.get_pos()
119
BackButton.CheckClicked(pos)
120
121
pygame.display.update()
122
if back:
123
continue
124
125
main()
126
Advertisement
Answer
It looks like you misplaced a parenthesis in the second squared term. This is what you have now:
JavaScript
1
2
1
(pos[1]- (self.y - self.text.get_height())**2)
2
Instead of that, try moving the closing parenthesis to before the exponential:
JavaScript
1
2
1
(pos[1]- (self.y - self.text.get_height()))**2
2