I have problem with results of math operators. I want to draw the line with range =100 by using angle but i get different lenght on screen... I use sin/cos operators and angle of line. Print results are correct values but on screen line is shorter.
My code
"""Class"""
class Knight(pygame.sprite.Sprite):
[..]
# Knight spear
self.spear_color = (0, 0, 0)
self.spear_length = 100 # pixels
self.spear_angle = 0
# Movement
self.speed = 0
self.direction = direct # 1=right, -1=left
def setSpearPosition(self, angle):
""" The user has adjusted the spear position, regenerate the Knight's image """
self.spear_angle = angle
def raiseSpear(self):
self.setSpearPosition(self.spear_angle + 1 + random.randint(0, 2))
def lowerSpear(self):
self.setSpearPosition(self.spear_angle - 1 - random.randint(0, 2))
#[oder classes...]
#[screen...]
#[Values and sprite groups]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# handle player input
keys = pygame.key.get_pressed()
# Moving knight
if game_active:
if event.type == pygame.KEYDOWN:
#Key input
# Draw the screen
# Knight1 spear
K1_spear_point_x = Knight1.rect.x + (Knight1.spear_length * math.sin(math.radians(Knight1.spear_angle)))
K1_spear_point_y = Knight1.rect.y + (Knight1.spear_length * math.cos(math.radians(Knight1.spear_angle)))
pygame.draw.line(screen, Knight1.spear_color,
(Knight1.rect.x + Knight1.rect.width / 2, Knight1.rect.y + Knight1.rect.height / 2),
(K1_spear_point_x, K1_spear_point_y), 2)
# Knight2 spear
K2_spear_point_x = Knight2.rect.x - (Knight2.spear_length * math.sin(math.radians(Knight2.spear_angle)))
K2_spear_point_y = Knight2.rect.y - (Knight2.spear_length * math.cos(math.radians(Knight2.spear_angle)))
pygame.draw.line(screen, Knight2.spear_color,
(Knight2.rect.x + Knight2.rect.width / 2, Knight2.rect.y + Knight2.rect.height / 2),
(K2_spear_point_x, K2_spear_point_y), 2)
#[code...]
Edit: I changed starting point of my lines... All works perfect
K1_spear_point_x = (Knight1.rect.x+Knight1.rect.width / 2) + (Knight1.spear_length * math.cos(math.radians(Knight1.spear_angle)))
K1_spear_point_y = (Knight1.rect.y+ Knight1.rect.height / 2) + (Knight1.spear_length * math.sin(math.radians(Knight1.spear_angle)))
pygame.draw.line(screen, Knight1.spear_color,
(Knight1.rect.x + Knight1.rect.width / 2, Knight1.rect.y + Knight1.rect.height / 2),
(K1_spear_point_x, K1_spear_point_y), 2)
# Knight2 spear
K2_spear_point_x = (Knight2.rect.x+Knight2.rect.width / 2) - (Knight2.spear_length * math.cos(math.radians(Knight2.spear_angle)))
K2_spear_point_y = (Knight2.rect.y+ Knight2.rect.height / 2) + (Knight2.spear_length * math.sin(math.radians(Knight2.spear_angle)))
pygame.draw.line(screen, Knight2.spear_color,
(Knight2.rect.x + Knight2.rect.width / 2, Knight2.rect.y + Knight2.rect.height / 2),
(K2_spear_point_x, K2_spear_point_y), 2)
