无法在pygame

时间:2017-05-31 05:24:32

标签: python pygame

当我运行此代码时,我收到错误“ValueError:size needs be(int width,int height)”。

display_width = 800
display_height = 600
welcomeScreen = pygame.display.set_mode((display_width, display_height))

red = (255, 0, 0)
white = (255, 255, 255)

welcomeScreen.fill(white, rect = None, special_flags = 0)

welcomeMessageBackground = pygame.draw.rect(welcomeScreen, red, [200, 100, 400, 75]) 
welcomeFont = pygame.font.Font(None, 50)
welcomeMessage = welcomeFont.render('Welcome! Click a button to get started.', True, white)
pygame.Surface.blit(pygame.Surface(display_width, display_height), welcomeMessageBackground, area = None, special_flags = 0)
pygame.display.update()

导致问题的一行是

pygame.Surface.blit(pygame.Surface(display_width, display_height), welcomeMessageBackground, area = None, special_flags = 0)

我尝试将变量替换为值800和600,相同的错误。我想要的是welcomeMessage中的文本出现在rect的welcomeMessageBackground上。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:1)

这条线没有意义。

pygame.Surface.blit(pygame.Surface(display_width, display_height), welcomeMessageBackground, area = None, special_flags = 0)

你不应该调用pygame.Surface.blit(),而是要在其他表面上进行blit的表面的blit方法,例如:

welcomeScreen.blit(welcomeMessage, (70, 100))

welcomeFont.render会返回一个表面,您可以在屏幕或其他表面上进行blit。

要创建pygame.Surface实例,您必须传递宽度和高度的元组pygame.Surface((100, 200)),但在您的情况下,您根本不必创建另一个曲面。

此外,如果要在文本后面添加背景颜色,只需将颜色传递给字体的render方法:

welcomeMessage = welcomeFont.render(
    'Welcome! Click a button to get started.', True, white, red)

答案 1 :(得分:0)

您需要使用display_height作为值:

()

densityNUM_DISTINCT包裹在If target is the name of an existing column, and an automatically created statistics on this column exists, information about that auto-created statistic is returned. If an automatically created statistic does not exist for a column target, error message 2767 is returned. 内应该有效。请查看documentation