Here's a pool/billiards table I am using as a testbed for the SpriteKit wrapper.
You can actually play 8-ball with it, but mainly demo-pool.py tests and demonstrates how to:
- Create a "hollow sprite" - the sides of the pool table, for the balls to bounce in.
- Make a node (the table) unmoveable by setting
- Make nodes for display only (the pockets) by setting
- Create a gravity field node (in the pockets) to simulate the small slope into the pockets.
- Limit the effect of the gravity field to a small circular region - first version pulled the balls clear across the table, which made the game a bit too easy. :-)
- Manage the touches on the cue ball with its internal coordinate system that of course rotates with the ball.
contact_bitmaskto detect when the ball enters a pocket.
updateto make changes that cannot be made while the simulation step is running (moving balls to the side of the table).
collision_bitmaskto temporarily prevent a pocketes cue ball from interacting with the table or other balls when it is being moved back to the table.
I think that this also demonstrates the relative power of SpriteKit, as the demo is only about 200 lines of code.
Got excited about using SpriteKit, and quickly noted that it is not fun to use without a Python wrapper, so.
SpriteKit is interesting mainly if you need some 2D physics like collisions or field effects.
Here‘s a simple example demonstrating some features available so far:
scene = Scene( background_color='black', #1 physics=SpacePhysics) #2 class SpaceRock(SpriteNode): #3 def __init__(self, **kwargs): super().__init__(**kwargs) self.angular_velocity = random.random()*4-2 self.touch_enabled = True #4 def touch_ended(self, touch): #4 self.velocity = ( random.randint(-100, 100), random.randint(-100, 100) ) ship = SpriteNode( image=ui.Image('spc:EnemyBlue2'), position=(150,600), velocity=(0, -100), #5 parent=scene) rock = SpaceRock( image=ui.Image('spc:MeteorGrayBig3'), position=(170,100), velocity=(0,100), parent=scene) scene.view.present()
Points to note:
- Many familiar features work as they do in the scene module, like setting colors etc.
- Physics settings like gravity, linear damping (air friction) and restoration (bounciness) can be set with convenient packages.
- All nodes are inheritable.
- Individual nodes are touchable (and thus support gestures as well).
- Physics are accessible without explicitly playing with SpriteKit physicsBody.
This is work in progress, available on github. If you are interested in using SpriteKit in this way, let me know and we can guide the wrapping progress in a useful way.
The following crashes on the last line and in objc_util.py on line 897. Any ideas how to get this working?
from objc_util import * load_framework('SpriteKit') SKShapeNode = ObjCClass('SKShapeNode') cg_points = [CGPoint(0,0), CGPoint(10,5), CGPoint(5,0)] node = SKShapeNode.shapeNodeWithSplinePoints_count_(cg_points, len(cg_points))
I had a dire need for some gradients, so I packaged your code into a class and added the radial option (here).
Some usage examples:
Linear gradient, left to right, adjusting the location of the color shift for best effect:
Gradient(['grey', 'black'], locations=[0.0, 0.6])
Radial gradient from center:
Gradient(['white', 'transparent'], radial=True, width=200, height=200)
Finer control available with the additional parameters, I can detail those if someone has a need - too late now for serious documentation.
@shinyformica, the following works. Setting
presentdoes not - offset is set, but gets set again back to (0,0).
Hope this helps.
#coding: utf-8 from ui import * import editor ''' Dummy lines to have something to scroll to ''' l = ListDataSource(editor.get_text().splitlines()) v = TableView() v.data_source = l v.background_color = 'white' v.present() v.content_offset = (0,100)