# @name Pixel Aquarium # @desc A tiny living aquarium for your AWTRIX NG # @version 0.1 import math class PixelAquarium var fish1_x var fish1_y var fish1_dir var fish2_x var fish2_y var fish2_dir var bubble1_x var bubble1_y var bubble2_x var bubble2_y var bubble3_x var bubble3_y var food_x var food_y var feeding var last_move var last_bubble var last_food def init() self.fish1_x = 5 self.fish1_y = 2 self.fish1_dir = 1 self.fish2_x = 23 self.fish2_y = 5 self.fish2_dir = -1 self.bubble1_x = 8 self.bubble1_y = 6 self.bubble2_x = 17 self.bubble2_y = 4 self.bubble3_x = 27 self.bubble3_y = 7 self.food_x = 16 self.food_y = 0 self.feeding = false self.last_move = 0 self.last_bubble = 0 self.last_food = 0 end # -------------------------------------------------- # DAY / NIGHT # -------------------------------------------------- def is_night() var h = hour() if h < 0 return false end if h >= 22 || h < 7 return true end return false end # -------------------------------------------------- # BUTTON # -------------------------------------------------- def on_button(btn) if btn == "select" self.food_x = 6 + (math.rand() % 21) self.food_y = 0 self.feeding = true self.last_food = now_ms() end end # -------------------------------------------------- # ANIMATION # -------------------------------------------------- def update_fish() var t = now_ms() var speed = 350 if self.feeding speed = 180 end if self.is_night() speed = 700 end if t - self.last_move < speed return end self.last_move = t # Fish 1 if self.feeding if self.fish1_x < self.food_x self.fish1_dir = 1 elif self.fish1_x > self.food_x self.fish1_dir = -1 end end self.fish1_x += self.fish1_dir if self.fish1_x >= width() - 4 self.fish1_dir = -1 end if self.fish1_x <= 1 self.fish1_dir = 1 end # Fish 2 moves a little less predictably if (math.rand() % 12) == 0 self.fish2_dir = self.fish2_dir * -1 end self.fish2_x += self.fish2_dir if self.fish2_x >= width() - 4 self.fish2_dir = -1 end if self.fish2_x <= 1 self.fish2_dir = 1 end end def update_bubbles() var t = now_ms() if t - self.last_bubble < 320 return end self.last_bubble = t self.bubble1_y -= 1 self.bubble2_y -= 1 self.bubble3_y -= 1 if self.bubble1_y < 0 self.bubble1_y = 6 self.bubble1_x = 3 + (math.rand() % 26) end if self.bubble2_y < 0 self.bubble2_y = 6 self.bubble2_x = 3 + (math.rand() % 26) end if self.bubble3_y < 0 self.bubble3_y = 6 self.bubble3_x = 3 + (math.rand() % 26) end end def update_food() if !self.feeding return end var t = now_ms() if t - self.last_food < 450 return end self.last_food = t self.food_y += 1 if self.food_y >= 7 self.feeding = false return end # Fish reached the food var dx = self.fish1_x - self.food_x if dx < 0 dx = dx * -1 end var dy = self.fish1_y - self.food_y if dy < 0 dy = dy * -1 end if dx <= 2 && dy <= 1 self.feeding = false end end # -------------------------------------------------- # BACKGROUND # -------------------------------------------------- def draw_water() if self.is_night() clear(0x000008) else clear(0x000018) end end def draw_sand() var sand = 0xD6A434 if self.is_night() sand = 0x392A12 end rect_fill(0, 7, width(), 1, sand) pixel(2, 7, 0x8A6325) pixel(11, 7, 0xF0C052) pixel(20, 7, 0x8A6325) pixel(29, 7, 0xF0C052) end def draw_plants() var green1 = 0x00AA44 var green2 = 0x00DD66 if self.is_night() green1 = 0x003318 green2 = 0x005522 end var sway = (now_ms() / 500) % 2 # left plant pixel(2, 6, green1) pixel(2, 5, green2) pixel(3, 4, green1) if sway == 0 pixel(2, 3, green2) else pixel(4, 3, green2) end # middle plant pixel(14, 6, green2) pixel(14, 5, green1) if sway == 0 pixel(13, 4, green2) pixel(15, 3, green1) else pixel(15, 4, green2) pixel(13, 3, green1) end # right plant pixel(29, 6, green1) pixel(29, 5, green2) pixel(28, 4, green1) if sway == 0 pixel(30, 3, green2) else pixel(27, 3, green2) end end def draw_rocks() var rock = 0x666666 if self.is_night() rock = 0x222222 end pixel(6, 6, rock) pixel(7, 6, rock) pixel(7, 5, rock) pixel(25, 6, rock) pixel(26, 6, rock) pixel(26, 5, rock) end def draw_chest() var brown = 0x8B4513 var gold = 0xFFBB00 if self.is_night() brown = 0x341A08 gold = 0x664400 end rect_fill(18, 5, 4, 2, brown) pixel(18, 5, gold) pixel(19, 5, gold) pixel(20, 5, gold) pixel(21, 5, gold) pixel(20, 6, gold) end # -------------------------------------------------- # FISH # -------------------------------------------------- def draw_fish(x, y, dir, body, tail) if self.is_night() body = 0x203040 tail = 0x152030 end if dir > 0 # tail pixel(x, y, tail) # body pixel(x + 1, y, body) pixel(x + 2, y, body) # nose pixel(x + 3, y, body) # lower body pixel(x + 1, y + 1, body) pixel(x + 2, y + 1, body) # eye pixel(x + 2, y, 0xFFFFFF) else pixel(x + 3, y, tail) pixel(x + 1, y, body) pixel(x + 2, y, body) pixel(x, y, body) pixel(x + 1, y + 1, body) pixel(x + 2, y + 1, body) pixel(x + 1, y, 0xFFFFFF) end end # -------------------------------------------------- # BUBBLES # -------------------------------------------------- def draw_bubbles() var c = 0x44CCFF if self.is_night() c = 0x123344 end pixel(self.bubble1_x, self.bubble1_y, c) pixel(self.bubble2_x, self.bubble2_y, c) pixel(self.bubble3_x, self.bubble3_y, c) end # -------------------------------------------------- # FOOD # -------------------------------------------------- def draw_food() if self.feeding pixel(self.food_x, self.food_y, 0xFF7722) end end # -------------------------------------------------- # NIGHT DETAILS # -------------------------------------------------- def draw_sleep() if !self.is_night() return end # tiny Z above fish if (now_ms() % 2000) < 1200 var x = self.fish1_x + 2 pixel(x, 0, 0x446688) if x + 1 < width() pixel(x + 1, 0, 0x446688) end if x + 1 < width() pixel(x + 1, 1, 0x446688) end end end # -------------------------------------------------- # MAIN DRAW # -------------------------------------------------- def draw() self.update_fish() self.update_bubbles() self.update_food() self.draw_water() self.draw_sand() self.draw_plants() self.draw_rocks() self.draw_chest() self.draw_bubbles() self.draw_fish( self.fish1_x, self.fish1_y, self.fish1_dir, 0xFF7A18, 0xFFB000 ) self.draw_fish( self.fish2_x, self.fish2_y, self.fish2_dir, 0x00BFFF, 0x0066FF ) self.draw_food() self.draw_sleep() end end return PixelAquarium()