I’ve been searching forums for over an hr and can’t find anything. I’m following a tutorial for a snake game and I keep getting the bad geometry error. No spaces between numbers, im using x, not *, it’s lowercase… it’s driving me crazy!
when i run the code with either varaint, it opens the game window for 0.1 seconds and then closes itself, what could be the problem? Also, even after writing as you said it still gives me the same error.
I gave it a shot however not being familiar with this game. But I did follow the traceback and it referred to the geometry as the source of the error. Instead of using variables in place of the values,
I used actual numerical values as arguments to the function. As in:
You’ll notice that I have commented out the upper two that use variable names. Maybe with
variable names (text), the x multiplier gets interpreted as part of the variable name. Perhaps
this is why if numbers are used explicitly, there is a clear distinction between strings and numbers, and no geometry error is generated. I suspect that if the asterisk (*) could be used as a multiplier,
instead of the x text, this would not be an issue.
x = int(screen_width/2) - (window_width/2)
y = int(screen_height/2) - (window_height/2)
The problem is that / returns a float, e.g. 740.0, but the geometry manager expects ints, e.g. 740.
In each of those calculations, the result of the first division is converted to an int, but the result of the second division is not, so the final sum is a float.
If you change those lines to:
x = int(screen_width/2) - int(window_width/2)
y = int(screen_height/2) - int(window_height/2)
it no longer complains.
Alternatively, you can use //, which returns an int:
x = screen_width // 2 - window_width // 2
y = screen_height // 2 - window_height // 2
Why is there such a big difference in window size for the different implementations types?
Via variables
Via explicit integers
x = screen_width // 2 - window_width // 2
y = screen_height // 2 - window_height // 2
##y = 350 # Uncomment to overwrite value and try same value as integer numerical way
print('x', x)
print('y', y)
print('window_width', window_width)
print('window_height',window_height)
window.geometry(f"{window_width}x{window_height}+{x}+{y}") # Via variable way
#window.geometry('GAME_WIDTHxGAME_HEIGHT')
#window.geometry('700x350') # Via the explicit integer numerical way