im trying to get this script to draw a line from the center of the rectangle to the mouse BUT i only want it to draw about 10 pixels out. how can i find that point that is 10 pixels(or points) out?
code so far:

import pygame
from pygame.locals import *
import math
pygame.init()
screen = pygame.display.set_mode((640, 480))

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
pink = (255,200,200)

x = 5
y = 5
username = "Bob"
delta = 0

print len(username)

running = 1

while running:
  for event in pygame.event.get():
    if event.type == QUIT:
      running = 0
    #elif event.type == KEYDOWN and event.key == K_w:
      #print "key"
    #else:
      #print event
      #a = 1
  keystate = pygame.key.get_pressed()
  if keystate[K_w]:
    y-=1
    if keystate[K_d]:
      x+=1
    elif keystate[K_a]:
      x-=1
  elif keystate[K_s]:
    y+=1
    if keystate[K_d]:
      x+=1
    elif keystate[K_a]:
      x-=1
  elif keystate[K_a]:
    x-=1
  elif keystate[K_d]:
    x+=1
  screen.fill((120, 120, 120))
  myfont = pygame.font.SysFont("monospace", 12)
  label = myfont.render(username, 1, (255,255,0))
  screen.blit(label, (x-(len(username)*3), y-12))

  mouse = pygame.mouse.get_pos()


  pygame.draw.rect(screen, red, (x,y,10,10), 0)
  pygame.draw.line(screen, blue, [x+5,y+5], [mouse[0],mouse[1]])
  pygame.display.flip()

pygame.quit()

I've gotten a little closer i think:

(dx, dy) = (mouse[0]-(x+5), mouse[1]-(y+5))
  rads = math.atan2(-dy,dx)
  rads %= 2*math.pi
  degs = math.degrees(rads)
  print str(degs) + " " + str(float(dx)) + " " + str(float(dy))
  pygame.draw.rect(screen, red, (x,y,10,10), 0)#tank
  #pygame.draw.rect(screen, red, (x+5,y+5,10,10), 0)#turret
  pygame.draw.line(screen, blue, [x+5,y+5], [(5*math.cos(degs*(math.pi/180))),(5*math.sin(degs*(math.pi/180)))])
  print (5*math.cos(degs*(math.pi/180))),(5*math.sin(degs*(math.pi/180)))

a little closer... i think

  (dx, dy) = (mouse[0]-(x+5), mouse[1]-(y+5))
  rads = math.atan2(-dy,dx)
  rads %= 2*math.pi
  degs = math.degrees(rads)
  #print str(degs) + " " + str(float(dx)) + " " + str(float(dy))
  pygame.draw.rect(screen, red, (x,y,10,10), 0)#tank
  #pygame.draw.rect(screen, red, (x+5,y+5,10,10), 0)#turret
  pygame.draw.line(screen, blue, [x+5,y+5], [math.sin(degs*(math.pi/180)) + x, math.cos(degs*(math.pi/180)) + y])
This article has been dead for over six months. Start a new discussion instead.