简体   繁体   中英

Pygame Cannot Blit An Image To A List Of Rectangles

I am working with pygame, and I thought it'd be a super neat idea to make it so that the bricks were automatically generated at the beginning of the game. So I made a function that asks for how many bricks you want generated, and then creates the "bodies" for those bricks in special locations and assigns them to a list. Then, in the while loop, I try to blit those with the according image. However, I get an error I have never seen before.

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

From what I can see, you're trying to add the result of image.get_rect() to your myBodies list.

You should use the list.append method to add an element to a list object.

Change this line:

myBodies = myBodies + image.get_rect()

To this:

myBodies.append(image.get_rect())

That will fix your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM