0

I have a small program in python and pygame but when I run my it I get this error:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "main.py", line 31, in <module>
    main()
  File "main.py", line 25, in main
    board.draw(WIN)
  File "/home/ether/Desktop/checkersai/checker/board.py", line 42, in draw
    piece.draw(win)
  File "/home/ether/Desktop/checkersai/checker/piece.py", line 32, in draw
    pygame.draw.circle(win, GREY, (self.x, self.y), radius + self.OUTLINE)
TypeError: integer argument expected, got float

and this is the function where the error is:

def draw(self, win):
        radius = SQUARE_SIZE//2 - self.PADDING
        pygame.draw.circle(win, GREY, (self.x, self.y), radius + self.OUTLINE)
        pygame.draw.circle(win, self.color, (self.x, self.y), radius)

and these are the variables I use:

WIDTH, HEIGHT = 800,800
ROWS, COLS = 8,8
SQUARE_SIZE = WIDTH/COLS

So I don't see how I get this error nor do I have any idea where I need to start looking for the error.

Here is the full code to my project https://pastebin.ubuntu.com/p/DHcRNT6948/

6
  • The operator you used in SQUARE_SIZE = WIDTH/COLS looks a bit different from what you used in radius = SQUARE_SIZE//2 - self.PADDING, doesn't it? Commented Sep 29, 2020 at 17:52
  • Something in this call is a float and must be cast to int: pygame.draw.circle(win, GREY, (self.x, self.y), radius + self.OUTLINE) Commented Sep 29, 2020 at 17:52
  • i dont understand what you mean Commented Sep 29, 2020 at 17:53
  • @Tgsmith61591 i know but i have nothing in my code that is a float or turns into a float. Its a checkers board Commented Sep 29, 2020 at 17:55
  • 2
    If Python says you've got a float, almost always, you've got a float. Commented Sep 29, 2020 at 17:56

1 Answer 1

5

Even though you used the integer division operator (//) when setting radius = SQUARE_SIZE//2 - self.PADDING, it's returning a float; that operator would normally return an int, but it still returns a float if you're dividing a float. In your case, you're dividing a float, SQUARE_SIZE. It's a float because SQUARE_SIZE = WIDTH/COLS uses the regular division operator (/), which always returns a float.

To fix your issue, do something like this:

SQUARE_SIZE = WIDTH//COLS  # SQUARE_SIZE is an int now

However, a more mathematically accurate approach would be to work with floats, and round & convert to int only at the last moment:

radius = int(round((WIDTH/COLS) / 2.0 - self.PADDING))
Sign up to request clarification or add additional context in comments.

2 Comments

even if use radius = SQUARE_SIZE/2 - self.PADDING i get the same error
@yappytwan That's giving you the same result because as I explained in my answer, the regular division operator returns a float; it's not that line that is causing the error. It's the fact that SQUARE_SIZE is a float, which happened because of the line SQUARE_SIZE = WIDTH/COLS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.