【问题标题】:Pygame in StudioCode on mac wont blit [duplicate]Mac上的Studio Code中的Pygame不会blit [重复]
【发布时间】:2021-02-22 18:46:10
【问题描述】:

我无法在我的 Mac 上使用工作室代码在我的 pygame 屏幕上添加任何内容。这是一个已知问题还是有办法解决我忽略的问题?我没有收到任何错误,只是没有做任何事情。我对 pygame 有点陌生,所以任何事情都可以工作。这是我的代码:

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

red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 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

【问题讨论】:

  • 我将输入的代码中的 pygame.init 和 pygam.font.init 与设置“myfont”变量的代码一起删除
  • 欢迎堆栈溢出。最好提供一个独立但很小的代码示例,以便人们尝试运行它,请参阅stackoverflow.com/help/minimal-reproducible-example

标签: python macos visual-studio-code pygame


【解决方案1】:

您只是没有将绘图图元刷新/更新到屏幕上。真的,你只需要一个 pygame.display.update()pygame.display.flip() 在你所有的 blits 完成之后。

我猜你删除了部分代码以保持问题的简单性,但我将它们放回去以获得有效的答案。

我还重新安排了代码,并删除了background Surface 的创建,只是为了获得中心坐标。这个操作可以在已有的screenSurface上进行。

import pygame
  
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 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()

【讨论】:

  • 这成功了!非常感谢。在我继续从事这个项目时,我会考虑到这一点,
猜你喜欢
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2016-03-31
相关资源
最近更新 更多