Чтобы добавить изображение в Pygame, вы можете использовать несколько методов. Вот несколько подходов, которые вы можете использовать:
Метод 1: использование функции pygame.image.load()
import pygame
pygame.init()
# Create a display surface
screen = pygame.display.set_mode((800, 600))
# Load the image
image = pygame.image.load("image.jpg")
# Blit the image onto the screen
screen.blit(image, (0, 0))
# Update the display
pygame.display.update()
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
В этом методе вы загружаете изображение с помощью pygame.image.load()
, а затем копируете его на экран с помощью функции blit()
.
Метод 2: использование pygame.image.load()
и pygame.Surface
import pygame
pygame.init()
# Create a display surface
screen = pygame.display.set_mode((800, 600))
# Load the image
image = pygame.image.load("image.jpg")
# Create a surface to blit the image onto
image_surface = pygame.Surface((image.get_width(), image.get_height()))
# Blit the image onto the surface
image_surface.blit(image, (0, 0))
# Blit the surface onto the screen
screen.blit(image_surface, (0, 0))
# Update the display
pygame.display.update()
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
В этом методе вы загружаете изображение с помощью pygame.image.load()
, а затем создаете отдельную поверхность с помощью pygame.Surface()
. Вы копируете изображение на поверхность, а затем копируете поверхность на экран.
Метод 3: использование pygame.image.load()
и pygame.transform
import pygame
pygame.init()
# Create a display surface
screen = pygame.display.set_mode((800, 600))
# Load the image
image = pygame.image.load("image.jpg")
# Resize the image to fit the screen
image = pygame.transform.scale(image, (800, 600))
# Blit the image onto the screen
screen.blit(image, (0, 0))
# Update the display
pygame.display.update()
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
В этом методе вы загружаете изображение с помощью pygame.image.load()
. Затем вы можете изменить размер изображения, используя pygame.transform.scale()
, чтобы оно соответствовало размеру экрана, если это необходимо. Наконец, вы переносите изображение на экран.