Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - How can I drag more than 2 images in PyGame?

i have this code but i wanna make this with other 4 images?

 import pygame
 from pygame.locals import *

 pygame.display.init()
 screen = pygame.display.set_mode((1143,677 ))

img = pygame.image.load(r"C:Usersga-saDownloadsAs.png")
img1 = pygame.image.load(r"C:Usersga-saDownloads3.png")
imgPos = img.get_rect(topleft = (20, 20))
imgPos1 = img1.get_rect(topleft = (60, 20))
current_image = None

 LeftButton = 0
while 1:
for e in pygame.event.get():
    if e.type == QUIT:
        pygame.quit()
        exit(0)

    if e.type == pygame.MOUSEBUTTONDOWN:
        if imgPos.collidepoint(e.pos):
            current_image = 0
        elif imgPos1.collidepoint(e.pos):
            current_image = 1
        else: 
            current_image = None

    if e.type == MOUSEMOTION:
        if e.buttons[LeftButton]:
            rel = e.rel
            if current_image == 0:
                imgPos.x += rel[0]
                imgPos.y += rel[1]
            elif current_image == 1:
                imgPos1.x += rel[0]
                imgPos1.y += rel[1]

screen.fill(0)
screen.blit(img, imgPos)
screen.blit (img1, imgPos1)
pygame.display.flip()
pygame.time.delay(30)

so this my code i want to put 4 image , instead only two images , i would like to put more images but is a boolean so is 0 or 1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a list of 4 images:

images = [img1, img2, img3, img4]

Create a list of image rectangles:

img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))]

Use pygame.Rect.collidelist to find the image which is clicked:

if e.type == pygame.MOUSEBUTTONDOWN:
    mouse_rect = pygame.Rect(e.pos, (1, 1))
    current_image = mouse_rect.collidelist(img_rects)

Draw the current_image:

if e.type == MOUSEMOTION:
    if e.buttons[LeftButton]:
        rel = e.rel
        if 0 <= current_image < len(images):
            img_rects[current_image].x += rel[0]
            img_rects[current_image].y += rel[1]

Draw the images in a loop:

for i in range(len(images)):
    screen.blit(imgages[i], img_rects[i])

Minimal example:

import pygame
from pygame.locals import *

pygame.display.init()
screen = pygame.display.set_mode((1143, 677))

img1 = pygame.image.load(r"C:Usersga-saDownloadsAs.png")
img2 = pygame.image.load(r"C:Usersga-saDownloads3.png")
img3 = pygame.image.load(r"C:Usersga-saDownloadsAs.png")
img4 = pygame.image.load(r"C:Usersga-saDownloads3.png")

images = [img1, img2, img3, img4]

current_image = -1
img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))]

LeftButton = 0
while 1:
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            exit(0)

        if e.type == pygame.MOUSEBUTTONDOWN:
            mouse_rect = pygame.Rect(e.pos, (1, 1))
            current_image = mouse_rect.collidelist(img_rects)

        if e.type == MOUSEMOTION:
            if e.buttons[LeftButton]:
                rel = e.rel
                if 0 <= current_image < len(images):
                    img_rects[current_image].x += rel[0]
                    img_rects[current_image].y += rel[1]

    screen.fill(0)
    for i in range(len(images)):
        screen.blit(images[i], img_rects[i])
    pygame.display.flip()
    pygame.time.delay(30)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...