有没有办法在pygame中获取/单击特定对象的坐标? [英] Is there a way to get the coordinates of a specific object/click in pygame?

查看:68
本文介绍了有没有办法在pygame中获取/单击特定对象的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个程序,如果我点击,会在 pygame 屏幕上绘制一个圆圈.如果我再次单击,则会绘制另一个圆以及将其连接到上一个绘制的圆的线.有什么办法可以追踪你上次点击的坐标吗?

ps.我想在多次点击后创建一个像星星星座效果(帮助你可视化)

解决方案

添加鼠标位置列表:

points = []

添加鼠标点击位置列表:

如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:点附加(e.pos)

在循环中绘制点:

用于点数:draw.circle(屏幕,绿色,位置,10)

如果至少有2个点,那么点之间的线可以通过

from pygame import *在里面()大小 = 宽度,高度 = 650, 650屏幕 = display.set_mode(size)黑色 = (0, 0, 0)绿色 = (0, 255, 0)运行 = 真myClock = time.Clock()点数 = []# 游戏循环在跑步的时候:对于 event.get() 中的 e:如果 e.type == 退出:运行 = 错误如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:点附加(e.pos)如果 e.button == 3:点数 = []屏幕填充(黑色)如果 len(points) >1:draw.lines(屏幕,绿色,假,点)对于 pos 点数:draw.circle(屏幕,绿色,位置,10)display.flip()myClock.tick(60)放弃()

<小时>

或者,单击位置可以存储(prev_pos)并在下次单击鼠标绘制线条时使用.
我不建议这样做,因为您会丢失有关点击位置的信息:

from pygame import *在里面()大小 = 宽度,高度 = 650, 650屏幕 = display.set_mode(size)黑色 = (0, 0, 0)绿色 = (0, 255, 0)运行 = 真myClock = time.Clock()prev_pos = 无# 游戏循环在跑步的时候:对于 event.get() 中的 e:如果 e.type == 退出:运行 = 错误如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:如果 prev_pos != 无:draw.line(屏幕,绿色,prev_pos,e.pos)prev_pos = e.posdraw.circle(屏幕,绿色,e.pos,10)如果 e.button == 3:prev_pos = 无屏幕填充(黑色))display.flip()myClock.tick(60)放弃()

I want to write a program where if I click, a circle is drawn on the pygame screen. 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:

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

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

这篇关于有没有办法在pygame中获取/单击特定对象的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆