如何在 PyGame 中拖动 2 个以上的图像? [英] How can I drag more than 2 images in PyGame?
本文介绍了如何在 PyGame 中拖动 2 个以上的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码,但我想用其他 4 张图片制作它?
导入 pygame从 pygame.locals 导入 *pygame.display.init()屏幕 = pygame.display.set_mode((1143,677))img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")imgPos = img.get_rect(topleft = (20, 20))imgPos1 = img1.get_rect(topleft = (60, 20))current_image = 无左按钮 = 0而 1:对于 pygame.event.get() 中的 e:如果 e.type == 退出:pygame.quit()退出(0)如果 e.type == pygame.MOUSEBUTTONDOWN:如果 imgPos.collidepoint(e.pos):current_image = 0elif imgPos1.collidepoint(e.pos):current_image = 1别的:current_image = 无如果 e.type == MOUSEMOTION:如果 e.buttons[LeftButton]:rel = e.rel如果 current_image == 0:imgPos.x += rel[0]imgPos.y += rel[1]elif current_image == 1:imgPos1.x += rel[0]imgPos1.y += rel[1]屏幕填充(0)screen.blit(img, imgPos)screen.blit (img1, imgPos1)pygame.display.flip()pygame.time.delay(30)
所以这是我的代码,我想放 4 张图片,而不是只有两张图片,我想放更多图片,但它是一个布尔值,所以是 0 或 1
解决方案
创建一个包含 4 个图像的列表:
images = [img1, img2, img3, img4]
创建图像矩形列表:
img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))]
使用
导入pygame从 pygame.locals 导入 *pygame.display.init()屏幕 = pygame.display.set_mode((1143, 677))img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")图片 = [img1, img2, img3, img4]current_image = -1img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))]左按钮 = 0而 1:对于 pygame.event.get() 中的 e:如果 e.type == 退出:pygame.quit()退出(0)如果 e.type == pygame.MOUSEBUTTONDOWN:mouse_rect = pygame.Rect(e.pos, (1, 1))current_image = mouse_rect.collidelist(img_rects)如果 e.type == MOUSEMOTION:如果 e.buttons[LeftButton]:rel = e.rel如果 0 <= current_image <镜头(图像):img_rects[current_image].x += rel[0]img_rects[current_image].y += rel[1]屏幕填充(0)对于我在范围内(len(图像)):screen.blit(图像[i],img_rects[i])pygame.display.flip()pygame.time.delay(30)
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:\Users\ga-sa\Downloads\As.png")
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.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
解决方案
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:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.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)
这篇关于如何在 PyGame 中拖动 2 个以上的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文