2013-05-18 70 views
0

我想繪製與時鐘指針相同的方式移動一條線,但是當我運行該程序時,線條移動像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) 

任何幫助找出我的錯誤可以理解的。

謝謝。

+0

你能後的全部內容錯誤?它應該包括髮生錯誤的行號,這將幫助我們找到錯誤。 – Haz

+0

這裏是錯誤:回溯(最近通話最後一個): 文件 「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

回答

3

這個錯誤是因爲你試圖計算負數的平方根,具體是R**2 - ((CIRCLE_LINE_END_X - CIRCLE_LINE_START_X)**2)的結果。

但是,如果你想繪製一些基於圓形運動的東西,我建議你使用一個名爲DEGREES的變量,並在每次迭代中增加它。然後,您可以使用math.sinmath.cos來計算基於角度的距離。

+0

謝謝A Rodas,您是正確的,sqrt下的數字在到達直徑邊緣時爲負數。 – mazlor

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)