[Pygame] creating a smooth drag and drop feature in pygame

[Pygame] creating a smooth drag and drop feature in pygame

Postby metulburr » Mon Apr 22, 2013 11:12 pm

Well the way i coded it, it wasnt going to be smooth. I expected it to be choppy upon dragging it. but i expected it to follow the mouse across the entire screen and not the initial surface.get_rect() location. I guess i am not sure why the image is dropped thereafter?
Code: Select all

import pygame
import os
import time

class ImageClass:
   def __init__(self):
      self.image = pygame.Surface((50,50)).convert_alpha()
      self.image.fill((255,255,0))
      self.mask = pygame.mask.from_surface(self.image)
      self.rect = self.image.get_rect()
      
class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.clock = pygame.time.Clock()
      self.mouseX = 0
      self.mouseY = 0
      self.keys = None
      self.mouse_keys = (0,0,0)
      self.mouse_held = False
      
      self.images = ImageClass()

   def update(self):
      self.screen.fill((150,150,150))
      
      if self.images.rect.collidepoint((self.mouseY, self.mouseX)) and self.mouse_held:
         self.images.rect[1] = self.mouseY - 25
         self.images.rect[0] = self.mouseX - 25

      
      self.screen.blit(self.images.image, (self.images.rect[1],self.images.rect[0]))
      

      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
               self.mouse_held = True
            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
               self.mouse_held = False

         self.keys = pygame.key.get_pressed()
         self.mouseY, self.mouseX = pygame.mouse.get_pos()
         self.mouse_keys = pygame.mouse.get_pressed()

         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()


also i was going to use a mask, but i couldnt find how to check if the mouse is in the mask. Everything i found was mask overlapping another mask?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: creating a smooth drag and drop feature in pygame

Postby metulburr » Tue Apr 23, 2013 2:56 am

OK i figured out what i need to to be able to do a smooth drag and drop, but now i am confused on why the mouse does not grab the lower portion of the rect, but can grab nothing out in space on the right side of rect. I could only assume that somewhere i mixed up assigning x to y, or y to x. I dont see it.

Code: Select all

import pygame
import os
import time


class ImageClass:
   def __init__(self):
      self.size = (50,250)
      self.image = pygame.Surface(self.size).convert_alpha()
      self.image.fill((255,255,0))
      self.mask = pygame.mask.from_surface(self.image)
      self.rect = self.image.get_rect()
      
class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.clock = pygame.time.Clock()
      self.mouseX = 0
      self.mouseY = 0
      self.keys = None
      self.mouse_keys = (0,0,0)
      self.mouse_held = False
      self.drag = False
      
      self.images = ImageClass()

   def update(self):
      self.screen.fill((150,150,150))
      
      if self.images.rect.collidepoint((self.mouseX, self.mouseY)) and self.mouse_held:
         self.drag = True
      if self.drag: #keep moving box if mouse is still holding but movees too fast
         self.images.rect[1] = self.mouseY - self.images.size[0] // 2
         self.images.rect[0] = self.mouseX# - self.images.image.get_width() // 2
         print(self.images.rect)
         print(self.mouseY)
      self.screen.blit(self.images.image, (self.images.rect[1],self.images.rect[0]))
      

      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
               self.mouse_held = True
            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
               self.mouse_held = False
               self.drag = False

         self.keys = pygame.key.get_pressed()
         self.mouseY, self.mouseX = pygame.mouse.get_pos()
         self.mouse_keys = pygame.mouse.get_pressed()

         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: creating a smooth drag and drop feature in pygame

Postby metulburr » Tue Apr 23, 2013 12:28 pm

ok nevermind, i just mixed up some x, y coords

ending code is:
Code: Select all

import pygame
import os


class ImageClass:
   def __init__(self):
      self.size = (50, 250)
      self.image = pygame.Surface(self.size).convert_alpha()
      self.image.fill((255,255,0))
      #self.mask = pygame.mask.from_surface(self.image)
      self.rect = self.image.get_rect()
      
      
class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.clock = pygame.time.Clock()
      self.mouseX = 0
      self.mouseY = 0
      self.keys = None
      self.mouse_keys = (0,0,0)
      self.mouse_held = False
      self.is_dragging = False
      
      self.images = ImageClass()

   def update(self):
      self.screen.fill((150,150,150))
      if self.images.rect.collidepoint((self.mouseX, self.mouseY)) and self.mouse_held:
         self.is_dragging = True
      if self.is_dragging: #keep moving box if mouse is still holding but movees too fast
         self.images.rect[1] = self.mouseY - self.images.size[1] // 2
         self.images.rect[0] = self.mouseX - self.images.size[0] // 2

      self.screen.blit(self.images.image, (self.images.rect[0],self.images.rect[1]))
      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
               self.mouse_held = True
            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
               self.mouse_held = False
               self.is_dragging = False

         self.keys = pygame.key.get_pressed()
         self.mouseX, self.mouseY = pygame.mouse.get_pos()
         self.mouse_keys = pygame.mouse.get_pressed()

         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] creating a smooth drag and drop feature in pyga

Postby metulburr » Tue Apr 23, 2013 12:56 pm

oh i am dumb, i didnt even look at your @Mekire drag and drop example until after i finished the one i made, servers me right ofr not checking first
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: Baldyr, jonin and 2 guests