Making Scene Games That Scale On All Device Resolutions And Aspect Ratios?
Hi, I’ve been using pythonista for a while now and have made some games with it one of which is on the App Store it’s called “Pixel Chase” in case any of you were wondering. Anyway I’m working on a game at the moment and it is nearing completion however I would like it to run on all iOS devices (eg all supported IPhones, IPads and ipods) but I don’t have any clue how to do this. Does anyone know how I can get my game to scale correctly all answers are appreciated.
PS in case this matters my game is made on a iPad Air 2 so everything is hard coded to run on a 1024, 768 size screen.
You can get the screen size with ui.get_screen_size()
Cool but what do I have to do to tell scene to scale to that size?
you need to design your scene so that you don't hardcode sizes, but instead base everything on the screen size, width and height.
One approach would be to set the Scene.view.transform scale based on the minimum ratio between current screen size and your design size. This would be the easiest -- for instance if you hard coded everything like positions, or motions in pixels, etc. However, this might not work for really big discrepancies, and may not be the fastest.
from scene import * import random design_size=Size(1024,768) sz=get_screen_size() size_ratio=min([x/y for x,y in zip(tuple(sz),tuple(design_size))]) ''' approach 1: design everything for design_size, but then scale using view.transform. this is probably not the most efficient way, and also your aspect ratio will be fixed. ''' class MyScene(Scene): def setup(self): self.size=design_size self.setup_map() self.view.transform=ui.Transform.scale(size_ratio,size_ratio) def setup_map(self): #create "road" by scaling cards to just fit x=0 while x<self.size.width: imagename=random.choice(['plf:BG_Blue_desert','plf:BG_Blue_grass', 'plf:BG_Blue_land','plf:BG_Blue_shroom']) tile=SpriteNode(imagename) tile.size=tile.size*(self.size.width/tile.size.width/2.) tile.anchor_point=(0,1) tile.position=(x,self.size.height) self.add_child(tile) x+=tile.size.width x=0 while x<self.size.width: imagename='plf:Ground_DirtMid' tile=SpriteNode(imagename) tile.size=tile.size*(self.size.width/tile.size.width/3.5) tile.anchor_point=(0,0) tile.position=(x,0) self.add_child(tile) x+=tile.size.width #for testing purposes s=MyScene() run(s)
Another approach: use the size ratio to scale sprite sizes, but do all of your math based on the scene size. so road_speed=0.7 changes to road_speed=0.7*size_ratio, and you have to scale all of your sprites, etc. but you would draw your background to cover the full screen which might be taller or wider than what you designed for.
Thanks a lot this helps a bunch! I’ll start implementing this into my game whenever I can. Thanks again
@JonB thanks for the help it worked as intended. I am currently re writing my game in Corona SDK which uses LUA as its scripting language. Corona SDK has a “dynamic scaling” feature to scale sprites ect on different devices resolutions but it isn’t very accurate and the code you sent works way better. If it’s possible could you please re-write the “size ratio” line in LUA for me?
since i din't know lua, no.
But essentially this is just finding and using the smaller of
design_size/sz and design_size/sz... ie finding the smaller of the width ratio, or the height ratio, to figure out which is the limiting ratio.