【问题标题】:Questions regarding pygame image.load function关于 pygame image.load 函数的问题
【发布时间】:2020-04-04 03:58:48
【问题描述】:

大家好,这里是普通的教科书新手,所以我最近看完了这个甜蜜的教程 https://www.youtube.com/watch?v=rfscVS0vtbw 并决定直接进入 pygame :D

问题如下,我有一个文件夹,我想从中抓取一些图像,并且有很多(30-40)个使用 pygame.image.load 函数(需要它们作为我的游戏的精灵),它们在不同的我的 python 文件中的目录,例如我的 python 游戏在“C:\PythonProjects\Programs\Pygame2.py”中,图像在“C:\Users\user\Documents\Generic game”中

我试着用谷歌搜索,显然你必须像这样复制整个目录路径来加载每个目录? (pygame.image.load("C:/Users/user/Documents/Le game/BG.jpg") 并且想知道是否有一种方法可以更快地做到这一点,比如我有一个包含 100 多张图片的项目......

非常感谢您花时间提供帮助

这是我的代码供参考(结构很混乱,但一旦我发现这个问题,我会改变一些东西):

import pygame

pygame.init()

win = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Potato ultra")
BG = pygame.image.load("C:/Users/user/Documents/Generic game/BG.jpg")
walkRight = [#need to use pygame.image.load function for images here]
walkLeft = [#Same case]

SW = 500
x = 0
y = 480
width = 64
height = 64
vel = 20
isJump = False
JumpCount = 10

def redrawGameWindow():

    win.blit(BG, (0,0))
    pygame.draw.rect(win, (0, 200, 0), (x, y, width, height))
    pygame.display.update()


run = True
while run:
    pygame.time.delay(50)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    if keys[pygame.K_RIGHT] and x < SW - width:
        x += vel
    if not isJump:
        if keys[pygame.K_UP] and y > 0:
            y -= vel
            if y < 0:
                y = 0
        if keys[pygame.K_DOWN] and y < SW - vel:
            y += vel
            if y > 436:
                y = 436
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if JumpCount >= -10:
            y -= (JumpCount * 4)
            if y < 0:
                y = 0
            JumpCount -= 2
        else:
            isJump = False
            JumpCount = 10

    redrawGameWindow()

pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    好吧,首先你可以有一个变量让它看起来更干净

    path = "C:/Users/user/Documents/Generic game/"
    bg = pygame.image.load(path + "BG.jpg")
    

    对于walkRightwalkLeft,我假设它们是动画,因此您可以执行for 循环。其中名称是 img1.jpg、img2.jpg、img3.jpg...

    for i in range(num_of_imgs):
        Dir = path + "img" + str(i) + ".jpg"
        walkRight.append(pygame.image.load(Dir))
    

    或一行

    walkRight = [pygame.image.load(path + "img" + str(i) + ".jpg") for i in range(num_of_img)]
    

    另一种方法是使用os.listDir

    filename = os.listDir("C:/Users/user/Documents/Generic game/")
    

    【讨论】:

      【解决方案2】:

      您可以在加载图像之前使用 glob。

      import glob
      imageList=glob.glob("C:/Users/user/Documents/Generic game/*.jpg")
      

      现在,您有一个要迭代的图像列表,如下所示。

      for each_image in imageList:
          BG = pygame.image.load(each_image)
      

      【讨论】:

        【解决方案3】:

        您可以有一个名为 directory 的字符串并将其连接到您加载的每个图像路径:

        directory = "C:/Users/user/Documents/Generic game/"
        BG = pygame.image.load(directory+"BG.jpg")
        

        如果您要加载大量图像,您可以有一个文件名列表并循环访问它们以创建一个新的图像列表:

        image_names = ["BG.jpg", "example.jpg", "example.jpg"]
        images = []
        for image in image_names:
            images.append(pygame.image.load(directory+image))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-13
          相关资源
          最近更新 更多