python-3.x 为什么不加载图像?

mqkwyuun  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(161)

由于某种原因马里奥图像不会加载在游戏中forest.png是背景,如果你要运行和mario.png是字符我已经做了一些测试,但无论它保持不加载这也是从技术与蒂姆教程的游戏,我按照它一行一行,但它仍然不工作(没有错误,它只是不加载字符)

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

gjmwrych1#

在更新显示之前,您必须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,则根本不绘制任何内容。

相关问题