Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

python-3.x - 为什么我在程序本地存储的PyGame图像无法加载?(Why are my PyGame images stored local to the program not loading?)

All images are within the folder of the game.

(所有图像都在游戏文件夹中。)

(using mac) I get no errors, but the images just dont load.

((使用mac)我没有收到任何错误,但图像没有加载。)

Just a plain white screen.

(只是一个纯白色的屏幕。)

I have tried everything and I may give up at this point.

(我已经尝试了一切,现在我可能会放弃。)

I am begging you to please, please, please help me!

(我求求您,请,请帮助我!)

My program is super long, and I know that what I am asking is insane, but I have been trying to fix this for over 2 weeks!

(我的程序超长,我知道我要问的是疯了,但是我已经尝试修复了2个多星期!)

I hope I have made a small mistake, and it isnt a larger issue!

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

Sorry about this!

(为此事道歉!)

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()


Thank you in advance!

(先感谢您!)

  ask by jetlikesferraris translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...