【发布时间】: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()
【问题讨论】: