You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame中Screen.blit图像重叠问题:仅显示底层图像求助

问题描述

作为高中IT项目,我用Pygame制作横版虐心游戏提升Python技能(代码灵感来自Clear Code视频),但遇到核心问题:同时用screen.blit()绘制player_surf和background_surf时,仅显示背景图像。单独绘制两者均正常,已排查图片透明度、绘制位置问题,仍无法解决。

问题代码
import pygame
from sys import exit


class GameState():
    def __init__(self):
        self.state = "cutscene"

    def cutscene(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        screen.blit(background_surf, (0, 0))

    def main_game(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        screen.blit(player_surf, (screen_x / 2, screen_y / 2))
        screen.blit(background_surf, (0, 0))


black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)

pygame.init()
screen_x = 800
screen_y = 400
screen = pygame.display.set_mode((screen_x, screen_y))
pygame.display.set_caption("Game")
fps = pygame.time.Clock()
game_state = GameState()

# Background
background_surf = pygame.image.load("Game/Background.png").convert()
background_surf = pygame.transform.smoothscale(background_surf, (screen_x, screen_y))

# Player
player_surf = pygame.image.load("Game/Red_Square.png").convert_alpha()
player_rect = player_surf.get_rect(center=(screen_x / 2, screen_y / 2))


while True:
    game_state.main_game()
    # game_state.cutscene()
    pygame.display.update()
    fps.tick(60)
解决方案

问题根源是绘制顺序错误:Pygame中,后调用blit()的图像会覆盖先绘制的内容。你当前代码里先画玩家,再画全屏背景,导致背景完全覆盖了玩家。

修复步骤:

  1. 调换main_game()方法中blit()的顺序,先绘制背景,再绘制玩家
  2. 建议使用已定义的player_rect来定位玩家,比直接写坐标更规范且易于后续修改

修改后的main_game()方法代码:

def main_game(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    screen.blit(background_surf, (0, 0))
    screen.blit(player_surf, player_rect)

完整修复后的代码:

import pygame
from sys import exit


class GameState():
    def __init__(self):
        self.state = "cutscene"

    def cutscene(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        screen.blit(background_surf, (0, 0))

    def main_game(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        # 先画背景,再画玩家
        screen.blit(background_surf, (0, 0))
        screen.blit(player_surf, player_rect)


black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)

pygame.init()
screen_x = 800
screen_y = 400
screen = pygame.display.set_mode((screen_x, screen_y))
pygame.display.set_caption("Game")
fps = pygame.time.Clock()
game_state = GameState()

# Background
background_surf = pygame.image.load("Game/Background.png").convert()
background_surf = pygame.transform.smoothscale(background_surf, (screen_x, screen_y))

# Player
player_surf = pygame.image.load("Game/Red_Square.png").convert_alpha()
player_rect = player_surf.get_rect(center=(screen_x / 2, screen_y / 2))


while True:
    game_state.main_game()
    # game_state.cutscene()
    pygame.display.update()
    fps.tick(60)

内容的提问来源于stack exchange,提问作者The_Eclipse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:10:29