Vinicius Fernandes
Vinicius Fernandes

Reputation: 23

how do i set the mouse position around a circle in pygame

I'm trying to create this program in pygame to study code/math, but i don't know how to lock the mouse position around a circle in pygame, any help?

    import pygame
    pygame.init()
    x = 250
    y = 250

    window = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("around circle")

    #line to be created
    def Lin(xref, yref):
       lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)

    window_open = True
    while window_open:
       pygame.display.update()
       window.fill((0, 0, 0))

      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          window_open = False
        
      mousepos = pygame.mouse.get_pos()
      xref = mousepos[0]
      yref = mousepos[1]

      # 2 circles to get only the border
      cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
      cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)

      Lin(xref, yref)


   pygame.quit()

Upvotes: 2

Views: 231

Answers (2)

Glenn Mackintosh
Glenn Mackintosh

Reputation: 2779

I would do something similar to Mike67 answer, but would take advantage of the features of pygame's Vector's (see docs here).

import pygame

pygame.init()
x = 250
y = 250

radius = 100
center = pygame.Vector2(x, y)

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")

#line to be created
def Lin(start_point, vector):
    pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)

window_open = True
while window_open:
    pygame.display.update()
    window.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            window_open = False
        
    mousepos = pygame.mouse.get_pos()
    line_vector = pygame.Vector2(mousepos) - center

    if line_vector.length() > radius:
        line_vector.scale_to_length(radius)

    # 2 circles to get only the border
    cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
    cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)

    Lin(center, line_vector)


pygame.quit()

I modified the Lin() function as well.

EDIT:

From your comment you indicated that you want it to always scale to the side of the circle. In that case just skip the test if line_vector.length() > radius and always scale it. You might be tempted to only scale it if length != radius, but when comparing floating point calculated numbers they are very unlikely to actually be the same (because of the decimal places involved) and you could see if the difference is less than a threshold and call them equal, but it really isn't worth the complication.

import pygame

pygame.init()
x = 250
y = 250

radius = 100
center = pygame.Vector2(x, y)

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")

#line to be created
def Lin(start_point, vector):
    pygame.draw.line(window, (250, 250, 0), start_point, start_point+vector, 1)

window_open = True
while window_open:
    pygame.display.update()
    window.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            window_open = False
        
    mousepos = pygame.mouse.get_pos()
    line_vector = pygame.Vector2(mousepos) - center

    line_vector.scale_to_length(radius)

    # 2 circles to get only the border
    cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius, 1)
    cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), radius-1, 1)

    Lin(center, line_vector)


pygame.quit()

Upvotes: 2

Mike67
Mike67

Reputation: 11342

To keep the line in the circle, just adjust the mouse x/y coordinates relative to the mouse distance from the center.

Here is the updated code:

import pygame
import math
pygame.init()
x = 250
y = 250

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")

#line to be created
def Lin(xref, yref):
   lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)

window_open = True
while window_open:
    pygame.display.update()
    window.fill((0, 0, 0))

    for event in pygame.event.get():
      if event.type == pygame.QUIT:
         window_open = False

    mousepos = pygame.mouse.get_pos()
    
    xref = mousepos[0]
    yref = mousepos[1]

    # get mouse distance from center
    dist = math.sqrt((xref-x)**2 + (yref-y)**2)
    if (dist > 100): # if mouse outside circle, adjust x/y proportionally
       xref = x + (xref - x) * (100/dist)
       yref = y + (yref - y) * (100/dist)
    
    # 2 circles to get only the border
    cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
    cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)

    Lin(xref, yref)

pygame.quit()

Circle

Upvotes: 2

Related Questions