]> vault307.fbx.one Git - PicoTamachibi.git/blob - main.py
initial commit
[PicoTamachibi.git] / main.py
1 # from icons import food_icon
2 from machine import I2C, Pin
3 from ssd1306 import SSD1306_I2C
4 from icon import Animate, Icon, Toolbar, Button, Event
5 from time import sleep
6 from sys import exit
7 import framebuf
8 import gc
9
10 sda = Pin(0)
11 scl = Pin(1)
12 id = 0
13
14 i2c = I2C(id=id, sda=sda, scl=scl)
15
16 oled = SSD1306_I2C(width=128, height=64, i2c=i2c)
17 oled.init_display()
18
19 health = 2
20 happiness = 1
21 energy = 1
22
23 # load icons
24 food = Icon('food.pbm', width=16, height=16, name="food")
25 lightbulb = Icon('lightbulb.pbm', width=16, height=16, name="lightbulb")
26 game = Icon('game.pbm', width=16, height=16, name="game")
27 firstaid = Icon('firstaid.pbm', width=16, height=16, name="firstaid")
28 toilet = Icon('toilet.pbm', width=16, height=16, name="toilet")
29 heart = Icon('heart.pbm', width=16, height=16, name="heart")
30 call = Icon('call.pbm', width=16, height=16, name="call")
31 save = Icon('disc.pbm', width=16, height=16, name="toilet")
32
33 def clear():
34 """ Clear the screen """
35 oled.fill_rect(0,0,128,64,0)
36
37 # def animate(frames, timer):
38 # for frame in frames:
39 # oled.blit(frame.image, frame.x, frame.y)
40 # oled.show()
41 # sleep(0.1)
42
43 def build_toolbar():
44 toolbar = Toolbar()
45 toolbar.spacer = 2
46 toolbar.additem(food)
47 toolbar.additem(lightbulb)
48 toolbar.additem(game)
49 toolbar.additem(firstaid)
50 toolbar.additem(toilet)
51 toolbar.additem(heart)
52 toolbar.additem(call)
53 toolbar.additem(save)
54 return toolbar
55
56 tb = build_toolbar()
57 poopy = Animate(x=96,y=48, width=16, height=16, filename='poop')
58 baby = Animate(x=48,y=16, width=48, height=48, filename='baby_bounce', animation_type='bounce')
59 eat = Animate(x=48,y=16, width=48, height=48, filename='eab: 20t')
60 babyzzz = Animate(animation_type="loop", x=48,y=16, width=48, height=48, filename='baby_zzz')
61 death = Animate(animation_type='bounce', x=40,y=16, width=16, height=16, filename="skull")
62 go_potty = Animate(filename="potty", animation_type='bounce',x=64,y=16, width=48, height=48)
63 call_animate = Animate(filename='call_animate', width=16, height=16, x=108, y=0)
64 call_animate.speed = 'very slow'
65
66 #button_jleft = Button(20)
67 #button_jright = Button(19)
68 #button_jup = Button(22)
69 #button_jdown = Button(21)
70 #button_jmid = Button(18)
71 #button_jset = Button(17)
72 #button_jrst = Button(16)
73
74 class ButtonConfig:
75 Configurations = {
76 "default":{
77 "left":20,
78 "right":19,
79 "action":18,
80 "cancel":16
81 },
82 "set1":{
83 "left":20,
84 "right":19,
85 "action":18
86 }
87 }
88 __ActiveConfig={}
89
90 def __init__(self, set="default"):
91 self.__ActiveConfig=self.Configurations[set]
92
93 @property
94 def ActiveConfig(self):
95 return self.__ActiveConfig
96
97
98 class controller:
99 PinObjects={}
100
101 def __init__(self,config):
102 for button in config.ActiveConfig:
103 self.PinObjects[button]=Pin(config.ActiveConfig[button])
104
105 ButtonSetup=ButtonConfig()
106 InputDevice=controller(ButtonSetup)
107
108 index = 3
109 tb.select(index, oled)
110 cancel = False
111 feeding_time = False
112 sleeping = False
113 death.set = True
114
115 # Set up Events
116 energy_increase = Event(name="Increase Energy", sprite=heart, value=1)
117 firstaid = Event(name="First Aid", sprite=firstaid, value=0)
118 toilet = Event(name="Toilet", sprite=toilet, value=0)
119 greatgame = Event(name="Gaming", sprite=game, value=0)
120 # poop_event = Event(name="poop time", sprite=poop_sprite, callback=poop_check())
121 sleep_time = Event(name="sleep time", sprite=lightbulb, value=1)
122 heart_status = Event(name="Status", sprite=heart)
123 # poop_event.timer = 3
124 # poop_event.timer_ms = 1
125
126 baby.bounce()
127 poopy.bounce()
128 death.loop(no=-1)
129 death.speed='slow'
130 babyzzz.speed = 'very slow'
131 go_potty.loop(no=1)
132 go_potty.set = True
133 poopy.set = False
134 go_potty.load()
135
136 while True:
137 if not cancel:
138 tb.unselect(index, oled)
139 if InputDevice.PinObjects["left"].value():
140 index -= 1
141 if index < 0:
142 index = tb.getlength()-1
143 cancel = False
144 if InputDevice.PinObjects["right"].value():
145 index += 1
146 if index == tb.getlength():
147 index = 0
148 cancel = False
149 if InputDevice.PinObjects["cancel"].value():
150 cancel = True
151 index = -1
152
153 if not cancel:
154 tb.select(index, oled)
155
156 if InputDevice.PinObjects["action"].value():
157 if tb.selected_item == "food":
158 feeding_time = True
159 sleeping = False
160 baby.unload()
161
162 if tb.selected_item == "game":
163 greatgame.message = "Gaming!!!"
164 greatgame.popup(oled=oled)
165 happiness += 1
166 energy -= 1
167 greatgame.message = "happy = " + str(happiness)
168 greatgame.popup(oled)
169 greatgame.message = "energy = " + str(energy)
170 greatgame.popup(oled)
171 clear()
172 print("game:\n energy = " + str(energy)+"\n happy = " + str(happiness))
173 if tb.selected_item == "toilet":
174 toilet.message = "Cleaning..."
175 toilet.popup(oled=oled)
176 poopy.set = False
177 baby.set = True
178 happiness += 1
179 clear()
180 poopy.unload()
181 if tb.selected_item == "lightbulb":
182 if not sleeping:
183 sleeping = True
184 babyzzz.load()
185 sleep_time.message = "Night Night"
186 sleep_time.popup(oled)
187 clear()
188 # need to add an event that increases energy level after sleeping for 1 minute
189 else:
190 sleeping = False
191 babyzzz.unload()
192 print("lightbulb")
193 if tb.selected_item == "firstaid":
194 firstaid.message = "Vitamins"
195 firstaid.popup(oled=oled)
196 health += 1
197
198 clear()
199 if tb.selected_item == "heart":
200 heart_status.message = "health = " + str(health)
201 heart_status.popup(oled)
202 heart_status.message = "happy = " + str(happiness)
203 heart_status.popup(oled)
204 heart_status.message = "energy = " + str(energy)
205 heart_status.popup(oled)
206 clear()
207 if tb.selected_item == "call":
208 # call_animate.animate(oled)
209 # call_animate.set = False
210 print("call")
211
212 # Time for Poop?
213 # poop_check()
214 # poop_event.tick()
215
216 if feeding_time:
217 eat.load()
218 if not eat.done:
219 eat.animate(oled)
220 if feeding_time and eat.done:
221 feeding_time = False
222 energy_increase.message = "ENERGY + 1"
223 energy_increase.popup(oled=oled)
224 energy += 1
225
226 clear()
227 eat.unload()
228 baby.load()
229 else:
230 if sleeping:
231 babyzzz.animate(oled)
232 else:
233 if baby.set:
234 baby.load()
235 baby.animate(oled)
236 if go_potty.set:
237 go_potty.animate(oled)
238 if go_potty.done:
239 print("potty done")
240 go_potty.set = False
241 poopy.set = True
242 baby.load()
243 baby.bounce(no=-1)
244 baby.set = True
245 if (energy <= 1) and (happiness <= 1) and (health <=1):
246 death.set = True
247 else:
248 death.set = False
249
250 #if (energy <= 1) or (happiness <= 1) or (health <= 1):
251 # set the toolbar call icon to flash
252 # call_animate.set = True
253 #else:
254 # call_animate.set = False
255
256 if poopy.set:
257 poopy.load()
258 poopy.animate(oled)
259 if death.set:
260 death.animate(oled)
261 tb.show(oled)
262 if index == 6:
263 tb.select(index, oled)
264 #else:
265 # if call_animate.set:
266 # call_animate.animate(oled)
267
268 oled.show()
269 sleep(0.05)
270