我試圖創建一個小的,簡單的遊戲刪除的實體。但目前我遇到了很多麻煩。 (我是相當新的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()
我還需要能夠在某種程度上,我可以通過單擊刪除塊來實現這一點。
很抱歉,如果這是過分的要求:/
我不明白。 'blocklist'是不在'remlist'中的項目。然後你從'remlist'中取出一個塊並嘗試從'blocklist'中刪除它? –
我從前一個線程得到了代碼,在這個線程中我問了同樣的問題,我也沒有明白。 –
好吧,試着理解這個......因爲那裏有一個非常明顯的錯誤。你想學習Python嗎?這是做這件事的最好方法...... –