Reputation: 13
I am on windows 8, I use python 3.7 and I have a problem with pygame.
First of all, sorry for my poor english. I try to follow the essentials games v1 from magpi and I am trying to use the drop.py to test it, but when I run, it return me an error --> integer argument expected, got float
I am stuck on this line
if surface.get_at((player["x"], player["y"] + player["height"])) == (0, 0, 0, 255):
and this is the variable source
player = {
"x": int(windowWidth / 2),
"y": 0,
"height": 25,
"width": 10,
"vy": 5
}
I converted the player["x"] because i know pygame.surface.get_at expect an integer and not a float. But even if i convert the float value with the int() function, i still get this error.
Can you help me please, I tried to print the value to be sure it return an integer, and it does but I don't understand why i still get an error.
I searched on stackoverflow and none of the topic helped me on this problem. This is why I am asking for the first time on stackoverflow.
Thank you in advance for any answer.
update: I converted all my variable to integer inside my functions and the game work, but it seems hard coded for me. Did i do something wrong ?
if surface.get_at((int(player["x"]), int(player["y"]) + int(player["height"]))) == (0, 0, 0, 255):
Upvotes: 0
Views: 963
Reputation: 3889
Working with floats and integers when programming a game with pygame and python can seem a little clunky at times. This is because the screen is measured in pixels as integers. So you can't try to draw or access any pixels with floats without converting them first. There a couple solutions:
1) Don't work with floats. This will limit the capabilities of your game, but simple games can get away with it. Converting everything all the time to int is the equivalent of this.
2) Convert your floats at the last possible moment before drawing or accessing any pixels. You can now continue operating on player with floats. This will help make your code more pleasant to look at and help rounding errors with repeated calls to int. Rounding errors can make things on the screen seem a little 'off'.
for example (albeit a simple one):
speed = 1.5
player["x"] += speed
player["y"] += speed / 2
if surface.get_at(int(player["x"]), int(player["y"] + player["height"])) == (0, 0, 0, 255):
# do stuff
Notice the last possible moment when pixels are involved the numbers are converted. also notice the operation player["y"] + player["height"]
. The result of the operation is converted to an int() instead of each individual one.
Any time you can limit conversions will help keep your code clean and eliminate rounding errors.
Upvotes: 0