2013-01-06 99 views
-1

我想在Pygame中寫一個關於「Brick Breaker」的遊戲。但我目前陷入困境,不知道該怎麼做才知道。ValueError:list.remove(x):x不在列表中〜Pygame(Python)

(等等等等等等,解釋的情況更加清楚,等等等等,清晰,明瞭的情況)

(我需要更多的隨機文本,以便我可以發佈這些代碼)

這裏是所有的代碼,(是全部的話):

import pygame, sys, time, random 
from pygame.locals import * 
pygame.init() 
fpsclock = pygame.time.Clock() 

WINDOWWIDTH = 450 
WINDOWHEIGHT = 650 
mainwindow = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) 
pygame.display.set_caption('Luzion - Brick Breaker') 

paddle = pygame.image.load('Brick Breaker - Paddle.png') 
paddlerect = paddle.get_rect() 
paddlerect.topleft = (190, 575) 

ball = pygame.image.load ('ball.png') 
ballrect = ball.get_rect() 
ballrect.topleft = (195, 565) 

cooltext = pygame.image.load('cooltext1.png') 
cooltextrect = cooltext.get_rect() 
cooltextrect.topleft = (0, 0) 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 128, 0) 
BLUE = (0, 0, 255) 
LIME = (0, 255, 0) 
TEXTCOLOR = WHITE 

font = pygame.font.SysFont(None, 48) 

def displaytext(text, font, surface, x, y): 
    text = font.render(text, 1, TEXTCOLOR) 
    textrect = text.get_rect() 
    textrect.topleft = (x, y) 
    surface.blit(text, textrect) 

def waitforplayer(): 
    while True: 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      if event.type == KEYDOWN: 
       if event.key == K_ESCAPE: 
        pygame.quit() 
        sys.exit() 
       return 

moveleft = False 
moveright = False 
SPEED = 7 

bmoveup = bmovedown = bmoveleft = bmoveright = False 
BALLSPEED = 8 

mainwindow.blit(cooltext, cooltextrect) 
pygame.display.update() 
time.sleep(1) 

displaytext('Level 1', font, mainwindow, 150, 100) 
pygame.display.update() 
time.sleep(1) 

displaytext('Press any key to begin...', font, mainwindow, 22, 200) 
pygame.display.update() 
waitforplayer() 

while True: 

    rb = pygame.image.load('redblock.png') 
    rbrect = rb.get_rect() 
    rbrect.topleft = (0, 0) 

    rb1 = rb 
    rb1rect = rb1.get_rect() 
    rb1rect.topleft = (40, 0) 

    level1blocks = [rb, rb1] 
    level1rects = [rbrect, rb1rect] 

    number = random.randint(0, 1) 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == KEYDOWN: 
      if event.key == ord('a') or event.key == K_LEFT: 
       moveleft = True 
       moveright = False 
      if event.key == ord('d') or event.key == K_RIGHT: 
       moveleft = False 
       moveright = True 
      if event.key == ord('g'): 
       bmoveup = True 
       if number == 1: 
        bmoveleft = True 
       else: 
        bmoveright = True 
     if event.type == KEYUP: 
      if event.key == ord('a') or event.key == K_LEFT: 
       moveleft = False 
      if event.key == ord('d') or event.key == K_RIGHT: 
       moveright = False 

    if moveleft and paddlerect.left > 0: 
     paddlerect.left -= SPEED 
    if moveright and paddlerect.right < WINDOWWIDTH: 
     paddlerect.right += SPEED 

    if bmovedown and ballrect.bottom < WINDOWHEIGHT: 
     ballrect.top += BALLSPEED 
    if bmoveup and ballrect.top > 0: 
     ballrect.top -= BALLSPEED 
    if bmoveleft and ballrect.left > 0: 
     ballrect.left -= BALLSPEED 
    if bmoveright and ballrect.right < WINDOWWIDTH: 
     ballrect.right += BALLSPEED 

    if ballrect.top <= 0: 
     bmovedown = not bmovedown 
     bmoveup = not bmoveup 
    if ballrect.left <= 0: 
     bmoveleft = not bmoveleft 
     bmoveright = not bmoveright 
    if ballrect.right >= WINDOWWIDTH: 
     bmoveleft = not bmoveleft 
     bmoveright = not bmoveright 
    if ballrect.bottom >= WINDOWHEIGHT: 
     bmovedown = not bmovedown 
     bmoveup = not bmoveup 

    mainwindow.fill(WHITE) 
    mainwindow.blit(paddle, paddlerect) 
    mainwindow.blit(ball, ballrect) 

    for x in range(len(level1blocks)): 
     mainwindow.blit(level1blocks[x], level1rects[x]) 

    for x in level1rects: 
     if ballrect.colliderect(x): 
      level1rects.remove([x]) 
      level1blocks.remove([x]) 

    if ballrect.colliderect(paddlerect): 
     bmovedown = not bmovedown 
     bmoveup = not bmoveup 
     bmoveleft = not bmoveleft 
     bmoveright = not bmoveright 




    pygame.display.update() 
    fpsclock.tick(35) 

這裏是我的錯誤:

Traceback (most recent call last): 
    File "C:/Python32/Luzion - Brick Breaker", line 144, in <module> 
    level1rects.remove([x]) 
ValueError: list.remove(x): x not in list 

請幫忙。

+0

'class mainwindow'是什麼樣的? – hd1

+2

這很奇怪。你可以在第141行之前向程序添加'print x,len(level1blocks),len(level1rects)'行並告訴我們它說了什麼? –

+0

這是真正的代碼,還是你削減了一些?我可以看到一個'level1rects.remove(x)'這讓我感到驚訝。 –

回答

4

正如您在評論中提到的,兩個for循環都在較大的while循環內。這意味着,當線

level1rects.remove(x) 

發生,它會降低的level1rects尺寸並引起level1rects[x](當x是1)引發異常。

解決這個問題的最好辦法是for循環更改爲以下內容:

for b in level1blocks: 
    mainwindow.blit(b, b.get_rect()) 

for x in level1blocks[:]: 
    if ballrect.colliderect(x.get_rect()): 
     level1blocks.remove(x) 

這obliviates需要level1rects - 它造成太多的問題,因爲你是從矩形的列表中刪除項目而無需從塊列表中刪除相應的塊。將第一個循環更改爲for b in level1blocks:,即使代碼塊消失,代碼也可以工作。

+0

哦,上帝,我現在感覺很糟糕。我一定沒有仔細閱讀你所說的內容,我的意思是代碼在一個較大的while循環中。循環不會更大。我的錯。 – user1952081

+0

@ user1952081:沒有理由,這是關於在你的問題中提供什麼樣的代碼的好教訓。歡迎來到StackOverflow! –

+0

@ user1952081:無論它是較大的循環還是較大的while循環都沒有區別。請嘗試更改並查看它是否有效。 –

-1

在level1blocks和level1rects列表上執行print或pprint,其中一個對於range(2)迭代器沒有足夠的成員,它將嘗試訪問第一個(索引0)和第二個(索引1)成員的每個列表。

相關問題