[英]How to click and drag an object in pygame?
I have created this window that allows the user to create dots in the windows and link them, What I'm trying to do now is to allow him to move which ever point he wants after finishing drawing them我创建了这个窗口,允许用户在窗口中创建点并链接它们,我现在要做的是允许他在完成绘制后移动他想要的任何点
In my example I allow him to draw five dots, then I want him to move the point of his choice, I really don't know how to do this在我的例子中我让他画五个点,然后我想让他移动他选择的点,我真的不知道该怎么做
This is my code:这是我的代码:
import pygame
#Initialise pygame
pygame.init()
#Create the screen
screen = pygame.display.set_mode((1200, 700))
#Change the title and the icon
pygame.display.set_caption('The Thoughtful Minds')
icon = pygame.image.load('IA.png')
pygame.display.set_icon(icon)
#Dots
dot = pygame.image.load('point.png')
class Dot:
def __init__(self, pos):
self.cx, self.cy = pos
def draw(self):
screen.blit(dot,(self.cx-8 , self.cy-8))
def text_objects(text,font):
textSurface = font.render(text, True, (100,100,100))
return textSurface, textSurface.get_rect()
dots = []
#Running the window
i = 0
running = True
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
for oPosition in dots:
if ((oPosition.cx - 8) < mx < (oPosition.cx + 8)) and ((oPosition.cy - 8) < my < (oPosition.cy + 8)):
'''
What should I put her to allow him to move the dot he clicked on
'''
if i<3:
# append a new dot at the current mouse position
dots.append(Dot((mx,my)))
i += 1
# clear the display
screen.fill((30,30,30))
# draw all the dots
if len(dots)>1:
for i in range(len(dots)-1):
pygame.draw.line(screen, (50, 50, 50), [dots[i].cx,dots[i].cy],[dots[i+1].cx,dots[i+1].cy],2 )
for d in dots:
d.draw()
if mx < 50 and my < 50:
pygame.draw.rect(screen,(24,24,24),(0,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(0,0,50,50))
text = pygame.font.Font("freesansbold.ttf",25)
textSurf, textRect = text_objects('–', text)
textRect.center = (25,25)
screen.blit( textSurf, textRect )
if 52 < mx < 102 and my < 50:
pygame.draw.rect(screen,(24,24,24),(52,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(52,0,50,50))
textSurf, textRect = text_objects('+', text)
textRect.center = (76,25)
screen.blit( textSurf, textRect )
# update the dispalay
pygame.display.flip()
Thanks.谢谢。
pygame is low lewel, it does not have any preimplemented method for drag and drop. pygame 是低级的,它没有任何预先实现的拖放方法。 You have to built it by yourself.你必须自己构建它。
You have to play with the various events types.您必须处理各种事件类型。 Here is the idea:这是一个想法:
First, create a variable dragged_dot = None
outside the main loop.首先,在主循环外创建一个变量dragged_dot = None
。 And in the event loop check for the following events:并在事件循环中检查以下事件:
pygame.MOUSEBUTTONDOWN
event tells you when the button is pressed. pygame.MOUSEBUTTONDOWN
事件会告诉您何时按下按钮。 When you detect this event, check if the mouse is clicking on an existing dot or not.当您检测到此事件时,请检查鼠标是否单击了现有的点。 This is what your for
loop does.这就是你的for
循环所做的。 If not, add a new dot like you are already doing.如果没有,请像您已经在做的那样添加一个新点。 Otherwise, set the dot to be dragged: dragged_dot = oPosition
.否则,设置要拖动的点: dragged_dot = oPosition
。pygame.MOUSEMOTION
event tells you when the mouse moves. pygame.MOUSEMOTION
事件告诉您鼠标何时移动。 When you detect this event, check if there is a dragged dot: if dragged_dot is not None
.当您检测到此事件时,请检查是否有拖动点: if dragged_dot is not None
。 If so, edit it's coordinates adding the mouse motion so that it can be redrawn at a new position (remember to delete the image of the dot at the previous postion).如果是这样,编辑它的坐标,添加鼠标运动,以便它可以在新位置重新绘制(记住删除上一个位置的点图像)。 Use event.rel
to know the difference between previous mouse position and current mouse position.使用event.rel
了解之前鼠标位置和当前鼠标位置之间的差异。pygame.MOUSEBUTTONUP
event tells you when the button is released. pygame.MOUSEBUTTONUP
事件会告诉您按钮何时被释放。 Simply set dragged_dot = None
, so that the dot is dropped and will not follow the mouse motion anymore.只需设置dragged_dot = None
,这样点就会被丢弃并且不再跟随鼠标移动。Ok this is the answer if you are interested好的,如果您有兴趣,这就是答案
import pygame
#Initialise pygame
pygame.init()
#Create the screen
screen = pygame.display.set_mode((1200, 700))
screen.set_alpha(None)
#Change the title and the icon
pygame.display.set_caption('The Thoughtful Minds')
icon = pygame.image.load('IA.png')
pygame.display.set_icon(icon)
#Dots
dot = pygame.image.load('point.png')
class Dot:
def __init__(self, pos):
self.cx, self.cy = pos
def draw(self):
screen.blit(dot,(self.cx-8 , self.cy-8))
def text_objects(text,font):
textSurface = font.render(text, True, (100,100,100))
return textSurface, textSurface.get_rect()
dots = []
#Running the window
i = 0
running = True
draging = False
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
pass
elif event.type == pygame.MOUSEBUTTONDOWN:
for oPosition in dots:
if ((oPosition.cx - 8) < mx < (oPosition.cx + 8)) and ((oPosition.cy - 8) < my < (oPosition.cy + 8)):
draging = True
break
if i<3:
# append a new dot at the current mouse position
dots.append(Dot((mx,my)))
i += 1
elif event.type == pygame.MOUSEBUTTONUP:
draging = False
elif event.type == pygame.MOUSEMOTION:
if draging :
oPosition.cx = mx
oPosition.cy = my
# clear the display
screen.fill((30,30,30))
# draw all the dots
if len(dots)>1:
for i in range(len(dots)-1):
pygame.draw.line(screen, (50, 50, 50), [dots[i].cx,dots[i].cy],[dots[i+1].cx,dots[i+1].cy],2 )
for d in dots:
d.draw()
if mx < 50 and my < 50:
pygame.draw.rect(screen,(24,24,24),(0,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(0,0,50,50))
text = pygame.font.Font("freesansbold.ttf",25)
textSurf, textRect = text_objects('–', text)
textRect.center = (25,25)
screen.blit( textSurf, textRect )
if 52 < mx < 102 and my < 50:
pygame.draw.rect(screen,(24,24,24),(52,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(52,0,50,50))
textSurf, textRect = text_objects('+', text)
textRect.center = (76,25)
screen.blit( textSurf, textRect )
# update the dispalay
pygame.display.flip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.