为什么我在将图像blit至屏幕时画面没有显示出来?

2024-04-20 00:50:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python(和Pygame)创建了一个短的单屏幕游戏,并在不同的窗口中编写了每个部分的代码。在我的主屏幕上,当我在屏幕上轻按播放按钮时,它不会出现。我是Python和Pygame的新手。这是我的密码:

import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1352,638))
pygame.display.set_caption("Termination: Part 1")
bg = True
playButton = pygame.image.load("Play Button.png")
mouse = pygame.mouse.get_pos()

def playButtonFunction():
    if background == pygame.image.load("Home Screen.png"):
        background.blit(playButton(533.5,278))

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

        if event.type == KEYDOWN and event.key == K_SPACE:
            bg = False

    screen.blit(background,(0,0))
    if bg:
        background = pygame.image.load("Intro Screen.png")
    else:
        background = pygame.image.load("Home Screen.png")
    playButtonFunction()

    pygame.display.update()

Tags: imageimporteventif屏幕pngdisplaysys
1条回答
网友
1楼 · 发布于 2024-04-20 00:50:38

正如弗里德里克·哈米迪(Frédéric Hamidi)在评论中所说,这句话

if background == pygame.image.load("Home Screen.png")

不会像你预期的那样工作。你知道吗

当您不想显示playButton图像时,您应该向该方法传递一个标志,或者根本不调用该函数。你知道吗


还有,那条线

background.blit(playButton(533.5,278))

将抛出一个异常

background.blit(playButton, (533, 278))

所以,把你的代码改成

...

if bg:
    background = pygame.image.load("Intro Screen.png")
else:
    background = pygame.image.load("Home Screen.png")

screen.blit(background,(0,0))    
if !bg:
    background.blit(playButton, (533, 278))

...

另一个问题是,在游戏循环的每次迭代中(使用pygame.image.load)都从磁盘加载图像。只需加载一次图像就足够了。你知道吗

相关问题 更多 >