【问题标题】:How to click and drag an object in pygame?如何在pygame中单击并拖动对象?
【发布时间】:2019-11-23 14:08:51
【问题描述】:

我创建了这个窗口,允许用户在窗口中创建点并链接它们, 我现在要做的是让他在画完它们之后移动他想要的任何点

在我的例子中我让他画五个点,然后我想让他移动他选择的点,我真的不知道该怎么做

这是我的代码:

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

谢谢。

【问题讨论】:

    标签: python python-3.x pygame drag-and-drop


    【解决方案1】:

    pygame 是低级的,它没有任何预先实现的拖放方法。您必须自己构建它。

    您必须玩各种事件类型。思路如下:

    首先,在主循环外创建一个变量dragged_dot = None。并在事件循环中检查以下事件:

    • pygame.MOUSEBUTTONDOWN 事件告诉您何时按下按钮。当您检测到此事件时,请检查鼠标是否正在单击现有点。这就是您的 for 循环所做的。如果没有,请像您已经在做的那样添加一个新点。否则,设置要拖动的点:dragged_dot = oPosition
    • pygame.MOUSEMOTION 事件告诉您鼠标何时移动。当您检测到此事件时,请检查是否有拖动点:if dragged_dot is not None。如果是这样,请编辑它的坐标添加鼠标运动,以便可以在新位置重绘它(记得删除前一个位置的点图像)。使用event.rel 可以知道之前鼠标位置和当前鼠标位置之间的差异。
    • pygame.MOUSEBUTTONUP 事件告诉您何时释放按钮。只需设置dragged_dot = None,这样点就会被放下,不再跟随鼠标移动。

    【讨论】:

    • 感谢您的评论我认为我解决了问题
    【解决方案2】:

    好的,如果你有兴趣,这就是答案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多