drawing a line moves like a clock pointer in pygame -
i trying draw line moving in same way clock pointer , when run program line move 20 degrees program got frozen , , can read :"valueerror: math domain error"
here code have written :
import pygame import sys import time import math 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: event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if circle_line_end_x <= circle_line_start_x , 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 , 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 , 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 , 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)
any find out mistake appreciated.
thank you.
this error because trying calculate square root of negative number, concretely result of r**2 - ((circle_line_end_x - circle_line_start_x)**2)
.
however, if want draw based on circular movement, suggest use variable called degrees
, increment in each iteration. can use math.sin
, math.cos
calculate distance based on angle.
Comments
Post a Comment