0

我正在尝试绘制一条以与时钟指针相同的方式移动的线,但是当我运行程序时,线移动了 20 度,然后程序被冻结,我可以读到:“ValueError:数学域错误”
这里是我写的代码:

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)

任何帮助找出我的错误将不胜感激。

谢谢你。

4

2 回答 2

3

这个错误是因为你试图计算一个负数的平方根,具体来说是R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2).

但是,如果您想基于圆周运动绘制某些东西,我建议您使用一个名为的变量DEGREES并在每次迭代中递增它。然后您可以使用math.sinmath.cos根据角度计算距离。

于 2013-05-18T23:35:33.403 回答
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)
于 2013-05-19T23:01:08.810 回答