Pygame:从列表中删除实体

时间:2013-09-23 13:46:00

标签: 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()

我还需要能够通过单击来删除块的方式来实现它。

很抱歉,如果这个问题太多了:/

2 个答案:

答案 0 :(得分:1)

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

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)

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

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()

答案 1 :(得分:0)

你做

blocklist = [block for block in blocklist if block not in remlist]

所以来自remlist的所有广告块都不在blocklist ...

for block in remlist:
    print 'killed it'

...所以你不能删除它们......

    blocklist.remove(block)

因此会抛出错误。只需删除最后一行,就应该停止错误。