我正在尝试使用 blit 方法在 pygame 中上传图像,但图像没有显示在屏幕上

Im trying to upload an image in pygame with blit method but the image doesn't show up on the screen

所以我基本上只是尝试使用 blit 在 pygame 中上传图像,但它没有显示在屏幕上。我正在观看有关此内容的 youtube 教程,但无法让图像显示在屏幕上。我仔细检查了我是否下载了图像。

import pygame

# Define the background colour
# using RGB color coding.
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Define the dimensions of
# screen object(width,height)
screen = pygame.display.set_mode((900, 500))

# Set the caption of the screen
pygame.display.set_caption('Space game')

# Fill the background colour to the screen
screen.fill(WHITE)
pygame.display.update()

# Update the display using flip
pygame.display.flip()

# Variable to keep our game loop running
running = True
#maximum the FPS can go up to
FPS = (60)
clock = pygame.time.Clock()
#loading the spaceship images
YELLOW_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_yellow.png')
RED_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_red.png')

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE(300, 100))
# game loop
while running:
#telling the game to run only at 60 FPS
    clock.tick(FPS)

    # for loop through the event queue
    for event in pygame.event.get():

        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False

修改draw_window函数

blit 函数有两个非可选参数:要在屏幕上显示的图像和该图像的坐标。在您的代码中,我看到您忘记用逗号分隔两个参数。

所以改变这个:

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE(300, 100))

为此:

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE, (300, 100))

修改游戏循环

在游戏循环(while running 循环)中,您没有调用 draw_window 函数。此外,您没有更新显示。如果您应用这些更改,您的精灵将按预期绘制在屏幕上。

您需要进行的另一项更改:

在事件循环下方,您需要添加以下行:

pygame.quit()
sys.exit(0)

您必须添加此内容,因为当 runningFalse 时,您需要程序停止 运行。不要忘记导入 sys 模块,像这样:import sys

修改后的游戏循环:

# game loop
while running:
    # Fill the background colour to the screen
    screen.fill(WHITE)
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False
            
    draw_window()
    #telling the game to run only at 60 FPS
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()
sys.exit(0)

完整代码

import pygame
import sys
pygame.init()

# Define the background colour
# using RGB color coding.
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Define the dimensions of
# screen object(width,height)
screen = pygame.display.set_mode((900, 500))

# Set the caption of the screen
pygame.display.set_caption('Space game')

# Variable to keep our game loop running
running = True
#maximum the FPS can go up to
FPS = 60
clock = pygame.time.Clock()
#loading the spaceship images
YELLOW_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_yellow.png')
RED_SPACESHIP_IMAGE = pygame.image.load('images/spaceship_red.png')

def draw_window():
    screen.blit(YELLOW_SPACESHIP_IMAGE, (300, 100))

# game loop
while running:
    # Fill the background colour to the screen
    screen.fill(WHITE)
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for QUIT event
        if event.type == pygame.QUIT:
            running = False

    draw_window()
    #telling the game to run only at 60 FPS
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()
sys.exit(0)