-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmove_image.py
75 lines (56 loc) · 1.67 KB
/
move_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Python program to move the image
# with the mouse
# Import the library pygame
import pygame
from pygame.locals import *
# Take colors input
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
# Construct the GUI game
pygame.init()
# Set dimensions of game GUI
w, h = 1000, 1000
screen = pygame.display.set_mode((w, h))
# Take image as input
img = pygame.image.load(
'/Users/maxscullion/Projects/PygameChess/classic_hq/b_bishop.png')
img.convert()
img = pygame.transform.smoothscale(img, (100, 100))
# Draw rectangle around the image
rect = img.get_rect()
rect.center = w//2, h//2
# Set running and moving values
running = True
moving = False
# Setting what happens when game
# is in running state
while running:
for event in pygame.event.get():
# Close if the user quits the
# game
if event.type == QUIT:
running = False
# Making the image move
elif event.type == MOUSEBUTTONDOWN:
if rect.collidepoint(event.pos):
moving = True
# Set moving as False if you want
# to move the image only with the
# mouse click
# Set moving as True if you want
# to move the image without the
# mouse click
elif event.type == MOUSEBUTTONUP:
moving = False
# Make your image move continuously
elif event.type == MOUSEMOTION and moving:
rect.move_ip(event.rel)
# Set screen color and image on screen
screen.fill(YELLOW)
screen.blit(img, rect)
# Construct the border to the image
#pygame.draw.rect(screen, BLUE, rect, 1)
# Update the GUI pygame
pygame.display.update()
# Quit the GUI game
pygame.quit()