python - Pygame:从列表中删除实体

标签 python pygame

我正在尝试创建一个小而简单的游戏。但到目前为止我遇到了很多麻烦。 (我对 pygame 还很陌生)

问题是从这段代码中提出的:

    #The blocks' code
    for block in blocklist:
        #Blocks Collide
        if any(block.rect.colliderect(x.rect) for x in blocklist if x is not block):
            x=(int(mse[0]) / 32)*32
            y=(int(mse[1]) / 32)*32
            blockpairs = itertools.combinations(blocklist,2) #2 for pairs
            remlist = frozenset(b2 for b1,b2 in blockpairs if b1.rect.colliderect(b2.rect))
            blocklist = [block for block in blocklist if block not in remlist]
            for block in remlist:
                print 'killed it'
                blocklist.remove(block)

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\samis_000\Desktop\blockgame.pyw", line 43, in <module>
    blocklist.remove(block)
ValueError: list.remove(x): x not in list

我不明白出了什么问题!

这是整个代码:

#Import required modules
import pygame
from pygame.locals import *
import itertools
pygame.init()
screen=pygame.display.set_mode((640,480),0)

#Define class for the blocks
class Block(object):

    sprite = pygame.image.load("dirt.png").convert_alpha()

    def __init__(self, x, y):
        self.rect = self.sprite.get_rect(top=y, left=x)

#Create the list for the blocks
blocklist = []

#Main Loop
while True:
    #Test for events
    for event in pygame.event.get():
        #Left mouse released event
        if event.type == pygame.MOUSEBUTTONUP:
            mse=pygame.mouse.get_pos()
            x=(int(mse[0]) / 32)*32
            y=(int(mse[1]) / 32)*32
            blocklist.append(Block(x,y))
        #Close button event
        if event.type == QUIT:
            exit()
    #The blocks' code
    for block in blocklist:
        #Blocks Collide
        if any(block.rect.colliderect(x.rect) for x in blocklist if x is not block):
            x=(int(mse[0]) / 32)*32
            y=(int(mse[1]) / 32)*32
            blockpairs = itertools.combinations(blocklist,2) #2 for pairs
            remlist = frozenset(b2 for b1,b2 in blockpairs if b1.rect.colliderect(b2.rect))
            blocklist = [block for block in blocklist if block not in remlist]
            for block in remlist:
                print 'killed it'
                blocklist.remove(block)
        #Display blocks
        screen.blit(block.sprite, block.rect)
    #Update the screen
    pygame.display.update()

我还需要能够通过单击 block 来删除 block 的方式来实现此功能。

抱歉,如果这个要求太多了:/

最佳答案

不知道你在这里尝试什么:

if any(block.rect.colliderect(x.rect) for x in blocklist if x is not block):
            x=(int(mse[0]) / 32)*32
            y=(int(mse[1]) / 32)*32
            blockpairs = itertools.combinations(blocklist,2) #2 for pairs
            remlist = frozenset(b2 for b1,b2 in blockpairs if b1.rect.colliderect(b2.rect))
            blocklist = [block for block in blocklist if block not in remlist]
            for block in remlist:
                print 'killed it'
                blocklist.remove(block)

如果您想通过单击鼠标来创建和删除 block ,请查看此示例(应该很容易理解):

import pygame
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,480))

class Block(object):
    sprite = pygame.image.load("dirt.png").convert_alpha()
    def __init__(self, x, y):
        # since x and y will be the mouse position,
        # let x and y be the center of the block
        self.rect = self.sprite.get_rect(centery=y, centerx=x)

blocklist = []

while True:
    # don't forget to clear the screen
    screen.fill((0, 0, 0))
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == QUIT: exit()
        if event.type == pygame.MOUSEBUTTONUP: 
            # get all blocks that "collide" with the current mouse position
            to_remove = [b for b in blocklist if b.rect.collidepoint(mouse_pos)]
            for b in to_remove:
                blocklist.remove(b)

            # if we didn't remove a block, we create a new one
            if not to_remove:
                blocklist.append(Block(*mouse_pos))

    for b in blocklist:
        screen.blit(b.sprite, b.rect)

    pygame.display.update()

关于python - Pygame:从列表中删除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18960914/

相关文章:

python - subprocess.communicate 仅在退出时读取

python - 如何在 Windows 10 中为 python 3.9 安装 pygame?

python - 为什么 MOD_SHIFT 事件键在 PYGAME Mac 中不起作用

python - Pygame (A bit racey) 游戏错误

python - pygame Sprite 和 pygame.display.flip() 导致工件

python - PyMongo Flask 应用程序缓慢

python - 从控制台将参数传递给导入模块中的函数

python - 如何从 JSON 响应中提取 HTML

python - 将数据和标签拆分为 pandas 数据框中的两个单独的列

Python 写 MIDI 文件