-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
298 lines (227 loc) · 7.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'''
A clone of ... something ... with the kivy framework
intro sound http://www.freesound.org/people/ERH/sounds/42286/
water sound http://www.freesound.org/people/junggle/sounds/30342/
Image credit: NASA/JPL-Caltech
'''
import os
import sys
import re
import pickle
import math
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.scatter import Scatter
from kivy.properties import ObjectProperty, StringProperty, ListProperty, NumericProperty
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.core.audio import SoundLoader
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition, SlideTransition, WipeTransition, SwapTransition, ShaderTransition
from kivy.garden.moretransitions import PixelTransition, RippleTransition, BlurTransition, RVBTransition, RotateTransition
from functools import partial
class Splash(Screen):
pass
class Menu(Screen):
pass
class Help(Screen):
pass
class Game(Screen):
score = NumericProperty(0)
class ElementButton(Button):
text= StringProperty('')
class Element(Scatter):
text = StringProperty('')
color = ListProperty([1,1,1,1])
app = ObjectProperty()
def on_touch_up(self, touch):
if touch.grab_current is self:
self.app.check_position(self)
return super(Element, self).on_touch_up(touch)
def on_touch_move(self, touch):
if touch.grab_current is self:
self.app.check_color(self)
return super(Element, self).on_touch_move(touch)
class ElementPop(Scatter):
text = StringProperty('')
color = ListProperty([1,1,1,1])
class Transmut(App):
elements_found = ['Air', 'Earth', 'Fire', 'Water']
ether = []
universe = []
def build(self):
self.data_path = os.path.realpath(os.path.dirname(sys.argv[0])) + os.sep + "Data" + os.sep
self.load_ether()
#self.test()
#self.solution()
self.screenManager = ScreenManager(transition=FadeTransition())
self.splash = Splash(name="splash")
self.menu = Menu(name="menu")
self.help = Help(name="help")
self.game = Game(name="game")
self.screenManager.add_widget(self.splash)
self.screenManager.add_widget(self.menu)
self.screenManager.add_widget(self.help)
self.screenManager.add_widget(self.game)
sound_intro = SoundLoader.load(filename= self.data_path + '42286__erh__f-eh-angelic-3.ogg')
self.sound_find = SoundLoader.load(filename= self.data_path + '30342__junggle__waterdrop25.ogg')
sound_intro.play()
self.showSplash()
return self.screenManager
def showSplash(self):
self.screenManager.current = 'splash'
Animation(
scale=self.splash.background.scale*1.3,
duration=25.0
).start(self.splash.background)
def showMenu(self):
self.screenManager.transition=RippleTransition(duration=2.0)
self.screenManager.current = 'menu'
Animation(
scale=self.menu.background.scale*1.3,
duration=25.0
).start(self.menu.background)
def showHelp(self):
self.screenManager.transition=BlurTransition(duration=2.0)
self.screenManager.current = 'help'
Animation(
scale=self.help.background.scale*1.3,
duration=25.0
).start(self.help.background)
def newGame(self):
self.showGame()
def restore_game(self):
try:
g = open(self.data_path + 'savegame', 'rb')
self.elements_found = pickle.load(g)
g.close()
self.showGame()
except:
self.showHelp()
def showGame(self):
for e in self.elements_found:
self.update_elements_menu(e)
self.screenManager.transition=FadeTransition(duration=1.0)
self.screenManager.current = 'game'
def update_elements_menu(self,e):
self.game.elementlist.add_widget(ElementButton(text=e))
self.game.score = self.game.score + 1
def add_element_to_universe(self,*args):
e = args[0]
if len(self.universe) > 0:
#let's roll
a = 2*math.pi/len(self.universe)
c = 0
r = 150 + 100*len(self.universe)/10
for x in self.universe:
A = Animation(
center_x = Window.center[0] + math.cos(c*a)*r,
center_y = Window.center[1] + math.sin(c*a)*r,
transition = 'out_back',
duration = 1
)
c = c + 1
A.start(x)
f = Element(text=e, center=(Window.width,Window.height), app=self)
#f.bind(on_touch_up=partial(self.check_position,f))
#f.bind(on_touch_move=partial(self.check_color,f))
self.universe.append(f)
self.game.add_widget(f)
A = Animation(
center = Window.center,
transition = 'out_back',
duration = 1
)
A.start(f)
return 1
def check_color(self,f):
#border => red (delete)
if f in self.universe and (f.x <0.1*Window.width or f.x>0.9*Window.width or f.y<0.05*Window.height or f.y>0.90*Window.height):
f.color = [1,0,0.4,1]
else:
f.color = [1,1,1,1]
def check_position(self,f):
if f in self.universe and (f.x <0.1*Window.width or f.x>0.9*Window.width or f.y<0.05*Window.height or f.y>0.90*Window.height):
self.universe.remove(f)
self.game.remove_widget(f)
return 1
for e in self.universe:
if e == f:
continue
if f.collide_widget(e):
b = self.transmute(e.text,f.text)
if b:
pop = ElementPop(
text="%s + %s = %s" % (e.text,f.text,b),
center=Window.center,
app=self)
self.sound_find.play()
self.universe.remove(e)
self.universe.remove(f)
self.game.remove_widget(e)
self.game.remove_widget(f)
self.add_element_to_universe(b)
if b not in self.elements_found:
self.elements_found.append(b)
output = open(self.data_path + 'savegame', 'wb')
pickle.dump(self.elements_found, output)
output.close()
self.update_elements_menu(b)
#animation
pop.scale = pop.scale*0.2
self.game.add_widget(pop)
A = Animation(
scale = 1.0,
transition = 'out_sine',
duration = 1.0
)
A.on_complete = partial(self.deletePop,pop)
A.start(pop)
return 1
def deletePop(self,*args):
pop = args[0]
self.game.remove_widget(pop)
def load_ether(self):
f = open(self.data_path + "ether.dat", 'r')
c = 0
for line in f:
c = c + 1
if re.match("#",line):
continue
a = re.match("(\d+)\s+(.*?)\s+\=\s+(.*?)\s+\+\s+(.*?)\s+",line)
if a != None:
self.ether.append( [int(a.group(1)), a.group(2), a.group(3), a.group(4)] )
else:
print "Error building ether [%d] " % c
def transmute(self, item1, item2):
for formula in self.ether:
if (formula[2] == item1 and formula[3] == item2) or (formula[3] == item1 and formula[2] == item2):
return formula[1]
return None
def test(self):
print "%d %s = %s + %s" %(self.ether[5][0], self.ether[5][1], self.ether[5][2], self.ether[5][3])
print "transmute Storm & Snow => %s" % self.transmute('Storm','Snow')
print "transmute Bread & Snow => %s" % self.transmute('Bread','Snow')
# doesn't work
def solution(self):
s = ['Air', 'Earth', 'Fire', 'Water']
count = 4
def solution_rec(e,l):
for x in l:
n = self.transmute(e,x)
if n and n not in s:
#count = count + 1;
print "%s + %s = %s" %(e,x,n)
l.append(n)
solution_rec(n,l)
for e in s:
solution_rec(e,s)
#here we go!
if __name__ in ('__android__', '__main__'):
Transmut().run()