1

我正在尝试创建一个小而简单的游戏。但到目前为止我遇到了很多麻烦。(我对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()

我还需要能够以一种可以通过单击它们来删除块的方式来实现这一点。

对不起,如果这问得太多了:/

4

2 回答 2

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()
于 2013-09-23T14:03:15.017 回答
0

你做

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

所以所有的块remlist都不再在blocklist......

for block in remlist:
    print 'killed it'

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

    blocklist.remove(block)

所以抛出一个错误。只需删除最后一行,它应该会停止错误。

于 2013-09-23T14:04:34.723 回答