微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame 中不断弹出弃用警告

如何解决pygame 中不断弹出弃用警告

所以,我正在制作一款我遇到多个问题的游戏,因为这是我的第一个游戏。问题之一是,每当我打开它时,只会弹出 -5,然后程序开始工作。我认为这是因为下面的这个特定警告:

Warning (from warnings module):
  File "C:/Users/LENOVO/Desktop/Everything Here!!/MoreCollision.py",line 57
    win.blit(text,(250 -(text.get_width()/2),200))
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated,and may be removed in a future version of Python.

属于它的代码是:

 def hit(self):
        self.x = 60
        self.y = 410
        self.walkCount = 0
        font1 = pygame.font.SysFont('comicsans',100)
        text = font1.render('-5',1,(255,0))
        win.blit(text,200))
        pygame.display.update()
        i = 0
        while i < 300:
            pygame.time.delay(10)
            i += 1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    i = 301
                    pygame.quit()

所以,我真的不知道如何解决它。如果有人可以,请帮忙。

解决方法

警告与 blit() 的坐标参数有关。浮点坐标意味着 Surface 的原点介于窗口中的像素之间。那没有多大意义。坐标会自动隐式截断,并由警告指示。

使用 //(地板除法)运算符

win.blit(text,(250 -(text.get_width() // 2),200))

round 到整数值

win.blit(text,(250 - round(text.get_width()/2),200))
,

删除将 -5 写入屏幕的代码以阻止其写入屏幕。这也解决了 DeprecationWarning,因为它是由将 -5 写入屏幕的代码引起的!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。