如何在pygame中将鼠标位置设置在一个圆圈周围

时间:2020-07-29 12:18:26

标签: python pygame

我正在尝试在pygame中创建此程序来研究代码/数学,但是我不知道如何在pygame中将鼠标的位置锁定在圆周围,有帮助吗?

    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()

2 个答案:

答案 0 :(得分:2)

要将直线保持在圆圈中,只需相对于鼠标距中心的距离调整鼠标的x / y坐标即可。

这是更新的代码:

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

答案 1 :(得分:2)

我会做一些类似于Mike67的回答,但是会利用pygame的Vector的功能(请参阅文档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()

我也修改了Lin()函数。

编辑:

根据您的评论,您表示希望它始终缩放到圆圈的一侧。在这种情况下,只需跳过测试if line_vector.length() > radius并始终对其进行缩放即可。您可能会尝试仅缩放if length != radius,但是在比较浮点计算的数字时,它们实际上不太可能相同(由于涉及的小数位),并且您可以看到差异是否小于阈值并称它们相等,但这确实不值得复杂化。

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()

相关问题