Created
August 30, 2015 19:50
-
-
Save SpaceVoyager/b08efb36b11f8842fbcd to your computer and use it in GitHub Desktop.
two cannon version 1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.cannon_size = 128 | |
self.layer = Layer(Rect(bottom_center.x - self.cannon_size/2, bottom_center.y, self.cannon_size, self.cannon_size)) | |
self.layer.image = '_cannon' | |
self.root_layer.add_layer(self.layer) | |
self.angle = 90 | |
self.direction = 'counter_clock_wise' | |
self.cannon_balls = [] | |
self.ball_size = 50 | |
self.ball_speed = 8 | |
self.layer2 = Layer(Rect(bottom_center.x - self.cannon_size/2, self.bounds.h - self.cannon_size, self.cannon_size, self.cannon_size)) | |
self.layer2.image = '_cannon' | |
self.root_layer.add_layer(self.layer2) | |
self.cannon_balls2 = [] | |
self.ball_size2= 50 | |
self.ball_speed2 = 8 | |
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.40, 1.00, 0.80) | |
if self.angle > 180: | |
self.direction = 'clock_wise' | |
if self.angle < 0: | |
self.direction = 'counter_clock_wise' | |
if self.direction == 'counter_clock_wise': | |
self.angle += 1 | |
else: | |
self.angle -= 1 | |
self.layer.animate('rotation', self.angle, 1/60) | |
self.layer2.animate('rotation', -self.angle, 1/60) | |
self.root_layer.update(self.dt) | |
self.root_layer.draw() | |
for c in self.cannon_balls: | |
image('Cat_Face_Smiling', c['position'].x - self.ball_size/2, c['position'].y - self.ball_size/2, self.ball_size, self.ball_size) | |
c['position'].x += self.ball_speed*math.cos(math.radians(c['angle'])) | |
c['position'].y += self.ball_speed*math.sin(math.radians(c['angle'])) | |
def touch_began(self, touch): | |
new_cannon_ball = {'position': Point(self.bounds.w/2 + self.cannon_size/2*math.cos(math.radians(self.angle)), self.cannon_size/2 + self.cannon_size/2*math.sin(math.radians(self.angle))), | |
'angle': self.angle | |
} | |
self.cannon_balls.append(new_cannon_ball) | |
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