我试图以与时钟指针相同的方式绘制一条线,但是当我运行程序时,线条移动20度然后程序被冻结,我可以读到:“ValueError:math domain error “
这是我写的代码:
import pygame
import sys
import time
import math
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((300, 600), 0, 32)
pygame.display.set_caption("circle_line")
BLACK = (0, 0, 0)
WHITE = (250, 250, 250)
R = 50
CIRCLE_LINE_START_X = 150
CIRCLE_LINE_START_Y = 300
CIRCLE_LINE_END_X = CIRCLE_LINE_START_X
CIRCLE_LINE_END_Y = CIRCLE_LINE_START_Y - R
MOVE_SPEED = 3
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if CIRCLE_LINE_END_X <= CIRCLE_LINE_START_X and CIRCLE_LINE_END_Y <= CIRCLE_LINE_START_Y :
CIRCLE_LINE_END_X -= MOVE_SPEED
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
if CIRCLE_LINE_END_X <= CIRCLE_LINE_START_X and CIRCLE_LINE_END_Y >= CIRCLE_LINE_START_Y :
CIRCLE_LINE_END_X += MOVE_SPEED
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
if CIRCLE_LINE_END_X >= CIRCLE_LINE_START_X and CIRCLE_LINE_END_Y >= CIRCLE_LINE_START_Y :
CIRCLE_LINE_END_X += MOVE_SPEED
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
if CIRCLE_LINE_END_X >= CIRCLE_LINE_START_X and CIRCLE_LINE_END_Y <= CIRCLE_LINE_START_Y :
CIRCLE_LINE_END_X -= MOVE_SPEED
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
windowSurface.fill(WHITE)
pygame.draw.line(windowSurface, BLACK, (CIRCLE_LINE_START_X, CIRCLE_LINE_START_Y), (CIRCLE_LINE_END_X, CIRCLE_LINE_END_Y), 3)
pygame.display.update()
time.sleep(0.02)
任何有助于找出我的错误的帮助将不胜感激。
谢谢。答案 0 :(得分:3)
此错误是因为您正在尝试计算负数的平方根,具体而言是R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)
的结果。
但是,如果你想根据圆周运动绘制一些东西,我建议你使用一个名为DEGREES
的变量,并在每次迭代中递增它。然后,您可以使用math.sin
和math.cos
根据角度计算距离。
答案 1 :(得分:0)
我在我使用的算法中进行了一些调制,这段代码与上面的代码相比给出了可接受的结果,这里是有趣的代码:
import pygame
import sys
import time
import math
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((300, 600), 0, 32)
pygame.display.set_caption("circle_line")
BLACK = (0, 0, 0)
WHITE = (250, 250, 250)
R = 50
CIRCLE_LINE_START_X = 150
CIRCLE_LINE_START_Y = 300
MIN_X = CIRCLE_LINE_START_X - R
MAX_X = CIRCLE_LINE_START_X + R
CIRCLE_LINE_END_X = MIN_X
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
MOVE_SPEED = 5 # has to be a number where as ( R % MOVE_SPEED = 0 )
DIRECTION = "right"
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if CIRCLE_LINE_END_X >= MAX_X :
CIRCLE_LINE_END_X = MAX_X
DIRECTION = "left"
if CIRCLE_LINE_END_X <= MIN_X :
CIRCLE_LINE_END_X = MIN_X
DIRECTION = "right"
if DIRECTION == "right":
CIRCLE_LINE_END_X += MOVE_SPEED
CIRCLE_LINE_END_Y = math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)) + CIRCLE_LINE_START_Y
if DIRECTION == "left":
CIRCLE_LINE_END_X -= MOVE_SPEED
CIRCLE_LINE_END_Y = CIRCLE_LINE_START_Y - math.sqrt(R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2))
windowSurface.fill(WHITE)
pygame.draw.line(windowSurface, BLACK, (CIRCLE_LINE_START_X, CIRCLE_LINE_START_Y), (CIRCLE_LINE_END_X, CIRCLE_LINE_END_Y), 3)
pygame.display.update()
time.sleep(0.02)