Blit错误的目标位置无效

问题描述

我收到此错误。这是完整的追溯。

Traceback (most recent call last):
File "C:/Users/hobin/PycharmProjects/codeitPython/Snake_game.py",line 103,in <module>
snakegame()
File "C:/Users/hobin/PycharmProjects/codeitPython/Snake_game.py",line 52,in snakegame
dis.blit(value,round(display_width / 3),round(display_height / 5))
TypeError: invalid destination position for blit

这是与错误相关的代码

while not game_over:
    while game_end == True:
        #For displaying the score
        score = Length_of_snake -1
        score_font = pygame.font.SysFont("comicsansms",35)
        value = score_font.render("Your score: " + str(score),True,greencolor)
        dis.blit(value,round(display_height / 5))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True # The game window is still open
                game_end = False # game has been ended

我没有任何线索...我该怎么办?

解决方法

pygame.Surfac.blit的第二个参数( dest )是一对坐标。具有2个组成部分的元组或具有2个元素的列表:

dis.blit(value,round(display_width / 3),round(display_height / 5))

dis.blit(value,( round(display_width / 3),round(display_height / 5) ) )

为了完整起见,必须提到该参数也可以是矩形。具有四个组成部分(左,顶部,宽度,高度)的元组。