2013-09-23 27 views
1

我試圖創建一個小的,簡單的遊戲刪除的實體。但目前我遇到了很多麻煩。 (我是相當新的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() 

我還需要能夠在某種程度上,我可以通過單擊刪除塊來實現這一點。

很抱歉,如果這是過分的要求:/

+0

我不明白。 'blocklist'是不在'remlist'中的項目。然後你從'remlist'中取出一個塊並嘗試從'blocklist'中刪除它? –

+0

我從前一個線程得到了代碼,在這個線程中我問了同樣的問題,我也沒有明白。 –

+0

好吧,試着理解這個......因爲那裏有一個非常明顯的錯誤。你想學習Python嗎?這是做這件事的最好方法...... –

回答

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

終於有人明白了!但實際上此: X =(INT(MSE [0])/ 32)* 32 Y =(INT(MSE [1])/ 32)* 32 被要求...因爲它卡塊以32×32格。 謝謝:) –

0

你做

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

因此所有從remlist塊是blocklist不再...

for block in remlist: 
    print 'killed it' 

...所以你不能刪除它們......

blocklist.remove(block) 

因此引發錯誤。只要刪除最後一行,它應該停止錯誤。