Python 精灵图像不会出现在pygame中

Python 精灵图像不会出现在pygame中,python,pygame,Python,Pygame,我正在学习这个pygame教程,我不能在窗口上显示我的精灵。当我运行程序时,我看到矩形在屏幕上移动,但它只是显示全黑。我尝试根据窗口更改矩形上的背景色,但仍然一无所获。我注意到矩形会根据我尝试加载的不同图像改变形状。这是代码。多谢各位 import pygame import random import os WIDTH = 800 HEIGHT = 600 FPS = 30 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0)

我正在学习这个pygame教程,我不能在窗口上显示我的精灵。当我运行程序时,我看到矩形在屏幕上移动,但它只是显示全黑。我尝试根据窗口更改矩形上的背景色,但仍然一无所获。我注意到矩形会根据我尝试加载的不同图像改变形状。这是代码。多谢各位

import pygame
import random
import os

WIDTH = 800
HEIGHT = 600
FPS = 30

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#SET ASSETS FOLDERS
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")

class Player(pygame.sprite.Sprite):
    # sprite for the Player
    def __init__(self):
        # this line is required to properly create the sprite
        pygame.sprite.Sprite.__init__(self)
        # create a plain rectangle for the sprite image
        self.image = pygame.image.load(os.path.join(img_folder, "p1_jump.png")).convert()


        # find the rectangle that encloses the image
        self.rect = self.image.get_rect()
        # center the sprite on the screen
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

    def update(self):
        # any code here will happen every time the game loop updates
        self.rect.x += 5
        if self.rect.left > WIDTH:
            self.rect.right = 0

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sprite Example")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update
    all_sprites.update()

    # Draw / render
    screen.fill(GREEN)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
enter code herepygame.display.flip()

pygame.quit()

我刚刚更改了您问题中代码末尾的一行,它开始工作:

    .
    .
    .
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update
    all_sprites.update()

    # Draw / render
    screen.fill(GREEN)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()  # <----- FIX THIS LINE

pygame.quit()
。
.
.
#游戏循环
运行=真
运行时:
#保持环路以正确的速度运行
时钟滴答声(FPS)
#过程输入(事件)
对于pygame.event.get()中的事件:
#检查车窗是否关闭
如果event.type==pygame.QUIT:
运行=错误
#更新
所有精灵更新()
#绘制/渲染
屏幕填充(绿色)
所有精灵。绘制(屏幕)
#*绘制完所有内容后,翻转显示器

pygame.display.flip()#您似乎没有正确编写
游戏循环
末尾的
pygame.display.flip()
语句。里面有一些垃圾,没有正确缩进。当我修复这些问题时,您的代码开始正常工作(不管怎样,还是使用我自己的测试映像)。非常感谢您的回复。你能告诉我怎么修吗?谢谢你花时间帮我。我修正了最后一行,仍然得到了没有图像的矩形。我使用的是python 3.5还是mac电脑,有什么不同吗?下面是它现在的样子。#绘制/渲染屏幕。填充(绿色)所有精灵。绘制(屏幕)#绘制完所有内容后,翻转显示pygame.display.flip()pygame.quit(),就我所知,代码看起来还行(尽管我看不到缩进)。我在Windows上使用Python3.7.0,所以这是可能的。它也可能是您试图显示的图像,因此请将其上载到类似的位置,然后在您的问题中(或在下面的评论中)发布指向它的链接。感谢您的帮助。听了你的话,我想可能不是代码或图像。所以我去更新了我的pygame到新版本,重新配置了我使用的IDE,我运行了与它相同的代码。再次感谢#马提诺