在Mac上的StudioCode中的Pygame不会失败

问题描述

我无法使用Mac上的工作室代码在pygame屏幕上显示任何内容。这是一个已知问题,还是有办法解决我所忽略的问题?我没有任何错误,只是什么也没做。我是pygame的新手,所以任何东西都可以工作。这是我的代码

pygame.display.set_caption('The space Simulator')

red=(255,0)
white=(255,255,255)
black=(0,0)
green=(0,0)
blue=(0,255)
image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/logo.jpg')
screen = pygame.display.set_mode([1000,1000])
background = pygame.Surface((1000,1000))
text1 = myfont.render('WELCOME TO MY SIMULATOR.',True,red)
textpos = text1.get_rect()
textpos.centerx = background.get_rect().centerx





running=True

while running:
    screen.blit(image,(textpos))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

解决方法

您只是没有将绘图图元刷新/更新到屏幕上。确实,完成所有blit后,您只需要pygame.display.update()pygame.display.flip()

我想您删除了代码的一部分以使问题变得简单,但我将它们放回去以得到有效的答案。

我还重新排列了代码,并删除了background Surface的创建,只是为了获得中心坐标。可以在现有的screen表面上执行此操作。

import pygame
  
red=(255,0)
white=(255,255,255)
black=(0,0)
green=(0,0)
blue=(0,255)

pygame.init()
pygame.display.set_caption('The space Simulator')
screen = pygame.display.set_mode([1000,1000])

#image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
image  = pygame.image.load('background.png' ).convert()
image  = pygame.transform.smoothscale( image,(1000,1000) )
#background = pygame.Surface((1000,1000))

myfont  = pygame.font.SysFont( None,24 )
text1   = myfont.render('WELCOME TO MY SIMULATOR.',True,red)
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx


running=True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.blit(image,(0,0))
    screen.blit(text1,(textpos))

    pygame.display.flip()

pygame.quit()