所以我正在尝试制作一款塔防游戏,我尝试制作一个后退按钮,但在它起作用之前,但现在当我尝试时,它给了我一个数学域错误。我在网上看到,如果我取-1之类的根,它可能会出现,但这是不可能的,因为该值正在被平方。它发生在 Class Back_Button
下的 CheckClicked
函数中。有人能解出来吗?
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()
最佳答案
看起来您在第二个平方项中放错了括号。 这就是你现在拥有的:
(pos[1]- (self.y - self.text.get_height())**2)
相反,尝试将右括号移至指数之前:
(pos[1]- (self.y - self.text.get_height()))**2
关于python - 获取数学域错误,但这不是因为负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65328722/