我正在使用 pygame,我认为在游戏开始时自动生成积木是一个非常好的主意。所以我做了一个函数,询问你想要生成多少块积木,然后在特殊位置为这些积木创建“实体”,并将它们分配到一个列表中。然后,在 while 循环中,我尝试使用相应的图像 blit 那些。但是,我收到了以前从未见过的错误。
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()
据我所知,您正试图将 的结果添加image.get_rect()
到您的myBodies
列表中。
您应该使用该list.append
方法向list
对象添加元素。
改变这一行:
myBodies = myBodies + image.get_rect()
对此:
myBodies.append(image.get_rect())
这将解决您的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句