0

So, I'm making a game that I am having multiple problems with as it is my first one. One of the problem is that whenever I open it only a -5 pops up and then the program starts working. I think this is because of this specific warning below:

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.

And the code belonging to it is:

 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,0))
        win.blit(text, (250 -(text.get_width()/2), 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()

So, I really have no clue how to fix it. Please help if anyone can.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You should change that line to `win.blit(text, (250 -int(text.get_width()/2), 200))`. However, this probably is not the cause of your problem. Where does a negative 5 pop up? In the terminal, pygame window, or somewhere else? – Seth Feb 02 '21 at 16:24
  • @Seth The negative 5 pops up right in the middle of the screen and the whole background goes black while it is not supposed to do any of this. And when my character in the game collides with the enemy everything workd fine until it starts again and the character just goes real down in the pygame window. Do you want me to upload the whole code? – Bruce Banner Feb 02 '21 at 16:30
  • Your code reads `text = font1.render('-5', 1, (255,0,0))`, so that is going to show a -5 on-screen when blitted. What did you expect? – Seth Feb 02 '21 at 16:35
  • @Seth how should I fix it? I'm sorry I am a total beginner – Bruce Banner Feb 02 '21 at 16:39
  • Remove the part of the code that writes a -5 to the screen... – Seth Feb 02 '21 at 16:40
  • Great! I would appreciate if you could upvote and mark as accepted my answer :) – Seth Feb 02 '21 at 16:44
  • @Seth my words expressing the issue were not clear at all (this coud be bc my native language isn't english). Your code did help but not in the way I intended it to, so I will just post another nice and clear question. And I am very sorry for the inconvinience I caused you ... – Bruce Banner Feb 04 '21 at 11:34

2 Answers2

0

The warning is related to the coordinate argument of blit(). Floating point coordinates, would mean that the origin of the Surface is somewhere in between to pixels in the window. That doesn't make much sense. The coordinates are automatically, implicitly truncated and that is indicated by the warning.

Use eiter the // (floor division) operator

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

or round to integral values

win.blit(text, (250 - round(text.get_width()/2), 200))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I'm sorry but it is still not going away. – Bruce Banner Feb 02 '21 at 16:34
  • @BruceBanner I'm sorry, so you did it wrong. – Rabbid76 Feb 02 '21 at 16:35
  • @BruceBanner Your question is about the warning *"an integer is required (got type float)"*. This question is duplicate. – Rabbid76 Feb 02 '21 at 16:38
  • I'm sorry I am a total beginner. – Bruce Banner Feb 02 '21 at 16:40
  • I think this was my fault because my question was very unclear, and yes you're right that I want to dislay the -5 but only when my character collides with the enemy, but it is showing it at the starting of my programme which is not what it is supposed to do. I'm very sorry for all this misunderstanding, and I will be posting a new and clear question... – Bruce Banner Feb 04 '21 at 11:32
0

Remove the code that writes -5 to the screen to stop it from writing to the screen. This also solves the DeprecationWarning since it was caused by the code that writes -5 to the screen!

Seth
  • 2,214
  • 1
  • 7
  • 21
  • The deprecate waring is caused by the line `win.blit(text, (250 -(text.get_width()/2), 200))`, as clearly shown in the error trace. It is caused by the fact that the result of a division is always a floating point number – Rabbid76 Feb 02 '21 at 16:45
  • Yes but that is writing `-5` to the screen which the OP doesn't want. Deleting the line entirely solves both problems. @Rabbid76 – Seth Feb 02 '21 at 16:46
  • So you want delete the line `win.blit(text, (250 -(text.get_width()/2), 200))`? – Rabbid76 Feb 02 '21 at 16:47
  • Yes. If you look at the comments on the question, you will see that this solved the OP's problem. @Rabbid76 – Seth Feb 02 '21 at 16:48
  • No I don't think so. I'm very sure he wants to draw some text. I think deleting all t he code is not the solution. However, the question is completely unclear and needs to be improved. – Rabbid76 Feb 02 '21 at 16:49