如何在pygame中用bg图像覆盖整个窗口[重复]

问题描述 投票:0回答:1

我想设置背景图像以覆盖我的整个屏幕尺寸,现在它覆盖了窗口的某些部分

import pygame
import sys
from pygame import *

pygame.init()
clock = pygame.time.Clock()
width = 1000
height = 800
screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Space Invaders")
backgroundImg = pygame.image.load("imp_resources/background.jpg")

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

screen.blit(backgroundImg, (0, 0))

pygame.display.flip()
clock.tick(60)
python pygame
1个回答
0
投票

以下是如何在 Pygame 中设置背景图像覆盖整个屏幕:

1。缩放图像以适合屏幕尺寸:

# Get the size of the background image
background_width, background_height = backgroundImg.get_size()

# Scale the image to fit the screen dimensions, preserving aspect ratio
scaled_background = pygame.transform.smoothscale(backgroundImg, (width, height))

2。将缩放后的图像传输到屏幕上:

screen.blit(scaled_background, (0, 0))

经过更改的完整代码:

import pygame
import sys
from pygame import *

pygame.init()
clock = pygame.time.Clock()
width = 1000
height = 800
screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Space Invaders")
backgroundImg = pygame.image.load("imp_resources/background.jpg")

# Scale the background image to fit the screen
scaled_background = pygame.transform.smoothscale(backgroundImg, (width, height))

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

  screen.blit(scaled_background, (0, 0))  # Use the scaled background image

  pygame.display.flip()
  clock.tick(60)
© www.soinside.com 2019 - 2024. All rights reserved.