如何将背景和图像导入pygame

时间:2019-08-24 19:37:24

标签: python pygame python-3.7

所以我用pygame制作了游戏。我完成了大部分代码,艺术已经完成。但是当我尝试加载精灵时,pygame给出了一个错误。错误消息为“无法在char = pygame.image.load('person1.png')中打开person1.png,文件[文件位置],第8行

这是针对使用pygame制作的游戏。我试图在其他人对同一问题的其他问题上使用他人的帮助。总是回到“无法打开person1.png”。

import pygame

pygame.init()
debug = True # If set to True, The console will print out the X and Y cords

win = pygame.display.set_mode((1280, 720)) # Game runs at 720p
pygame.display.set_caption("[Name (I dont want to leak the name)] ALPHA")
char = pygame.image.load('person1.png') # Sprites
bg = pygame.image.load('stage1.png')
isClimbing = True


x = 50
y = 50

height = 69
width = 69

vel = 13
level = 0

isJump = False
jumpCount = 10



run = True

if y > 335 or y < 335: 
            y = 335



def next_stage(): # Used for going on to the next stage
    stage + 1
    level = 0


def redrawGameWindow(): # Redraws the Game Window
    global walkCount 

    win.blit(bg, (0,0)) 
    win.blit(char, (x, y)) #Replaces the Rectangle with the sprite
    pygame.display.update()

# MAIN GAME LOOP BELOW

1 个答案:

答案 0 :(得分:0)

您必须编写.convert().convert_alpha()才能使pygame正确读取给定的图像:

img = pygame.image.load('my_image.png').convert_alpha()

请注意,convert_alpha()也会进行转换,但会关注图像的alpha(不透明度)