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