【问题标题】:Why are my PyGame images stored local to the program not loading?为什么我的 PyGame 图像存储在程序本地而不加载?
【发布时间】:2019-12-01 16:56:50
【问题描述】:

所有图片都在游戏文件夹中。 (使用麦克) 我没有错误,但图像只是不加载。只是一个纯白的屏幕。

我已经尝试了一切,我可能会在这一点上放弃。我求你了,求求你,请帮助我!

我的程序超长,我知道我的要求很疯狂,但我已经尝试解决这个问题超过 2 周了!

我希望我犯了一个小错误,这不是一个更大的问题!

对不起!

import pygame
pygame.init()



display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Hack Attack')
clock = pygame.time.Clock()



charUp = pygame.image.load('StandUp.png') #Can also be used for down
charDown = pygame.image.load('StandUp.png')
charLeft = pygame.image.load('StandLeft.png') #Can also be used for right
charRight = pygame.image.load('StandLeft.png')
UpFor1 = pygame.image.load('Upfor1.png') #Can also be used for Down Forward 2
DownFor2 = pygame.image.load('Upfor1.png')
UpFor2 = pygame.image.load('UpFor2.png')
DownFor1 = pygame.image.load('UpFor2.png')
LeftFor1 = pygame.image.load('LeftFor1.png')
RightFor2 = pygame.image.load('LeftFor1.png')
RightFor1 = pygame.image.load('LeftFor2.png')
LeftFor2 = pygame.image.load('LeftFor2.png')

UpCount = 0
LeftCount = 0
RightCount = 0
DownCount = 0  





character = pygame.image.load('StandUp.png')








x = (display_width * 0.5)
y = (display_height * 0.5)

def gameLoop():
    global character
    global UpCount
    global DownCount
    global LeftCount
    global RightCount
    global y
    global x
    global x_change
    global y_change


    x_change = 0
    y_change = 0





    crashed = False

    while not crashed:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True
            if event.type == pygame.KEYDOWN:


                if event.key == pygame.K_a:
                    LeftCount += 1
                    x_change = -5
                    character = charLeft

                    if LeftCount % 2 == 0:
                        x = LeftCount/4
                        try:

                            x = int(x)
                        except ValueError:
                            character = LeftFor1
                        else:
                            character = LeftFor2
                    else:
                        character = charLeft





                if event.key == pygame.K_s:
                    y_change = -5
                    character = charRight

                    DownCount += 1
                    character = charDown

                    if DownCount % 2 == 0:
                         x = DownCount/4
                         try:
                            x = int(x)

                         except ValueError:
                            character = DownFor1
                         else:
                            character = DownFor2
                         pass


                    else:
                        character = charDown

                if event.key == pygame.K_w:
                    y_change = 5
                    character = charRight
                    UpCount += 1
                    character = charUp
                    x = UpCount
                    if DownCount % 2 == 0:
                        x = UpCount/4
                        try:
                            x = int(x)
                        except ValueError:
                            character = UpFor1
                        else:
                            character = UpFor2
                    else:
                        character = charUp

                if event.key == pygame.K_d:
                    x_change = 5
                    character = charRight

                    RightCount += 1
                    character = charRight
                    x = RightCount/4
                    if DownCount % 2 == 0:
                        x = RightCount/4
                        try:
                            x = int(x)
                        except ValueError:
                            character = UpFor1
                        else:
                            character = UpFor2
                    else:
                        character = charUp






            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    x_change = 0
                    y_change = 0


        x = x + x_change
        y = y + y_change

        white = [255, 255, 255]


        gameDisplay.fill(white)
        gameDisplay.blit(character,(x,y))
        print('x ' + str(x) + ' y ' + str(y))
        pygame.display.flip()
        clock.tick(20)

gameLoop()
pygame.quit()
quit()


提前谢谢你!

【问题讨论】:

  • 您可以将其中一张图片附加到问题中吗?也许 .PNG 中有一些奇怪的东西?

标签: python-3.x pygame


【解决方案1】:

您能否尝试检查您的角色图像是否显示在游戏窗口的边界之外,例如将 x 和 y 变量设置为 400 和 300,这样您就知道您的图像将被放置在游戏窗口的中心窗口。

【讨论】:

    【解决方案2】:

    尝试导入它... 从 pygame.locals 导入 *

    我不确定它是否有效,但尝试一次......

    【讨论】:

    • 如果仍然无法正常工作,请尝试将所有图像放在另一个文件夹中,并通过导入 os 并通过 pygame.image.load(os.path.join('folder name','image_name ')).convert()
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2021-11-20
    相关资源
    最近更新 更多