-1

我正在尝试在 Pygame 中编写一个抄袭“Brick Breaker”游戏。但我目前被卡住了,不知道该怎么做。

(等等等等,更清楚地解释场景,等等等等,清晰,清晰的场景)

(我需要更多随机文本,以便发布所有这些代码)

这是所有代码,(是的,全部):

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

请帮忙。

4

2 回答 2

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)

这消除了需要level1rects- 它导致了太多问题,因为您从矩形列表中删除了项目,而没有从块列表中删除相应的块。将第一个循环更改为for b in level1blocks:即使块消失也允许代码工作。

于 2013-01-06T01:43:45.850 回答
-1

在 level1blocks 和 level1rects 列表上执行 print 或 pprint,其中一个没有足够的成员用于 range(2) 迭代器,它将尝试访问每个列表的第一个(索引 0)和第二个(索引 1)成员。

于 2013-01-06T01:36:12.637 回答