[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] how to get point where is an image ...



On Wed, 2005-10-12 at 00:20 -0300, Juan Reyes LÃpez wrote:

> Actually all I need is an "drag and drop", and I'm trying to do it,
> Âthere's other way to get drag and drop? tks for you patience Peter


There are several ways to make drag and drop work. Here is a simple
demo.

import pygame


def Main():
    pygame.init()
    win = pygame.display.set_mode((640, 480))
    
    font = pygame.font.Font(None, 40)
    dragImage = font.render("drag", 1, (20, 20, 50), (120, 120, 150))
    dropImage = font.render("drop", 1, (20, 50, 20), (120, 150, 120))
    clock = pygame.time.Clock()
    
    dragging = False
    position = [200, 200]
    image = dropImage
    
    while True:
        # clear screen
        win.fill((200, 210, 210))
        
        # Get events
        for event in pygame.event.get():
            if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                rect = image.get_rect().move(position)
                if rect.collidepoint(event.pos):
                    image = dragImage
                    dragging = True
            elif event.type == pygame.MOUSEBUTTONUP:
                image = dropImage
                dragging = False
            elif event.type == pygame.MOUSEMOTION:
                if dragging:
                    position[0] += event.rel[0]
                    position[1] += event.rel[1]

        # Draw
        win.blit(image, position)
        pygame.display.flip()
        clock.tick(30)


if __name__ == "__main__":
    Main()