Dragging Objects

Goals

  • Use MOUSEBUTTONDOWN and MOUSEBUTTONUP to toggle the dragging state.

  • Set box location to the mouse position only if dragging.

Extra:

  • Snap box to a specific location or reset to previous location.

  • “Throw” the box using pygame.mouse.get_rel() to influence the box’s “speed”

Starter code

Pygame Template

Video Tutorial

What you need to do

  • Add the ability to drag a second box

  • Create a mini game that will drop smaller boxes from the sky and you must collect the small boxes with the same coloured “container box”.

Final Code

 1"""
 2PYGAME DRAGGING
 3
 4Use MOUSEBUTTONDOWN and MOUSEBUTTONUP to toggle the dragging state.
 5Set box location to the mouse position only if dragging.
 6
 7Extra
 8Snap box to a specific location or reset to previous location.
 9"Throw" the box using pygame.mouse.get_rel() to influence the box's "speed"
10"""
11import pygame
12from pygame.constants import MOUSEBUTTONDOWN, MOUSEBUTTONUP
13from pygame.locals import K_ESCAPE, KEYDOWN, QUIT
14from pygame.sprite import collide_rect
15
16pygame.init()
17
18WIDTH = 640
19HEIGHT = 480
20SIZE = (WIDTH, HEIGHT)
21
22screen = pygame.display.set_mode(SIZE)
23clock = pygame.time.Clock()
24
25
26# Create a rect to represent the box
27# Rects come with useful methods that will help us
28# Determine if the mouse point is over the box (.collidepoint()).
29# Also, Rects have a .center object that we can snap to the mouse
30# location when it is being dragged.
31box1 = pygame.Rect(50, 50, 75, 75)
32
33# Need a variable so our game loop can remember if we are currently
34# dragging the box. This is called a toggle. If box1_dragging is True,
35# set the center of our box to the mouse location.
36box1_dragging = False
37
38
39# If we want to drop the box into another box, like a sword into
40# a weapon slot, we can create a Rect object for the slot.
41weapon_slot = pygame.Rect(300, 200, 100, 100)
42# ---------------------------
43
44running = True
45while running:
46    # EVENT HANDLING
47    for event in pygame.event.get():
48        if event.type == KEYDOWN:
49            if event.key == K_ESCAPE:
50                running = False
51        elif event.type == QUIT:
52            running = False
53        elif event.type == MOUSEBUTTONDOWN:
54            # If the mouse button gets pressed and
55            # if the mouse location is within the
56            # box, then we toggle on dragging.
57            if box1.collidepoint(event.pos):
58                box1_dragging = True
59        elif event.type == MOUSEBUTTONUP:
60            # if we were dragging and we moved the box over
61            # the "weapon slot", then we move the box to
62            # the center of the item slot.
63            if box1_dragging is True and weapon_slot.colliderect(box1):
64                box1.center = weapon_slot.center
65                
66            # When the mouse goes up, we need to let go
67            # and thus, dragging needs to be set to false
68            box1_dragging = False
69    
70    # GAME STATE UPDATES
71    # Snap the box location to the mouse location, 
72    # ONLY IF we are currently dragging it.
73    if box1_dragging is True:
74        box1.center = pygame.mouse.get_pos()
75
76    # DRAWING
77    screen.fill((255, 255, 255))  # always the first drawing command
78    
79    # Draw the box and weapon slot.
80    pygame.draw.rect(screen, (0, 0, 0), box1)
81    pygame.draw.rect(screen, (0, 0, 0), weapon_slot, 3)
82
83    # Must be the last two lines
84    # of the game loop
85    pygame.display.flip()
86    clock.tick(30)
87    #---------------------------
88
89
90pygame.quit()