【发布时间】:2021-08-08 18:52:56
【问题描述】:
由于某种原因,马里奥图像无法在游戏中加载 如果您要运行,forest.png 是背景 mario.png 是角色 我已经做了一些测试,但无论如何它总是无法加载 这也是 Tech With Tim 教程中的游戏,我逐行跟进 但它仍然不起作用 (没有错误它只是不加载字符)
import pygame
import os, sys
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("the game!!!")
APP_FOLDER = os.path.dirname(os.path.realpath(sys.argv[0]))
#background_image = pygame.image.load(r"C:\Users\NONAME\Desktop\Coding\Games\Learning\forest.png").convert()
background_image = pygame.image.load(os.path.join(APP_FOLDER, 'forest.png')).convert()
#mario = pygame.image.load(r"C:\Users\NO NAME\Desktop\Coding\Games\Learning\mario.png").convert()
R_mario = pygame.image.load(os.path.join(APP_FOLDER, 'mario.png')).convert()
L_mario = pygame.transform.flip(R_mario, True, False)
screenWidth = 500
x = 50
y = 425
width = 40
height = 60
vel = 10
isJump = False
jumpCount = 10
left = False
right = True
walkcount = 0
def redrawGameWindow():
global walkcount
win.blit(background_image, [0, 0])
pygame.display.update()
if left == True:
win.blit(L_mario, (x, y))
print("work left")
elif right == True:
win.blit(R_mario, (x, y))
print("work right")
#mainloop
run = True
while run:
pygame.time.delay(27)
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 > vel:
x -= vel
Left = True
right = False
elif keys[pygame.K_a] and x > vel:
x -= vel
right = True
left = False
else:
right = False
left = False
if keys[pygame.K_RIGHT] and x < screenWidth - width - vel:
x += vel
if keys[pygame.K_d] and x < screenWidth - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawGameWindow()
pygame.quit()
【问题讨论】:
-
正如我所说,没有错误消息,我在背景上使用了同样的东西,效果很好
-
没有字符不加载
-
如果需要我可以提供图片
-
当您取消注释完整路径的行并注释掉另一行时,图像是否加载?你确定图像在那里吗?
-
不,遗憾的是它没有
标签: python python-3.x pygame