Created
September 19, 2015 18:08
-
-
Save SpaceVoyager/fc15872ecdea85d41fed to your computer and use it in GitHub Desktop.
aquarium4.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
from scene import * | |
from random import uniform | |
import sound | |
bounds = None | |
class SeaCreature: | |
def __init__(self, image_name, direction, size, speed_x, y): | |
self.image_name = image_name | |
self.direction = direction | |
self.size = size | |
self.speed_x = speed_x | |
if self.direction == 'move_right': | |
self.x = -self.size/2 | |
else: | |
self.x = bounds.w +self.size/2 | |
self.y = y | |
self.alive = True | |
def update_state(self): | |
if self.direction == 'move_right': | |
self.x += self.speed_x | |
if self.x >= bounds.w + self.size/2: | |
self.x = -self.size/2 | |
else: | |
self.x -= self.speed_x | |
if self.x <= -self.size/2: | |
self.x = bounds.w + self.size/2 | |
if self.alive: | |
image(self.image_name, self.x - self.size/2, self.y-self.size/2, self.size, self.size) | |
class MyScene (Scene): | |
def setup(self): | |
# This will be called before the first frame is drawn | |
global bounds | |
bounds = self.bounds | |
# self.fish1 = SeaCreature('Tropical_Fish', 'move_left', 150, 2, 200) | |
# self.snail = SeaCreature('Snail', 'move_right', 64, 1, 10) | |
# self.octopus = SeaCreature('Octopus', 'move_right', 200, 2, 400) | |
self.fish_school1 = [] | |
for i in range(2): | |
self.fish_school1.append(SeaCreature('Tropical_Fish', 'move_left', uniform(50, 150), uniform(1,5), uniform(100, 600))) | |
# self.octopus_school = [] | |
# for i in range(10): | |
# self.octopus_school.append(SeaCreature('Octopus', 'move_right', uniform(50, 200), uniform(1, 4), uniform(50, 700))) | |
self.play_background_sound() | |
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 | |
self.button_radius = 50 | |
self.bottom_button_center = Point(650, self.button_radius) | |
self.top_button_center = Point(375, self.bounds.h-self.button_radius) | |
def play_background_sound(self): | |
sound.play_effect('Drums_08') | |
self.delay(0.5, self.play_background_sound) | |
def collide(self, fish, ball ): | |
fish_center = Point(fish.x, fish.y) | |
ball_center = ball['position'] | |
d = fish_center.distance(ball_center) | |
if d <= fish.size/2 + self.ball_size/2: | |
return True | |
else: | |
return False | |
def draw(self): | |
# This will be called for every frame (typically 60 times per second). | |
background(0.00, 1.00, 1.00) | |
fill(1.00, 0.80, 0.40) | |
rect(0, 0, self.bounds.w, 50) | |
for fish in self.fish_school1: | |
for ball in self.cannon_balls: | |
if self.collide(fish, ball) and ball['alive'] and fish.alive: | |
sound.play_effect('Explosion_5') | |
ball['alive'] = False | |
fish.size -= 10 | |
if fish.size <= 20: | |
fish.size = 20 | |
if fish.size == 20: | |
fish.alive = False | |
# self.snail.update_state() | |
# self.fish1.update_state() | |
# self.octopus.update_state() | |
for fish in self.fish_school1: | |
fish.update_state() | |
# for o in self.octopus_school: | |
# o.update_state() | |
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: | |
if c['alive'] == True: | |
image('Billiards', 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'])) | |
for c in self.cannon_balls2: | |
image('Cookie', c['position'].x - self.ball_size2/2, c['position'].y - self.ball_size2/2, self.ball_size2, self.ball_size2) | |
c['position'].x += self.ball_speed2*math.cos(math.radians(c['angle'])) | |
c['position'].y -= self.ball_speed2*math.sin(math.radians(c['angle'])) | |
image('Red_Circle', self.bottom_button_center.x - self.button_radius, self.bottom_button_center.y - self.button_radius, self.button_radius*2, self.button_radius*2) | |
text('Fire', 'ChalkboardSE-Bold', 20, self.bottom_button_center.x, self.bottom_button_center.y) | |
image('Blue_Circle', self.top_button_center.x - self.button_radius, self.bounds.h-self.button_radius*2, self.button_radius*2, self.button_radius*2) | |
text('Feed', 'ChalkboardSE-Bold', 20, 375, 700) | |
def bottom_button_touched(self, touch): | |
if touch.location.distance(self.bottom_button_center) < self.button_radius: | |
return True | |
else: | |
return False | |
def top_button_touched(self, touch): | |
if touch.location.distance(self.top_button_center) < self.button_radius: | |
return True | |
else: | |
return False | |
def touch_began(self, touch): | |
if self.bottom_button_touched(touch): | |
sound.play_effect('Shot') | |
new_cannon_ball = {'position': Point(self.bounds.w/2 + self.cannon_size/1.5*math.cos(math.radians(self.angle)), self.cannon_size/2 + self.cannon_size/1.5*math.sin(math.radians(self.angle))), | |
'angle': self.angle, | |
'alive' : True | |
} | |
self.cannon_balls.append(new_cannon_ball) | |
if self.top_button_touched(touch): | |
sound.play_effect('Boing_1') | |
new_cannon_ball = {'position': Point(self.bounds.w/2 + self.cannon_size/1.5*math.cos(math.radians(self.angle)), self.bounds.h - (self.cannon_size/2 + self.cannon_size/1.5*math.sin(math.radians(self.angle)))), | |
'angle': self.angle | |
} | |
self.cannon_balls2.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