Skip to content

Instantly share code, notes, and snippets.

@SpaceVoyager
Created November 7, 2015 19:35
Show Gist options
  • Save SpaceVoyager/fd563465cddb39d80865 to your computer and use it in GitHub Desktop.
Save SpaceVoyager/fd563465cddb39d80865 to your computer and use it in GitHub Desktop.
aquarium_v1.py
from scene import *
from random import uniform
import sound
import speech
bounds = None
class SeaCreature:
upper_limit = 500
lower_limit = 225
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
self.super_fish = False
self.y_direction = uniform(-1, 1)
def change_direction(self):
self.y_direction = uniform(-1, 1)
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
self.y += self.y_direction
if self.y >= self.upper_limit:
self.y = self.upper_limit
if self.y <= self.lower_limit:
self.y = self.lower_limit
if self.super_fish:
tint(1.00, 0.00, 0.00)
else:
tint(1,1,1)
if self.alive:
image(self.image_name, self.x - self.size/2, self.y-self.size/2, self.size, self.size)
class MyScene (Scene):
game_start = False
game_over = False
ticks = 0
bottom_ammo_index = 0
bottom_ammo_list = ['Jack-O-Lantern', 'Honey_Pot', 'Alien_Monster']
top_ammo_index = 0
top_ammo_list = ['Chestnut', 'Doughnut', 'Hamburger']
def setup(self):
# This will be called before the first frame is drawn
global bounds
bounds = self.bounds
self.min_fish_size = 20
self.max_fish_size = 200
# 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(10):
self.fish_school1.append(SeaCreature('Tropical_Fish', 'move_left', uniform(50, 170), uniform(2,4), uniform(200, self.bounds.h - 200)))
# 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.reset_button_center = Point(800, self.bounds.h/2)
self.bottom_ammo_button_center = Point(375, self.button_radius)
self.top_button_center = Point(375, self.bounds.h-self.button_radius)
self.top_ammo_button_center = Point(650, self.bounds.h-self.button_radius)
self.balls_left = 10
self.food_left = 10
self.bottom_player_score = 0
self.top_player_score = 0
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 all_balls_dead(self):
for m in self.cannon_balls + self.cannon_balls2:
m_x = m['position'].x
m_y = m['position'].y
if 0 <= m_x <= bounds.w and 0 <= m_y <= bounds.h and m['alive']:
return False
return True
def check_game_over(self):
if self.balls_left == 0 and self.food_left == 0 and self.all_balls_dead():
self.game_over = True
def draw(self):
self.ticks += 1
# 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 ball['alive'] and fish.alive and fish.super_fish == False and self.collide(fish, ball):
self.bottom_player_score += 10
if self.bottom_player_score > 0 and self.bottom_player_score % 100 == 0:
self.balls_left += 10
speech.say('you got ten free cannon balls')
sound.play_effect('Explosion_5')
ball['alive'] = False
fish.size -= 10
if fish.size <= self.min_fish_size:
fish.size = self.min_fish_size
if fish.size == self.min_fish_size:
fish.alive = False
self.bottom_player_score += 100
for ball in self.cannon_balls2:
if self.collide(fish, ball) and ball['alive'] and fish.alive and not fish.super_fish :
self.top_player_score += 10
if self.top_player_score >0 and self.top_player_score % 100 == 0:
self.food_left += 10
speech.say('you got ten free food pieces', )
sound.play_effect('Laser_3')
ball['alive'] = False
fish.size += 10
if fish.size >= self.max_fish_size:
fish.size = self.max_fish_size
if fish.size == self.max_fish_size:
fish.super_fish = True
self.top_player_score += 100
# self.snail.update_state()
# self.fish1.update_state()
# self.octopus.update_state()
for fish in self.fish_school1:
fish.update_state()
if self.ticks % 120 == 0:
fish.change_direction()
# 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(self.bottom_ammo_list[c['type_index']], 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:
if c['alive'] == True:
image(self.top_ammo_list[c['type_index']], 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)
image(self.bottom_ammo_list[self.bottom_ammo_index], self.bottom_ammo_button_center.x - self.button_radius, self.bottom_ammo_button_center.y - self.button_radius, self.button_radius*2, self.button_radius*2)
image(self.top_ammo_list[self.top_ammo_index], self.top_ammo_button_center.x - self.button_radius, self.top_ammo_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)
text('Balls Left: ' + str(self.balls_left), 'AvenirNext-Heavy', 20, 800, 30)
text('Food Left: ' + str(self.food_left), 'AvenirNext-Heavy', 20, 800, 720)
text('Score: ' + str(self.bottom_player_score), 'MarkerFelt-Wide', 20, 200, 30)
text('Score: ' + str(self.top_player_score), 'MarkerFelt-Wide', 20, 200, 720)
if self.game_start == False:
tint(1.00, 0.00, 0.50)
text('Angry Fish', 'Futura-CondensedExtraBold', 100, bounds.w/2, 400 )
tint(1.00, 1.00, 0.40)
text('Touch anywhere to play', 'GillSans-Italic', 50, bounds.w/2, 300)
self.check_game_over()
if self.game_over:
image('Recycling_Symbol', self.reset_button_center.x - self.button_radius, self.reset_button_center.y - self.button_radius, self.button_radius*2, self.button_radius*2)
text('game over, touch anywhere to play again', 'Georgia-Bold', 50, bounds.w/2, 350)
if self.bottom_player_score > self.top_player_score:
text('Bottom player won!', 'Georgia-Bold', 100, bounds.w/2, 200)
elif self.bottom_player_score < self.top_player_score:
text('Top player won!', 'Georgia-Bold', 100, bounds.w/2, 200)
else:
text('Both players won!', 'Georgia-Bold', 100, bounds.w/2, 200)
def bottom_button_touched(self, touch):
if touch.location.distance(self.bottom_button_center) < self.button_radius:
return True
else:
return False
def bottom_ammo_type_button_touched(self, touch):
if touch.location.distance(self.bottom_ammo_button_center) < self.button_radius:
return True
else:
return False
def top_ammo_type_button_touched(self, touch):
if touch.location.distance(self.top_ammo_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 reset_button_touched(self, touch):
if touch.location.distance(self.reset_button_center) < self.button_radius:
return True
else:
return False
def touch_began(self, touch):
if self.bottom_button_touched(touch) and self.balls_left >= 1 and self.game_start:
self.balls_left -= 1
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,
'type_index': self.bottom_ammo_index
}
self.cannon_balls.append(new_cannon_ball)
if self.top_button_touched(touch) and self.food_left >= 1 and self.game_start:
self.food_left -= 1
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,
'alive': True,
'type_index': self.top_ammo_index
}
self.cannon_balls2.append(new_cannon_ball)
if self.bottom_ammo_type_button_touched(touch) and self.game_start:
self.bottom_ammo_index += 1
self.bottom_ammo_index = self.bottom_ammo_index % 3
if self.top_ammo_type_button_touched(touch) and self.game_start:
self.top_ammo_index += 1
self.top_ammo_index = self.top_ammo_index % 3
self.game_start = True
if self.game_over and self.reset_button_touched(touch):
self.game_over = False
self.setup()
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