当前位置: 代码迷 >> python >> 图片不会在Pygame中加载,但Python IDLE中没有错误
  详细解决方案

图片不会在Pygame中加载,但Python IDLE中没有错误

热度:47   发布时间:2023-06-21 11:01:26.0

无论我做什么,我的图像都不会加载到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()

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

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

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

BGImage = pygame.image.load('Plat.jpg').convert()
  相关解决方案