我想繪製與時鐘指針相同的方式移動一條線,但是當我運行該程序時,線條移動像20度,然後程序被凍結,而我可以讀取:「ValueError異常:數學域錯誤」
這裏是我寫的代碼:繪製一條線像時鐘指針在pygame中移動
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)
任何幫助找出我的錯誤可以理解的。
謝謝。
你能後的全部內容錯誤?它應該包括髮生錯誤的行號,這將幫助我們找到錯誤。 – Haz
這裏是錯誤:回溯(最近通話最後一個): 文件 「C:/Desktop/circle_line.py」,第47行,在 CIRCLE_LINE_END_Y = math.sqrt(R ** 2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X) ** 2))+ CIRCLE_LINE_START_Y ValueError:數學域錯誤 –
mazlor