gpt4 book ai didi

python - 在 Pygame 中单击堆叠图像时出错

转载 作者:行者123 更新时间:2023-12-04 08:29:40 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Drag multiple sprites with different “update ()” methods from the same Sprite class in Pygame

(2 个回答)



How can I drag more than 2 images in PyGame?

(1 个回答)


9 个月前关闭。




我制作了一些单击和拖动图像以在某些时候堆叠的代码。最初点击几下时效果很好,但是当我将它多次堆叠在同一个位置时,它突然变得无法点击。我该如何修复它,如果你没问题,你能让它更紧凑吗?

import pygame
from pygame import *
import sys
import os

pygame.init()
s_width = 640
s_height = 480

screen = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption("TITLE")
bigfont = pygame.font.SysFont(None, 50)
smallfont = pygame.font.SysFont(None, 25)
title = bigfont.render("TITLE", True, (0,0,0))

current_path = os.path.dirname(__file__)
img_path = os.path.join(current_path, "images")

img_list = []
place_list = []
temp = []

class Img_Set:
def __init__(self, path, x_pos, y_pos):
self.image = pygame.image.load(path)
self.x_pos = x_pos
self.y_pos = y_pos
self.rect = self.image.get_rect()
self.rect.left = self.x_pos
self.rect.top = self.y_pos
img_list.append(self)
self.click = False
def offset(self):
self.offset_len_x = event.pos[0] - self.x_pos
self.offset_len_y = event.pos[1] - self.y_pos
def drag(self):
self.x_pos = event.pos[0] - self.offset_len_x
self.y_pos = event.pos[1] - self.offset_len_y
self.rect.left = self.x_pos
self.rect.top = self.y_pos

class Place_Set:
def __init__(self, path, x_pos, y_pos):
self.image = pygame.image.load(path)
self.x_pos = x_pos
self.y_pos = y_pos
self.rect = self.image.get_rect()
self.rect.left = self.x_pos
self.rect.top = self.y_pos
place_list.append(self)
self.click = False

img1 = Img_Set("images/80x80_blueball.png", s_width/3, s_height/3)
img2 = Place_Set("images/80x80_ball.png", s_width/2, s_height/3)
img3 = Place_Set("images/80x80_greenball.png", s_width/3*2, s_height/3)

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
for x in img_list:
if x.rect.collidepoint(event.pos):
temp.append(x)
if len(temp) > 0:
temp[-1].offset()
temp[-1].click = True
img_list.remove(temp[-1])
img_list.append(temp[-1])
temp = []
elif event.type == MOUSEMOTION:
for x in img_list:
if x.click:
x.drag()
elif event.type == MOUSEBUTTONUP:
for x in img_list:
for y in place_list:
if x.rect.colliderect(y.rect):
x.x_pos = y.x_pos
x.y_pos = y.y_pos
for x in img_list:
x.click = False
screen.fill((255, 255, 255))
for x in place_list:
screen.blit(x.image, (x.x_pos, x.y_pos))
for x in img_list:
screen.blit(x.image, (x.x_pos, x.y_pos))


pygame.display.update()
运行 gif 的代码在下面。
/image/vAQyU.gif

最佳答案

您错过了更新 rect属性,当鼠标按钮被释放时。拖动图像时,这会导致 x 之间的错误偏移, yrect属性:

while True:
for event in pygame.event.get():
# [...]

elif event.type == MOUSEBUTTONUP:
for x in img_list:
for y in place_list:
if x.rect.colliderect(y.rect):
x.x_pos = y.x_pos
x.y_pos = y.y_pos
x.rect.left = y.x_pos # <-- ADD
x.rect.top = y.y_pos # <-- ADD


但是,您不需要 xy属性。相反,使用保存在 rect 属性中的位置:
import pygame
from pygame import *
import sys
import os

pygame.init()
s_width = 640
s_height = 480

screen = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption("TITLE")
bigfont = pygame.font.SysFont(None, 50)
smallfont = pygame.font.SysFont(None, 25)
title = bigfont.render("TITLE", True, (0,0,0))

current_path = os.path.dirname(__file__)
img_path = os.path.join(current_path, "images")

img_list = []
place_list = []
temp = []

class Img_Set:
def __init__(self, path, x_pos, y_pos):
self.image = path # pygame.image.load(path)
self.rect = self.image.get_rect()
self.rect.left = x_pos
self.rect.top = y_pos
img_list.append(self)
self.click = False
def offset(self):
self.offset_len_x = event.pos[0] - self.rect.x
self.offset_len_y = event.pos[1] - self.rect.y
def drag(self):
self.rect.x = event.pos[0] - self.offset_len_x
self.rect.y = event.pos[1] - self.offset_len_y

class Place_Set:
def __init__(self, path, x_pos, y_pos):
self.image = path # pygame.image.load(path)
self.rect = self.image.get_rect()
self.rect.left = x_pos
self.rect.top = y_pos
place_list.append(self)
self.click = False

img1 = Img_Set("images/80x80_blueball.png", s_width/3, s_height/3)
img2 = Place_Set("images/80x80_ball.png", s_width/2, s_height/3)
img3 = Place_Set("images/80x80_greenball.png", s_width/3*2, s_height/3)

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
for x in img_list:
if x.rect.collidepoint(event.pos):
temp.append(x)
if len(temp) > 0:
temp[-1].offset()
temp[-1].click = True
img_list.remove(temp[-1])
img_list.append(temp[-1])
temp = []
elif event.type == MOUSEMOTION:
for x in img_list:
if x.click:
x.drag()
elif event.type == MOUSEBUTTONUP:
for x in img_list:
for y in place_list:
if x.rect.colliderect(y.rect):
x.rect.x = y.rect.x
x.rect.y = y.rect.y
for x in img_list:
x.click = False
screen.fill((255, 255, 255))
for x in place_list:
screen.blit(x.image, x.rect)
for x in img_list:
screen.blit(x.image, x.rect)

pygame.display.update()

关于python - 在 Pygame 中单击堆叠图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65088657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com