来自列表的image.load

时间:2018-07-18 12:50:42

标签: python python-3.x pygame virtualbox ubuntu-18.04

我正在尝试使用列表随机选择图像,但出现此错误:

  

暴民=   随机(list [pygame.image.load(“ image1.png”),pygame.image.load(“ image2.png”),pygame.image.load(“ image3.png”),])   TypeError:“类型”对象不可下标

如果我在pygame.image.load(“ Image”)中手动加载图像,它可以工作,但是随后出现此错误。 Angryball_image_rect应该在

中定义
  

screen.blit(愤怒的球,angryball_image_rect.move(愤怒的图像_rect.width,0))   NameError:名称“ angryball_image_rect”未定义

import pygame
import os
import random

size = width, height = 750, 422
screen = pygame.display.set_mode(size)

img_path = os.path.join(os.getcwd())
background_image = pygame.image.load('background.jpg').convert()
bg_image_rect = background_image.get_rect()
pygame.mixer.pre_init(44100, 16, 2, 4096)

pygame.display.set_caption("BallGame")

class Ball(object):
    def __init__(self):
        self.image = pygame.image.load("ball.png")
        self.image_rect = self.image.get_rect()
        self.image_rect.x
        self.image_rect.y
        self.facing = 'LEFT'

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN] and self.image_rect.y < 321:
            self.facing = 'DOWN'
            self.image_rect.y += dist
        elif key[pygame.K_UP] and self.image_rect.y > 0:
                self.facing = 'UP'
        self.image_rect.y -= dist
        if key[pygame.K_RIGHT] and self.image_rect.x < 649:
           self.facing = 'RIGHT'
            self.image_rect.x += dist
       elif key[pygame.K_LEFT] and self.image_rect.x > 0:
            self.facing = 'LEFT'
            self.image_rect.x -= dist

    def draw(self, surface):
        if self.facing == "RIGHT":
            surface.blit(pygame.transform.flip(self.image, True, False),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "DOWN":
            surface.blit(pygame.image.load("ball_down.png"),(self.image_rect.x,self.image_rect.y))
        if self.facing == "UP":
            surface.blit(pygame.image.load("ball_up.png"),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "LEFT":
            surface.blit(self.image,(self.image_rect.x,self.image_rect.y))


#List of objects to randomly select the image of the Angryball
mob = random(list[pygame.image.load("image1.png"),pygame.image.load("image2.png"),pygame.image.load("image3.png"),])

class Angryball(object):
    def __init__(self):
        self.image = mob
        self.image_rect = self.image.get_rect()
        self.image_rect.x
        self.image_rect.y
        self.facing = 'LEFT'


pygame.init()
screen = pygame.display.set_mode((750, 422))

ball = Ball()
angryball = Angryball()

clock = pygame.time.Clock()

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1, 0.0)

running = True
while running:
    esc_key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if esc_key[pygame.K_ESCAPE]:
            pygame.display.quit()
            pygame.quit()
            running = False

    ball.handle_keys()

#Move the Angryball from right to left at speed 5 spawning randomly on the right border when it reaches the left border
    screen.blit(angryball, angryball_image_rect.move(angryball_image_rect.width, 0))
    angryball_image_rect = angryball.get_rect()
    angryball_image_rect.move_ip(-5, 0)
    if angryball_image_rect.x <= 0:
        angryball_image_rect.x = random.randint(101, 649) #101 is the size of the image so it wont be split at the border's edge

    screen.blit(background_image, bg_image_rect)
    screen.blit(background_image, bg_image_rect.move(bg_image_rect.width, 0))
    bg_image_rect.move_ip(-2, 0)
    if bg_image_rect.right <= 0:
        bg_image_rect.x = 0


    ball.draw(screen)
    pygame.display.update()

    clock.tick(60)

1 个答案:

答案 0 :(得分:1)

在全局范围内定义图像列表,并用图像填充它。然后,您可以调用random.choice来获取列表中的图像之一,并将其​​与随机坐标一起传递到sprite类的__init__方法中,您可以在其中将它们分配给sprite的特定属性

# Load the images once and put them into a global list. Then you don't have
# to load them from the hard disk again (which is inefficient) and you can
# use `random.choice` to pick one of the images. Also, call convert or
# convert_alpha to improve the blit performance.
mob_images = [
    pygame.image.load("image1.png").convert_alpha(),
    pygame.image.load("image2.png").convert_alpha(),
    pygame.image.load("image3.png").convert_alpha(),
    ]

mob_image = random.choice(mob_images)  # Pick a random image.


class Angryball(object):
    # The __init__ method now takes an image (pygame.Surface) and
    # the x- and y-position.
    def __init__(self, image, pos_x, pos_y):
        self.image = image
        self.image_rect = self.image.get_rect()
        self.image_rect.x = pos_x
        self.image_rect.y = pos_y
        self.facing = 'LEFT'

# Pass the random image and the coordinates to the `__init__` method.
angryball = Angryball(mob_image, 700, random.randrange(400))

引发TypeError是因为您试图用方括号(用于索引)调用list

list[1, 2, 3]  # Won't work, because square brackets are used for indexing.

list([1, 2, 3])  # This works but is unnecessary, because it's already a list.