Skip to content

Instantly share code, notes, and snippets.

@SpaceVoyager
Created August 14, 2015 11:54
Show Gist options
  • Save SpaceVoyager/073accc5e03f36e78442 to your computer and use it in GitHub Desktop.
Save SpaceVoyager/073accc5e03f36e78442 to your computer and use it in GitHub Desktop.
gravity-controlled-rotation-test.py
# Demo code showing how to use iPad gravity sensor to control cannon orientation
# cannon clipart downloaded from
# http://www.clker.com/cliparts/7/9/9/b/1206570465701485742johnny_automatic_cannon_3.svg.med.png
from scene import *
from random import random
import math
class MyScene (Scene):
def setup(self):
# This will be called before the first frame is drawn.
# Set up the root layer and one other layer:
self.root_layer = Layer(self.bounds)
bottom_center = Point(self.bounds.w/2, 0)
self.layer = Layer(Rect(bottom_center.x - 64, bottom_center.y, 128, 128))
self.layer.image = '_cannon'
self.root_layer.add_layer(self.layer)
self.angle = 90
def draw(self):
# Update and draw our root layer. For a layer-based scene, this
# is usually all you have to do in the draw method.
background(0.00, 1.00, 1.00)
g = gravity()
gravity_angle = math.degrees(math.atan2(g.y, g.x))
if -180 <= gravity_angle <= 0:
self.angle = math.degrees(math.atan2(g.y, g.x)) + 180
self.layer.animate('rotation', self.angle, 1/60)
self.root_layer.update(self.dt)
self.root_layer.draw()
def touch_began(self, touch):
pass
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
pass
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment