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