首页 > 解决方案 > 图像不会在 Pygame 中加载,但 Python IDLE 中没有错误

问题描述

无论我做什么,我的图像都不会加载到 pygame 中。我尝试使用带有正斜杠和反斜杠的绝对路径来制作图像。在 pygame 中,屏幕只是加载并且没有给我任何错误,但图像也没有加载。

这是代码:

import pygame
window = pygame.display.set_mode((1000,1000))

BGImage = pygame.image.load('Plat.jpg')
window.blit(BGImage(0,0))

Eggshell = (240,235,220)

vel = 15
x = 3
y = 450
width = 50
height= 60




isJump = False
jumpCount = 10

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run == False


    pressed = pygame.key.get_pressed()


    if pressed[pygame.K_LEFT] and x  > vel:
        x-= vel
    if pressed[pygame.K_RIGHT] and x < 920 :
        x+=vel
    if not (isJump):
        if pressed[pygame.K_UP] and y > vel:
            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


    window.fill((0,0,0))
    pygame.draw.rect(window,Eggshell,(x,y,width,height))
    pygame.display.update()


pygame.quit()

标签: pythonimagepygame

解决方案


pygame.Surface使用该fill方法清除图像后,您需要将图像/ s blit到显示表面上。如果BGImage覆盖了整个屏幕,则不需要在对图像进行 blit 之前填充屏幕。

window.fill((0, 0, 0))
window.blit(BGImage, (0, 0))  # Blit the image at the top left coords (0, 0).

我还建议使用该convert方法转换图像(或者convert_alpha如果图像具有透明部分),因为这将大大提高 blit 性能。

BGImage = pygame.image.load('Plat.jpg').convert()

推荐阅读