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

Pygame平台游戏窗口显示黑屏问题的解决方案咨询

Fixing Black Screen & Movement Bugs in Your Pygame Platformer

Hey there! Let's get your platformer up and running properly—your black screen issue has a straightforward fix, and we'll also sort out that wonky player movement while we're at it.

1. The Core Black Screen Culprit

The biggest issue is that you've commented out the pygame.display.flip() line at the end of your game loop. This function is critical: it takes all the elements you've drawn (background, platforms, player) from Pygame's off-screen buffer and renders them to the visible window. Without it, nothing you draw ever gets shown, leaving you stuck with a black screen.

Fix: Remove the # before pygame.display.flip() to re-enable screen updates.

2. Broken Player Movement Logic

You've got two identical condition checks for the K_a key:

if keys[pygame.K_a]:
    player_x -= 2
if keys[pygame.K_a]:
    player_x += 2

This means pressing A will subtract 2 then immediately add 2 to player_x, so your player never moves. I assume you meant to use K_d for right movement.

Fix: Change the second condition to check for pygame.K_d instead:

if keys[pygame.K_d]:
    player_x += 2

3. Corrected Full Code

Here's your code with both fixes applied, plus a small cleanup note for platform layout:

import pygame
SCREEN_SIZE = (700,500)
DARK_GREY = (50,50,50)
OGURKOWY = (21,179, 58)
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Platformówka")
player_image = pygame.image.load("ogurek\\doggo.png")
player_x = 300
platforms = [
    pygame.Rect(100,300,400,50),
    pygame.Rect(100,250 ,50,50),
    pygame.Rect(450,250,50,50),
    pygame.Rect(450,250,100,50)  # Note: this overlaps with the previous platform (optional to remove)
]
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        player_x -= 2
    if keys[pygame.K_d]:  # Fixed key check for right movement
        player_x += 2
    #tło
    screen.fill(DARK_GREY)
    #platformy
    for p in platforms:
        pygame.draw.rect(screen, OGURKOWY, p)  # Cleaned up extra parentheses around color variable
    #gracz
    screen.blit(player_image, (player_x,100))
    pygame.display.flip()  # Re-enabled screen refresh
pygame.quit()

Quick Extra Note

You might notice two platforms in your list share the same position ((450,250)). That's not breaking anything, but it's drawing overlapping rectangles—feel free to remove one if that's not intentional.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 05:24:07