Harry Eastell Harry Eastell - 3 months ago 7
Python Question

Multiple unexpected errors

I'm not entirely sure what is wrong with these line, I don't understand why they are problematic. In the order of the problems (failing line will be highlighted with ***)

File "C:\Users\Harry\Desktop\CS Project\game test", line 32, in drawArena
pygame.draw.line(DISPLAYSURF, WHITE, ((WINDOWWIDTH/2),0),((WINDOWWIDTH/2),WINDOWHEIGHT), (LINETHICKNESS/4))
TypeError: integer argument expected, got float

File "C:\Users\Harry\Desktop\CS Project\game test", line 144, in main
drawArena()

File "C:\Users\Harry\Desktop\CS Project\game test", line 178, in
main()

def drawArena():
DISPLAYSURF.fill((0,0,0))
#Draw outline of arena
pygame.draw.rect(DISPLAYSURF, WHITE, ((0,0),(WINDOWWIDTH,WINDOWHEIGHT)), LINETHICKNESS*2)
#Draw centre line
***pygame.draw.line(DISPLAYSURF, WHITE, ((WINDOWWIDTH/2),0),((WINDOWWIDTH/2),WINDOWHEIGHT), (LINETHICKNESS/4))***

***drawArena()***
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)

if __name__ == '__main__':
***main()***

Answer

It's telling you the error. When using the method pygame.draw.line(), you must provide integer arguments for the dimensions of the line. If you look at your problematic line:

pygame.draw.line(DISPLAYSURF, WHITE, ((WINDOWWIDTH/2),0),((WINDOWWIDTH/2),WINDOWHEIGHT), (LINETHICKNESS/4))

One or more of the quotients of those division operations are resulting in a float(decimal) number. One simple way to fix this is to use the Python int() function to round any of your decimals to a whole number, like so:

pygame.draw.line(int((WINDOWWIDTH/2)),0),(int((WINDOWWIDTH/2)),WINDOWHEIGHT), int((LINETHICKNESS/4)))

If int, for some reason is not suitable, you can also use the Python round() function:

pygame.draw.line(round((WINDOWWIDTH/2)),0),(round((WINDOWWIDTH/2)),WINDOWHEIGHT), round((LINETHICKNESS/4)))