【问题标题】:Why doesnt the image load? [duplicate]为什么图片加载不出来? [复制]
【发布时间】: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


【解决方案1】:

您必须在更新显示之前blit 字符:

def redrawGameWindow():
    global walkcount

    win.blit(background_image, [0, 0])
    
    if left == True:
        win.blit(L_mario, (x, y))
        print("work left")
    elif right == True:
        win.blit(R_mario, (x, y))
        print("work right")

    pygame.display.update()

但请注意,如果 right == Falseleft == False,则根本不会绘制任何内容。

【讨论】:

  • 辛苦了,非常感谢!!!!
猜你喜欢
  • 2021-09-26
  • 2021-02-23
  • 2016-04-22
  • 1970-01-01
  • 2020-02-25
  • 2020-07-14
相关资源
最近更新 更多