2024-09-28 20:50:28 发布
网友
我想写一个程序,如果我点击,在pygame屏幕上画一个圆圈。如果再次单击,将绘制另一个圆以及一条将其连接到先前绘制的圆的线。有没有办法追踪你上次点击的位置的坐标
另外,我想在多次点击后创建一个类似星星星座的效果(帮助你可视化)
添加鼠标位置列表:
points = []
单击鼠标时将位置添加到列表:
if e.type == MOUSEBUTTONDOWN: if e.button == 1: points.append(e.pos)
在循环中绘制点:
for pos in points: draw.circle(screen,GREEN, pos, 10)
如果至少有2个点,则可通过^{}绘制点之间的线:
if len(points) > 1: draw.lines(screen, GREEN, False, points)
根据你前面的问题,我建议如下:
from pygame import * init() size = width, height = 650, 650 screen = display.set_mode(size) BLACK = (0, 0, 0) GREEN = (0, 255, 0) running = True myClock = time.Clock() points = [] # Game Loop while running: for e in event.get(): if e.type == QUIT: running = False if e.type == MOUSEBUTTONDOWN: if e.button == 1: points.append(e.pos) if e.button == 3: points = [] screen.fill(BLACK) if len(points) > 1: draw.lines(screen, GREEN, False, points) for pos in points: draw.circle(screen,GREEN, pos, 10) display.flip() myClock.tick(60) quit()
或者,单击的位置可以存储(prev_pos)并在下次单击鼠标绘制线时使用。 我不建议这样做,因为您将丢失有关单击位置的信息:
prev_pos
from pygame import * init() size = width, height = 650, 650 screen = display.set_mode(size) BLACK = (0, 0, 0) GREEN = (0, 255, 0) running = True myClock = time.Clock() prev_pos = None # Game Loop while running: for e in event.get(): if e.type == QUIT: running = False if e.type == MOUSEBUTTONDOWN: if e.button == 1: if prev_pos != None: draw.line(screen, GREEN, prev_pos, e.pos) prev_pos = e.pos draw.circle(screen, GREEN, e.pos, 10) if e.button == 3: prev_pos = None screen.fill(BLACK)) display.flip() myClock.tick(60) quit()
我认为这将解决您的问题: https://www.pygame.org/docs/ref/mouse.html
“pygame.mouse.get_pos() 获取鼠标光标的位置 获取位置()->;(x,y)
Returns the X and Y position of the mouse cursor. The position is relative to the top-left corner of the display. The cursor position can be located outside of the display window, but is always constrained to the screen."
您可以使用pygame.draw.circle()绘制一个圆心位于鼠标所在点的圆,以及一个多边形或多条线(如果您想连接它们)
pygame.draw.circle()
我希望这有帮助
添加鼠标位置列表:
单击鼠标时将位置添加到列表:
在循环中绘制点:
如果至少有2个点,则可通过^{} 绘制点之间的线:
根据你前面的问题,我建议如下:
或者,单击的位置可以存储(
prev_pos
)并在下次单击鼠标绘制线时使用。我不建议这样做,因为您将丢失有关单击位置的信息:
我认为这将解决您的问题: https://www.pygame.org/docs/ref/mouse.html
“pygame.mouse.get_pos() 获取鼠标光标的位置 获取位置()->;(x,y)
您可以使用
pygame.draw.circle()
绘制一个圆心位于鼠标所在点的圆,以及一个多边形或多条线(如果您想连接它们)我希望这有帮助
相关问题 更多 >
编程相关推荐