Pygame不能将图像Blit到矩形列表

时间:2017-11-29 23:18:44

标签: python pygame

我正在使用pygame,我认为这是一个非常巧妙的想法,以便在游戏开始时自动生成砖块。因此,我创建了一个函数,询问您想要生成多少块砖,然后为特殊位置的砖块创建“实体”并将它们分配给列表。然后,在while循环中,我尝试使用相应的图像进行blit。但是,我收到了一个我从未见过的错误。

Error Image

import pygame, sys
import random
import numpy
pygame.init()

def myPopulater(amountToMake, image, width, h):


    myBodies = []
    for i in range(amountToMake):
        r1 = numpy.random.randint(0, width)
        r2 = numpy.random.randint(0, h)
        myBodies = myBodies + image.get_rect()
        myBodies[i].x = r1
        myBodies[i].x = r2
    return myBodies

width = 500
h = 500

ball = pygame.image.load("c:\\python\\ball.png")
screen = pygame.display.set_mode((width, h))

myList = myPopulater(25, ball, width, h)
while (1):
        #WHENEVER SOMETHING HAPPENS
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)
        for i in range(0, 25):
            screen.blit(ball, myList[i])
        pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

我可以看到,您正在尝试将image.get_rect()的结果添加到您的myBodies列表中。

您应该使用list.append方法向list对象添加元素。

更改此行:

myBodies = myBodies + image.get_rect()

对此:

myBodies.append(image.get_rect())

这将解决您的错误。

相关问题