我正在尝试创建一个小而简单的游戏。但到目前为止我遇到了很多麻烦。(我对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()
我还需要能够以一种可以通过单击它们来删除块的方式来实现这一点。
对不起,如果这问得太多了:/