pygame 程序未报错却黑屏无法显示 请问有大佬知道解决方法吗

以下是从一本书上抄下来稍加改动的源代码,在 Python Idle 里运行没有报错,然而弹出的pygame窗口却除了显示窗口标题外一片漆黑。(如下图)

import pygame
import sys

# set everything to where started
pygame.init()

size = width, height = 650, 650
speed = [-2,1]
bg = (255, 255, 255)

# create a window with a determined size
screen = pygame.display.set_mode(size)

# create window title
pygame.display.set_caption("Hello!")

ball = pygame.image.load("D://Python//soccer.jpg")
# position of the image
position = ball.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

# move the image
position = position.move(speed)

if position.left < 0 or position.right > height:
    # flip image
    turtle = pygame.transform.flip(ball, True, False)
    # move to the other side
    speed[1] = -speed[1]

if position.top < 0 or position.bottom > height:
    speed[1] = -speed[1]

# fill the background
screen.fill()
# refresh image
screen.blit(ball, position)
# refresh screen
pygame.display.flip()
pygame.display.update()

# 10ms delay
pygame.time.delay(10)

pygame程序未报错却黑屏无法显示请问有大佬知道解决方法吗

Jason990420
最佳答案

修改如下, 说明在每行右边的批注

import pygame
import sys

# set everything to where started
pygame.init()

size = width, height = 650, 650
speed = [-2,1]
bg = (255, 255, 255)

# create a window with a determined size
screen = pygame.display.set_mode(size)

# create window title
pygame.display.set_caption("Hello!")

ball = pygame.image.load("D://soccer.jpg")
# position of the image
position = ball.get_rect()

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()                               # quit from pygame
            sys.exit()

    # move the image                                    # From this line
    position = position.move(speed)                     # should indent
    screen.fill(bg)                                     # clear screen
    screen.blit(ball, position)                         # draw ball at position
    pygame.display.update()                             # update screen
    clock.tick(100)                                     # 100 frames/second

    if position.left < 0 or position.right > height:
        # flip image
        ball = pygame.transform.flip(ball, True, False) # flip image to ball
        # move to the other side
        speed[0] = -speed[0]                            # index 0 for x

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]
4周前 评论
IFCll (楼主) 3周前
讨论数量: 2
Jason990420

修改如下, 说明在每行右边的批注

import pygame
import sys

# set everything to where started
pygame.init()

size = width, height = 650, 650
speed = [-2,1]
bg = (255, 255, 255)

# create a window with a determined size
screen = pygame.display.set_mode(size)

# create window title
pygame.display.set_caption("Hello!")

ball = pygame.image.load("D://soccer.jpg")
# position of the image
position = ball.get_rect()

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()                               # quit from pygame
            sys.exit()

    # move the image                                    # From this line
    position = position.move(speed)                     # should indent
    screen.fill(bg)                                     # clear screen
    screen.blit(ball, position)                         # draw ball at position
    pygame.display.update()                             # update screen
    clock.tick(100)                                     # 100 frames/second

    if position.left < 0 or position.right > height:
        # flip image
        ball = pygame.transform.flip(ball, True, False) # flip image to ball
        # move to the other side
        speed[0] = -speed[0]                            # index 0 for x

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]
4周前 评论
IFCll (楼主) 3周前
Jason990420

你只有在私底下不停的移动及翻转, 没有输出在屏幕上, 所以你觉得黑屏, 其实不是死机, 而是没输出.

3周前 评论
IFCll (楼主) 2周前

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!