-Regroupement des fonctions de chargment dans la classe io

-Ajout de la classe video qui lance la video au lancement du jeu
-Optimisation du rafraichissement de la fenêtre des mondes
This commit is contained in:
Le_dahut 2014-06-15 19:27:05 +02:00
parent 9c86d9ace9
commit 4306452b89
1 changed files with 168 additions and 135 deletions

View File

@ -34,38 +34,41 @@ from pyglet import image
''' *********************************************************************************************** ''' ''' *********************************************************************************************** '''
''' Fonctions de chargement ''' ''' Fonctions de chargement
'''
class io(object):
# Enregistre les données utilisateurs # Enregistre les données utilisateurs
def sync(): def sync(self):
global Uworlds, finished global Uworlds, finished
write(gethome() + "/dbdata", ["Uworlds", "finished"]) self.write(self.gethome() + "/dbdata", ["Uworlds", "finished"])
# Enregistre les données système # Enregistre les données système
def rebase(): def rebase(self):
global worlds global worlds
write("dbdata", ["worlds"]) self.write("dbdata", ["worlds"])
#Vérifie l'existence de la base de donnée utilisateur #Vérifie l'existence de la base de donnée utilisateur
def verifyhome(): def verifyhome(self):
global Uworlds, finished global Uworlds, finished
if not os.path.exists(gethome()): if not os.path.exists(self.gethome()):
os.makedirs(gethome()) os.makedirs(self.gethome())
if not os.path.exists(gethome() + "/dbdata"): if not os.path.exists(self.gethome() + "/dbdata"):
Uworlds = [[{0: 0}]] Uworlds = [[{0: 0}]]
finished = [(0, 0)] finished = [(0, 0)]
sync() self.sync()
#Trouve le chemin vers le repertoire utilisateur #Trouve le chemin vers le repertoire utilisateur
def gethome(): def gethome(self):
home = expanduser("~") + "/.wirechem" home = expanduser("~") + "/.wirechem"
return home return home
#Ecrit les variables spécifiés dans la base selectionnée (utilisateur ou système) #Ecrit les variables spécifiés dans la base selectionnée (utilisateur ou système)
def write(afile, var): def write(self, afile, var):
d = shelve.open(afile, writeback=True) d = shelve.open(afile, writeback=True)
for k in var: for k in var:
d[k] = copy.deepcopy(globals()[k]) d[k] = copy.deepcopy(globals()[k])
@ -74,7 +77,7 @@ def write(afile, var):
#Lit une base de donnée #Lit une base de donnée
def read(afile): def read(self, afile):
d = shelve.open(afile, writeback=True) d = shelve.open(afile, writeback=True)
for k in d.keys(): for k in d.keys():
globals()[k] = copy.deepcopy(d[k]) globals()[k] = copy.deepcopy(d[k])
@ -82,14 +85,14 @@ def read(afile):
#Charge le dictionnaire sous forme de variables #Charge le dictionnaire sous forme de variables
def load(d): def load(d):
for k in d.keys(): for k in d.keys():
if k[0] != "_": if k[0] != "_":
globals()[k] = copy.deepcopy(d[k]) globals()[k] = copy.deepcopy(d[k])
#Référence les variables #Référence les variables
def reference(var, noms): def reference(self, var, noms):
if len(noms) == 2: if len(noms) == 2:
for y in range(len(var)): for y in range(len(var)):
for x in range(len(var[y])): for x in range(len(var[y])):
@ -101,26 +104,20 @@ def reference(var, noms):
#duplique les références #duplique les références
def duplicateref(d): def duplicateref(self, d):
for k in d.keys(): for k in d.keys():
d[d[k]['nom']] = d[k] d[d[k]['nom']] = d[k]
def readlevel(self, w, l, user):
''' *********************************************************************************************** '''
''' Sauvegarde/Restauration '''
def readlevel(w, l, user):
global tuto, worlds, cout, selected, sizex, sizey, stat, tech global tuto, worlds, cout, selected, sizex, sizey, stat, tech
tuto = '' tuto = ''
if user: if user:
if w < len(Uworlds) and l < len(Uworlds[w]) and Uworlds[w][l].has_key("element"): if w < len(Uworlds) and l < len(Uworlds[w]) and Uworlds[w][l].has_key("element"):
load(Uworlds[w][l]) self.load(Uworlds[w][l])
else: else:
load(worlds[w][l]) self.load(worlds[w][l])
else: else:
load(worlds[w][l]) self.load(worlds[w][l])
menus[0][18]['icon'] = copy.deepcopy(art['null'])
sizex = len(world_new) sizex = len(world_new)
sizey = len(world_new[0]) sizey = len(world_new[0])
resize(); resize();
@ -129,7 +126,7 @@ def readlevel(w, l, user):
infos() infos()
def savelevel(w, l): def savelevel(self, w, l):
global tuto, users, worlds, Uworlds, nom, descriptif, video, link, tech, cout, victory, current, cycle, nrj, rayon, temp, maxcycle, maxnrj, maxrayon, maxtemp, world_new, world_art global tuto, users, worlds, Uworlds, nom, descriptif, video, link, tech, cout, victory, current, cycle, nrj, rayon, temp, maxcycle, maxnrj, maxrayon, maxtemp, world_new, world_art
while len(Uworlds) <= w: while len(Uworlds) <= w:
Uworlds.append(0) Uworlds.append(0)
@ -169,11 +166,12 @@ def savelevel(w, l):
#initialisation du jeu #initialisation du jeu
def init(): def init():
global worlds, debug, level global worlds, debug, level
verifyhome() inout=io()
read("dbdata") inout.verifyhome()
read(gethome() + "/dbdata") inout.read("dbdata")
reference(worlds, ['world', 'level']) inout.read(inout.gethome() + "/dbdata")
reference(Uworlds, ['world', 'level']) inout.reference(worlds, ['world', 'level'])
inout.reference(Uworlds, ['world', 'level'])
if len(sys.argv) > 1 and sys.argv[1] == 'debug': if len(sys.argv) > 1 and sys.argv[1] == 'debug':
debug = 1 debug = 1
else: else:
@ -250,26 +248,13 @@ class atext(object):
#Bouton sensible a plusieurs évènements #Bouton sensible a plusieurs évènements
class abutton(object): class abutton(object):
def update(self, dt): def update(self, dt):
if not self.hilite and dt>0:
return
if type(self.evalx) is str: if type(self.evalx) is str:
self.x = eval(self.evalx) self.x = eval(self.evalx)
if type(self.evaly) is str: if type(self.evaly) is str:
self.y = eval(self.evaly) self.y = eval(self.evaly)
try:
self.vertex_list.delete()
except:
foo = 0
try:
self.vertex_list2.delete()
except:
foo = 0
try:
self.vertex_list3.delete()
except:
foo = 0
try:
self.sprite.delete()
except:
foo = 0
if self.isvisible(): if self.isvisible():
if self.typeof == 'color': if self.typeof == 'color':
if not self.isactive(): if not self.isactive():
@ -285,18 +270,18 @@ class abutton(object):
self.vertex_list = eval(self.content) self.vertex_list = eval(self.content)
else: else:
if self.typeof == 'multicon': if self.typeof == 'multicon':
image = self.content[self.index] self.sprite.image = self.content[self.index]
else: else:
image = self.content self.sprite.image = self.content
self.sprite.x=self.x
self.sprite.y=self.y
if self.width == 0 or self.height == 0: if self.width == 0 or self.height == 0:
self.width = image.width self.width = self.sprite.image.width
self.height = image.height self.height = self.sprite.image.height
self.sprite = pyglet.sprite.Sprite(image, x=self.x, y=self.y, batch=self.window.batch, if self.width / float(self.height) < self.sprite.image.width / float(self.sprite.image.height):
group=self.window.p1) self.sprite.scale = float(self.width) / self.sprite.image.width
if self.width / float(self.height) < image.width / float(image.height):
self.sprite.scale = float(self.width) / image.width
else: else:
self.sprite.scale = float(self.height) / image.height self.sprite.scale = float(self.height) / self.sprite.image.height
if not self.isactive(): if not self.isactive():
self.sprite.color = (60, 60, 60) self.sprite.color = (60, 60, 60)
else: else:
@ -344,12 +329,29 @@ class abutton(object):
self.window.push_handlers(self.on_mouse_release) self.window.push_handlers(self.on_mouse_release)
self.window.push_handlers(self.on_mouse_scroll) self.window.push_handlers(self.on_mouse_scroll)
self.updateclock = clock.schedule_interval(self.update, 1) self.updateclock = clock.schedule_interval(self.update, 1)
if self.typeof=='multicon':
self.sprite = pyglet.sprite.Sprite(self.content[self.index],x=-300,y=-300, batch=self.window.batch, group=self.window.p1)
elif self.typeof=='icon':
self.sprite = pyglet.sprite.Sprite(self.content,x=-300,y=-300, batch=self.window.batch, group=self.window.p1)
self.update(0) self.update(0)
def delete(self): def delete(self):
try:
self.vertex_list.delete() self.vertex_list.delete()
del self except:
self = None foo = 0
try:
self.vertex_list2.delete()
except:
foo = 0
try:
self.vertex_list3.delete()
except:
foo = 0
try:
self.sprite.delete()
except:
foo = 0
def launch(self, state): def launch(self, state):
global debug global debug
@ -476,6 +478,31 @@ class game(pyglet.window.Window):
glClearColor(0, 0, 0, 255) glClearColor(0, 0, 0, 255)
self.clear() self.clear()
''' *********************************************************************************************** '''
''' Gestion lancement de la video '''
#Classe du menu principal
class video(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(video, self).__init__(width=1024, height=768, resizable=True, fullscreen=False, visible=True,
caption="Wirechem: The new chemistry game")
self.player = pyglet.media.Player()
self.player.queue(pyglet.resource.media("movie/intro.mp4"))
self.player.play()
self.clocks = clock.schedule(self.draw)
def draw(self,dt):
if self.player.source and self.player.source.video_format:
glColor3ub(255,255,255)
self.player.get_texture().blit(0,0,width=self.width,height=self.height)
def on_key_press(self, symbol, modifiers):
self.player.next()
super(video, self).close()
def on_mouse_press(self, x, y, button, modifiers):
self.player.next()
super(video, self).close()
''' *********************************************************************************************** ''' ''' *********************************************************************************************** '''
''' Gestion du menu principal ''' ''' Gestion du menu principal '''
@ -487,13 +514,7 @@ class menu(pyglet.window.Window):
super(menu, self).__init__(width=1024, height=768, resizable=True, fullscreen=False, visible=True, super(menu, self).__init__(width=1024, height=768, resizable=True, fullscreen=False, visible=True,
caption="Wirechem: The new chemistry game") caption="Wirechem: The new chemistry game")
self.focus = None self.focus = None
self.cursors = [pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/pointer.png'), 15, 46), self.set_mouse_cursor(cursors[0])
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/text.png'), 24, 30),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/move.png'), 24, 24),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/create.png'), 12, 17),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/cross.png'), 24, 33),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/delete.png'), 24, 32)]
self.set_mouse_cursor(self.cursors[0])
self.batch = pyglet.graphics.Batch() self.batch = pyglet.graphics.Batch()
self.p0 = pyglet.graphics.OrderedGroup(0) self.p0 = pyglet.graphics.OrderedGroup(0)
self.p1 = pyglet.graphics.OrderedGroup(1) self.p1 = pyglet.graphics.OrderedGroup(1)
@ -577,11 +598,14 @@ class menu(pyglet.window.Window):
self.lock = [ self.lock = [
pyglet.sprite.Sprite(pyglet.image.load('picture/locked.png'), batch=self.batch, group=self.p4, x=-300, pyglet.sprite.Sprite(pyglet.image.load('picture/locked.png'), batch=self.batch, group=self.p4, x=-300,
y=-300) for i in range(10)] y=-300) for i in range(10)]
self.update() self.update(0)
def on_resize(self, width, height): def on_resize(self, width, height):
super(menu, self).on_resize(width, height) super(menu, self).on_resize(width, height)
self.update() try:
self.update(0)
except:
dummy=0
def movefond(self, dt): def movefond(self, dt):
global loc global loc
@ -596,7 +620,7 @@ class menu(pyglet.window.Window):
if self.loc[1] < 0: if self.loc[1] < 0:
self.loc[3] = 1 self.loc[3] = 1
def update(self): def update(self,alevel):
global world, worlds, finished global world, worlds, finished
for obj in worlds[world]: for obj in worlds[world]:
if obj.has_key('special'): if obj.has_key('special'):
@ -610,8 +634,10 @@ class menu(pyglet.window.Window):
else: else:
self.labels[0].text = '' self.labels[0].text = ''
for l in range(len(self.buttons)): for l in range(len(self.buttons)):
if alevel!=0 and alevel!=l: continue
self.buttons[l].update(0) self.buttons[l].update(0)
for l in range(10): for l in range(10):
if alevel!=0 and alevel!=l: continue
if l >= len(worlds[world]): if l >= len(worlds[world]):
self.levels[l].x = -300 self.levels[l].x = -300
self.untitled[l].x = -300 self.untitled[l].x = -300
@ -766,7 +792,7 @@ class menu(pyglet.window.Window):
debug = 1 debug = 1
self.buttons[0].setselected(False) self.buttons[0].setselected(False)
ambiance.play() ambiance.play()
self.update() self.update(0)
def on_mouse_double_logo2(self, state): def on_mouse_double_logo2(self, state):
global debug global debug
@ -781,7 +807,7 @@ class menu(pyglet.window.Window):
global world global world
if world > 0: if world > 0:
world -= 1 world -= 1
self.update() self.update(0)
sounds.next() sounds.next()
sounds.queue(pyglet.resource.media("sound/lightning3.wav")) sounds.queue(pyglet.resource.media("sound/lightning3.wav"))
sounds.play() sounds.play()
@ -790,7 +816,7 @@ class menu(pyglet.window.Window):
global world global world
if world < len(worlds) - 1: if world < len(worlds) - 1:
world += 1 world += 1
self.update() self.update(0)
sounds.next() sounds.next()
sounds.queue(pyglet.resource.media("sound/lightning1.wav")) sounds.queue(pyglet.resource.media("sound/lightning1.wav"))
sounds.play() sounds.play()
@ -807,7 +833,7 @@ class menu(pyglet.window.Window):
except: except:
dummy = 0 dummy = 0
worlds[world][n]["special"] = True worlds[world][n]["special"] = True
self.update() self.update(n)
def on_mouse_drag_level(self, n, state): def on_mouse_drag_level(self, n, state):
global worlds, world global worlds, world
@ -816,11 +842,11 @@ class menu(pyglet.window.Window):
if state['buttons'] == 2: if state['buttons'] == 2:
worlds[world][n]["_xx"] += state['dx'] worlds[world][n]["_xx"] += state['dx']
worlds[world][n]["_yy"] += state['dy'] worlds[world][n]["_yy"] += state['dy']
self.set_mouse_cursor(self.cursors[2]) self.set_mouse_cursor(cursors[2])
self.update() self.update(n)
elif (state['buttons'] == 1 or state['buttons'] == 4) and type(self.selected) is int: elif (state['buttons'] == 1 or state['buttons'] == 4) and type(self.selected) is int:
self.selected = (world, n) self.selected = (world, n)
self.set_mouse_cursor(self.cursors[3]) self.set_mouse_cursor(cursors[3])
def on_mouse_release_level(self, n, state): def on_mouse_release_level(self, n, state):
global worlds, world global worlds, world
@ -846,7 +872,7 @@ class menu(pyglet.window.Window):
dummy = 0 dummy = 0
else: else:
self.selected = -1 self.selected = -1
self.set_mouse_cursor(self.cursors[0]) self.set_mouse_cursor(cursors[0])
def on_mouse_leave_menu(self, n, state): def on_mouse_leave_menu(self, n, state):
self.buttons[2 + n].setselected(False) self.buttons[2 + n].setselected(False)
@ -894,7 +920,7 @@ class menu(pyglet.window.Window):
self.selected = -1 self.selected = -1
if state['modifiers'] & key.MOD_CTRL: if state['modifiers'] & key.MOD_CTRL:
del worlds[world][n] del worlds[world][n]
self.update() self.update(0)
for ele in worlds[world]: for ele in worlds[world]:
l = 0 l = 0
while l < len(ele['link']): while l < len(ele['link']):
@ -921,7 +947,7 @@ class menu(pyglet.window.Window):
they = y they = y
if x > 1024 - 20 and world + 1 < len(worlds) and abs(self.selected[0] - world) < 1: if x > 1024 - 20 and world + 1 < len(worlds) and abs(self.selected[0] - world) < 1:
world += 1 world += 1
self.update() self.update(0)
return return
if self.focus: if self.focus:
self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers) self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
@ -956,7 +982,7 @@ class menu(pyglet.window.Window):
'video': False, 'video': False,
'world_art': [[0, 0, 0], [0, 0, 0], [0, 0, 0]], 'world_art': [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
'world_new': [[0, 0, 0], [0, 0, 0], [0, 0, 0]]}) 'world_new': [[0, 0, 0], [0, 0, 0], [0, 0, 0]]})
self.update() self.update(len(worlds[world])-1)
return return
for widget in self.untitled2 + self.untitled + self.infos: for widget in self.untitled2 + self.untitled + self.infos:
if widget.hit_test(x, y): if widget.hit_test(x, y):
@ -972,10 +998,10 @@ class menu(pyglet.window.Window):
return return
for widget in self.untitled2 + self.untitled + self.infos: for widget in self.untitled2 + self.untitled + self.infos:
if widget.hit_test(x, y): if widget.hit_test(x, y):
self.set_mouse_cursor(self.cursors[1]) self.set_mouse_cursor(cursors[1])
break break
else: else:
self.set_mouse_cursor(self.cursors[0]) self.set_mouse_cursor(cursors[0])
def on_text(self, text): def on_text(self, text):
if debug < 2: if debug < 2:
@ -999,9 +1025,9 @@ class menu(pyglet.window.Window):
if debug < 2: if debug < 2:
return return
if modifiers & key.MOD_SHIFT: if modifiers & key.MOD_SHIFT:
self.set_mouse_cursor(self.cursors[4]) self.set_mouse_cursor(cursors[4])
elif modifiers & key.MOD_CTRL: elif modifiers & key.MOD_CTRL:
self.set_mouse_cursor(self.cursors[5]) self.set_mouse_cursor(cursors[5])
if symbol == pyglet.window.key.TAB: if symbol == pyglet.window.key.TAB:
if modifiers & pyglet.window.key.MOD_SHIFT: if modifiers & pyglet.window.key.MOD_SHIFT:
dir = -1 dir = -1
@ -1030,6 +1056,12 @@ class menu(pyglet.window.Window):
''' *********************************************************************************************** ''' ''' *********************************************************************************************** '''
''' Lancement du menu principal ''' ''' Lancement du menu principal '''
cursors = [pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/pointer.png'), 15, 46),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/text.png'), 24, 30),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/move.png'), 24, 24),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/create.png'), 12, 17),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/cross.png'), 24, 33),
pyglet.window.ImageMouseCursor(pyglet.image.load('cursor/delete.png'), 24, 32)]
pyglet.font.add_file('font/Fluoxetine.ttf') pyglet.font.add_file('font/Fluoxetine.ttf')
pyglet.font.add_file('font/OpenDyslexicAlta.otf') pyglet.font.add_file('font/OpenDyslexicAlta.otf')
pyglet.font.add_file('font/Mecanihan.ttf') pyglet.font.add_file('font/Mecanihan.ttf')
@ -1042,8 +1074,7 @@ ambiance.eos_action = 'loop'
ambiance.play() ambiance.play()
sounds = pyglet.media.Player() sounds = pyglet.media.Player()
sounds.volume = 0.6 sounds.volume = 0.6
menu_principal = menu() video_intro=video()
menu_principal.set_minimum_size(1024, 768)
glEnable(GL_BLEND); glEnable(GL_BLEND);
#glEnable(GL_STENCIL_TEST); #glEnable(GL_STENCIL_TEST);
#glEnable(GL_LINE_SMOOTH); #glEnable(GL_LINE_SMOOTH);
@ -1051,7 +1082,9 @@ glEnable(GL_BLEND);
glEnable(GL_LINE_STIPPLE) glEnable(GL_LINE_STIPPLE)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pyglet.app.run() pyglet.app.run()
menu_principal = menu()
menu_principal.set_minimum_size(1024, 768)
pyglet.app.run()