python - Pygame:点击事件正在影响它们左侧的每个 Sprite

标签 python pygame sprite

我有一个数组,其中包含所有 Sprite ,在循环中,如果单击 Sprite ,则 Sprite 会被杀死。

如果我从左到右单击 Sprite ,一切都很好。它们是相互独立地一一清除的。但是,如果我单击屏幕最右侧的 Sprite ,它会杀死其左侧的所有内容。

完整代码,因为我不知道重要区域可能在哪里。

import pygame
from pygame.locals import *
import random
pygame.init()
level = 1
cooldown = 1000
class test(pygame.sprite.Sprite):
    def __init__(self, w):
        super().__init__()
        self.image = pygame.Surface([600,600])
        self.rect = self.image.get_rect()
        self.image = pygame.image.load("index.png")
        self.image = pygame.transform.scale(self.image, (w,w))
        self.last = pygame.time.get_ticks()
screen = pygame.display.set_mode([600,600])
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill([0,0,0])
rX = random.randint(0, 500)
rX2 = random.randint(0, 500) 
screen.blit(background, (0,0))
sp = []
all_sprites_list = pygame.sprite.Group()
a_1 = test(random.randint(40,60))
a_1.rect.x = rX
a_1.rect.y = -400
sp.append(a_1)
all_sprites_list.add(a_1)

#The steps for a_1 is repeated to make variables a_2, a_3, a_4...  

Running = True
while Running:
    now = pygame.time.get_ticks()
    for x in sp:
        x.rect.y+=level
    m_pos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT:
            Running = False
        if event.type == MOUSEBUTTONDOWN:
            for s in sp:
                if s.rect.collidepoint(m_pos):
                    s.kill()
    pygame.display.flip()
    all_sprites_list.clear(screen, background)
    all_sprites_list.draw(screen)
    all_sprites_list.update()
    clock.tick(60)

最佳答案

问题是因为在类里面您创建了尺寸为 (600, 600)Surface 并将该尺寸分配给 self.rect。之后,您加载图像,但忘记将其大小分配给 self.rect,因此程序始终检查大小为 (600, 600) 的鼠标单击,以及当您单击最右边的项目时那么所有矩形 (600, 600) 都在点击区域内。

但是如果加载图像,则不必创建 Surface

<小时/>

我的版本有很多更改。

我删除了列表 sp 因为我可以使用 all_sprites_list

当它发送 MOUSEBUTTONDOWN 事件时,鼠标位置位于 event.pos 中,所以我不需要 pygame.mouse.get_pos()

import pygame
import random

# --- classes --- (UpperCaseNames)

class Test(pygame.sprite.Sprite):

    def __init__(self, w):
        super().__init__()

        self.image = pygame.image.load("index.png")
        self.image = pygame.transform.scale(self.image, (w, w))
        self.rect = self.image.get_rect()

        self.last = pygame.time.get_ticks()

# --- main ----

level = 1
cooldown = 1000

# - init -

pygame.init()
screen = pygame.display.set_mode((600, 600))

# - items -

background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))

screen.blit(background, (0,0))

all_sprites_list = pygame.sprite.Group()

for x in range(10):
    item = Test(random.randint(40, 60))
    item.rect.x = 40 * random.randint(0, 10)
    item.rect.y = 0
    all_sprites_list.add(item)

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    now = pygame.time.get_ticks()

    for item in all_sprites_list:
        item.rect.y += level

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            for item in all_sprites_list:
                if item.rect.collidepoint(event.pos):
                    item.kill()

    pygame.display.flip()
    all_sprites_list.clear(screen, background)
    all_sprites_list.draw(screen)
    all_sprites_list.update()
    clock.tick(30)

pygame.quit()    

关于python - Pygame:点击事件正在影响它们左侧的每个 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47822233/

相关文章:

python - Django 绝对导入

python数据帧写入R数据格式

python - (Python贪吃蛇游戏)for循环不画蛇

css - 需要两个 block 在文本的右侧对齐 - 可能吗?

CSS 自定义 Sprite

Python 排序问题

python - 在 Python 中静默打印 PDF

python - Pygame:为什么我无法绘制圆形 Sprite

python - PygameRaspberry Pi失败

xna - 动态更改或销毁 Texture2D 以进行绘图和碰撞检测