Python 为什么我的代码在pygame上运行程序时不显示我的图像?

Python 为什么我的代码在pygame上运行程序时不显示我的图像?,python,pygame,Python,Pygame,我正在使用我导入到Pycharm中的Pygame 对于我的代码,我试图显示文件中的图像,以便在运行程序时加载到程序中,但它不起作用。该图像与我的python代码位于同一个文件中。当我运行它时,我看到的只是一个黑屏 图像的文件路径是正确的,因为当我将鼠标悬停在png文件上时,它显示了图像的预览 这是我的密码: 导入pygame,sys pygame.init() screen=pygame.display.set_模式((1275775)) bg_surface=pygame.image.loa

我正在使用我导入到Pycharm中的Pygame

对于我的代码,我试图显示文件中的图像,以便在运行程序时加载到程序中,但它不起作用。该图像与我的python代码位于同一个文件中。当我运行它时,我看到的只是一个黑屏

图像的文件路径是正确的,因为当我将鼠标悬停在png文件上时,它显示了图像的预览

这是我的密码:


导入pygame,sys
pygame.init()
screen=pygame.display.set_模式((1275775))
bg_surface=pygame.image.load(“C:/Users/davinaxx/Documents/Python/Testing/Background.png”)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame=quit()
sys.exit()
屏幕光点(bg_表面,(0,0))

你让它工作了吗

在对屏幕进行更改后,当调用
pygame.display.flip()
pygame.display.update()
时,它们都会被写入或“刷新”到显示器上(从更新队列中)。您没有此调用,因此无法在窗口中看到blit更改

pygame.quit()
也有问题,
blit()
需要缩进到
while
循环中,但这可能只是写问题时的粘贴问题

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((1275, 775))

bg_surface = pygame.image.load("C:/Users/DavinaXXX/Documents/Python/Testing/Background.png")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()                   # <<-- HERE (repair)
            sys.exit()


    screen.blit(bg_surface, (0, 0))         # <<-- HERE (indent into while)
    pygame.display.flip()                   # <<-- AND HERE (added)
import pygame,sys
pygame.init()
screen=pygame.display.set_模式((1275775))
bg_surface=pygame.image.load(“C:/Users/davinaxx/Documents/Python/Testing/Background.png”)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:

pygame.quit()#你让它工作了吗

在对屏幕进行更改后,当调用
pygame.display.flip()
pygame.display.update()
时,它们都会被写入或“刷新”到显示器上(从更新队列中)。您没有此调用,因此无法在窗口中看到blit更改

pygame.quit()
也有问题,
blit()
需要缩进到
while
循环中,但这可能只是写问题时的粘贴问题

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((1275, 775))

bg_surface = pygame.image.load("C:/Users/DavinaXXX/Documents/Python/Testing/Background.png")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()                   # <<-- HERE (repair)
            sys.exit()


    screen.blit(bg_surface, (0, 0))         # <<-- HERE (indent into while)
    pygame.display.flip()                   # <<-- AND HERE (added)
import pygame,sys
pygame.init()
screen=pygame.display.set_模式((1275775))
bg_surface=pygame.image.load(“C:/Users/davinaxx/Documents/Python/Testing/Background.png”)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:

pygame.quit()#您应该在
循环时写入
,是否尝试了相对路径?请阅读。请阅读。不要发布代码的截图。粘贴代码。您必须在
while
-循环中而不是在循环后调用
screen.blit(…)
。此外,您必须使用
pygame.display.flip()
(在循环中)更新显示器。您应该在
循环时写入
,您是否尝试了相对路径?请阅读。请阅读。不要发布代码的截图。粘贴代码。您必须在
while
-循环中而不是在循环后调用
screen.blit(…)
。此外,您还必须使用
pygame.display.flip()
(在循环中)更新显示器。感谢您的帮助。我设法使这段代码正常工作。谢谢你的帮助。我设法使这段代码正常工作。