简体   繁体   English

有没有办法在 pygame 中获取特定对象的坐标/单击?

[英]Is there a way to get the coordinates of a specific object/click in pygame?

I want to write a program where if I click, a circle is drawn on the pygame screen.我想编写一个程序,如果单击,则会在 pygame 屏幕上绘制一个圆圈。 If I click again, another circle is drawn as well as a line connecting it to the previous circle drawn.如果我再次单击,则会绘制另一个圆圈以及将其连接到先前绘制的圆圈的线。 Is there any way to track the coordinates of where you last clicked?有什么方法可以跟踪您最后一次点击的坐标吗?

ps.附言。 I want to create like a star constellation effect after many clicks (to help you visualize)我想在多次点击后创建像星座一样的效果(帮助您形象化)

Add a list for the mouse positions:添加鼠标位置列表:

points = []

Add the position to list when the mouse is clicked:单击鼠标时将 position 添加到列表中:

if e.type == MOUSEBUTTONDOWN:
    if e.button == 1:
        points.append(e.pos)    

Draw the points in a loop:在循环中绘制点:

for pos in points:
   draw.circle(screen,GREEN, pos, 10)

If there are at least 2 points, then the lines between the points can be drawn by pygame.draw.lines() :如果至少有 2 个点,则可以通过pygame.draw.lines()绘制点之间的线:

if len(points) > 1:
    draw.lines(screen, GREEN, False, points)

Based on your previous question, I suggest the following:根据您之前的问题,我建议以下内容:

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

Alternatively the clicked position can be stored ( prev_pos ) and used the next time, when the mouse is clicked to draw a line.或者,单击的 position 可以存储( prev_pos )并在下次单击鼠标绘制线条时使用。
I do not recommend this, because you'll lose the information about the clicked positions:我不建议这样做,因为您会丢失有关点击位置的信息:

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

I think that this will solve your problem: https://www.pygame.org/docs/ref/mouse.html我认为这将解决您的问题: https://www.pygame.org/docs/ref/mouse.html

" pygame.mouse.get_pos() get the mouse cursor position get_pos() -> (x, y) " pygame.mouse.get_pos() 获取鼠标 cursor position 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."

You can use pygame.draw.circle() to draw a circle with a center at the point where the mouse is, and a polygon or lines if you'd like to connect them.您可以使用pygame.draw.circle()在鼠标所在的点绘制一个圆心,如果您想连接它们,可以使用多边形或线条。

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM