Getting a game to scale on all devices
Behemoth last edited by
I've got a giant bunch of code for a game that I wrote specifically with numbers of pixels for just my device. I'm trying to use get_screen_size or get_screen_scale to make it work on every device but I just can't figure it out. Any help with how one can do this or anyone willing to?
Webmaster4o last edited by
You're saying you wrote a game with lots of magic numbers. This is a bad habit, in the future you should write it using
get_screen_size()the first time.
It's probably not hard to change if you take an easy approach:
- Store values like
screen_yin your code. Try
screen_x, screen_y = get_screen_size()
- Find all the constant numbers you have in your code.
- Decide whether these numbers describe height or width
- Express them as a proportion of the corresponding height value. For example, if you're using
400as a height value on an iPad mini, you can express this as
0.52 * screen_y. The height on an iPad mini (in landscape) is
400 / 768.0is
0.52. Now, if a user is on an iPad Pro in landscape, the height will be
0.52 * 1024is
532. That value is proportional to the value you used.
If you express all your values as a proportion to the screen size in this way, you can make a game that scales to any screen size.
In a lot of cases, this isn't really a good approach. Scaling to different screen sizes is really not easy to do well. In many cases, it's not ideal to have the game look the same on all devices. The UI that works on an iPad is probably too small for a phone. With this approach, if you do it thoroughly, the game will look stretched in the opposite orientation (if a landscape game is played in portrait or vice versa)
- Store values like
JonB last edited by
you could also try setting x_scale and y_scale by the appropriate ratio of whatever you have assumed in the design to the real screen size.
ccc last edited by ccc
@Behemoth, you might want to consider storing all of your user configurable variables in a single .yaml file like:
gold: 0 highscore: 0 shield: 0 ship: 0 ships: 0
You could then just do:
import yaml with open('laser.yaml') as in_file: user_settings = yaml.load_safe(in_file) print(user_settings)
Yaml files are supersimple for users to edit.
brumm last edited by
import scene print "total screen size = " + str(scene.get_screen_size() * scene.get_screen_scale())
Based on your total screen size you can work with a percental width and height. But as Webmaster40 already mentioned sometimes it's better to write different code because of the wide screen size range (iPod - iPad Pro).