【问题标题】:The loading pygame image is randomly filling the screen正在加载的 pygame 图像随机填满屏幕
【发布时间】:2019-08-06 01:00:25
【问题描述】:

我在 pygame 加载图像时遇到问题。这是代码:

import pygame

pygame.init()

pygame.time.delay(900)

scr=pygame.display.set_mode((626,626))

pygame.display.update()

pygame.display.set_caption("Cricket")

bg=pygame.image.load('C:/Users/asus/Documents/Python Files/Cricket Application/bg.jpg').convert()

scr.blit(bg,(0,0))

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            break

当我运行它时,屏幕要么没有被填满,要么是底部的小块,要么是左边的块。

我不明白为什么会这样。

(图像与 .py 文件位于同一文件夹中) 任何帮助将不胜感激!

【问题讨论】:

    标签: python-3.x image pygame


    【解决方案1】:

    加载图像后不要使用 .convert() 。 这没有必要。

    除了调用pygame.quit()后代码中不使用break函数外,直接使用quit()即可。

    避免为图片提供目录,而是将图片粘贴到与python文件相同的文件夹中,只需在代码中写入图片文件的名称即可。

    另外,请记住在使用之前调整图像大小。

    最终代码应如下所示:

    import pygame
    pygame.init()
    
    pygame.time.delay(900)
    
    scr=pygame.display.set_mode((626,626))
    
    pygame.display.update()
    
    pygame.display.set_caption("Cricket")
    
    bg=pygame.image.load('image.jpg')
    
    scr.blit(bg,(0,0))
    
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
    

    【讨论】:

    • 你一半帮了我,我一半帮了自己。使用pygame.display.update() 显示图像。
    • 是的,我忘记了。但是图像出现在上面的程序中。
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多