class GuiUnitPanel: def __init__(self, aspect, unit_id, panel_pos, unit_type, default_hp, hp, default_ap, ap): self.unit_id = unit_id self.frameWidth = 0.30 self.frameHeight = 0.06 self.frame = DirectFrame( # relief = DGG.FLAT scale = 1 , frameSize = (0, self.frameWidth, 0, -self.frameHeight) , frameColor = (0.2, 0.2, 0.2, 0.8) , parent = base.a2dTopLeft )#@UndefinedVariable self.pos = Point3(0, 0, -GUI_TOP_OFFSET-0.05 - (0.069*panel_pos)) self.frame.setPos(self.pos) self.ap_bar = DirectWaitBar(parent = self.frame , text = "" , range = default_ap , value = ap , pos = (0.2,0,-0.045) , barColor = (0,0,1,1) , frameColor = (0,0,0.5,0.2) , scale = (0.1,0.5,0.1)) self.hp_bar = DirectWaitBar(parent = self.frame , text = "" , range = default_hp , value = hp , pos = (0.2,0,-0.015) , barColor = (0,1,0,1) , frameColor = (1,0,0,0.9) , scale = (0.1,0.5,0.08)) self.insignia = OnscreenImage(parent = self.frame ,image = "unit_" + unit_type + "_big_transparent_32.png" ,pos = (0.035,0,-0.03) ,scale = 0.025) self.insignia.setTransparency(TransparencyAttrib.MAlpha) self.all_icons = {} self.visible_icons = {} self.addIcon("overwatch") self.addIcon("set_up") self.getBounds(aspect) def addIcon(self, name): self.all_icons[name] = OnscreenImage(parent = self.frame ,image = name + "_icon.png" #,pos = offset + (0,0,-0.1) ,scale = 0.035) self.all_icons[name].setTransparency(TransparencyAttrib.MAlpha) self.all_icons[name].hide() def refreshBars(self, hp, ap): self.ap_bar['value'] = ap self.hp_bar['value'] = hp self.ap_bar.setValue() self.hp_bar.setValue() def refreshIcons(self): count = len(self.visible_icons) start_pos = 0.34 for icon in self.all_icons: if icon in self.visible_icons: self.visible_icons[icon].setPos((start_pos, 0, -0.032)) self.visible_icons[icon].show() start_pos += 0.07 else: self.all_icons[icon].hide() def hideOverwatch(self): if "overwatch" in self.visible_icons: self.visible_icons.pop("overwatch") self.refreshIcons() def showOverwatch(self): self.visible_icons["overwatch"] = self.all_icons["overwatch"] self.refreshIcons() def hideSetUp(self): if "set_up" in self.visible_icons: self.visible_icons.pop("set_up") self.refreshIcons() def showSetUp(self): self.visible_icons["set_up"] = self.all_icons["set_up"] self.refreshIcons() def getBounds(self, aspect): posx, posy = self.frame.getTightBounds() self.pos_min_x = -1 self.pos_min_y = self.pos.getZ() + 1 - self.frameHeight self.pos_max_x = -1 + self.frameWidth/aspect self.pos_max_y = self.pos.getZ() + 1
class Fighter(DirectObject): def __init__(self): base.disableMouse() # Carga el fondo del juego self.bg = loader.loadModel("models/plane") self.bg.reparentTo(camera) self.bg.setPos(0, 200, 0) self.bg.setScale(300, 0, 146) self.bg.setTexture(loader.loadTexture("models/Backgrounds/farback.png"), 1) # Inicializa el gestor de teclado y los objetos del juego self.inputManager = InputManager() # Inicializa el menu del juego self.inicializarMenu() self.marcador = None self.barraEnergia = None self.marcadorFinalNP = None self.entrada = None self.rankingNP = None self.mostrarMenuJuego() self.accept("m", self.cambiarMenuJuego) self.accept("q", self.salir) # Inicializa el menu def inicializarMenu(self): self.menuGraphics = loader.loadModel("models/MenuGraphics") self.fonts = {"silver" : loader.loadFont("fonts/LuconSilver"), "blue" : loader.loadFont("fonts/LuconBlue"), "orange" : loader.loadFont("fonts/LuconOrange")} self.menu = Menu(self.menuGraphics, self.fonts, self.inputManager) self.menu.initMenu([0, None, ["Nueva Partida", "Salir"], [[self.nuevaPartida], [self.salir]], [[None], [None]]]) # Comienza una partida def nuevaPartida(self): if (not self.marcadorFinalNP is None): self.marcadorFinalNP.detachNode() self.marcadorFinalNP.remove() if (not self.rankingNP is None): self.rankingNP.detachNode() self.rankingNP.remove() self.ship = Ship(self.inputManager) self.mostrarInfo() taskMgr.add(self.actualizarInfo, "Actualizar Puntuacion") # Inicializa y muestra el marcador del jugador def mostrarInfo(self): self.marcador = TextNode("Marcador") self.marcador.setText("Puntos: " + str(self.ship.puntos)) self.marcador.setCardColor(0, 0, 0, 1) self.marcador.setCardDecal(True) self.marcador.setCardAsMargin(0.4, 0.4, 0.4, 0.4) self.marcadorNP = aspect2d.attachNewNode(self.marcador) self.marcadorNP.reparentTo(base.a2dTopLeft) self.marcadorNP.setPos(0.02, 0, -0.05) self.marcadorNP.setScale(0.07) self.barraEnergia = DirectWaitBar(text = "Energia", value = 5, range = 5, scale = 0.3, pos = (0, 0, 0.95)) # Actualiza la puntuacion del jugador en pantalla def actualizarInfo(self, tarea): self.marcador.setText("Puntos: " + str(self.ship.puntos)) self.barraEnergia["value"] = self.ship.vida self.barraEnergia.setValue() # Termina la partida if (self.ship.terminarPartida): self.terminarPartida() return tarea.done return tarea.cont # Termina partida liberando recursos para poder empezar una nueva # sin reiniciar el juego def terminarPartida(self): # Solicita al usuario un nombre para la tabla de puntuaciones self.entrada = DirectEntry(width = 15, numLines = 1, scale = 0.07, cursorKeys = 1, frameSize = (0, 15, 0, 1), command = self.almacenarPuntuacion, pos = (-0.3, 0, 0.1), focus = True, text_pos = (0.2, 0.2)) self.puntos = self.ship.puntos self.ship.ship.detachNode() self.ship.ship.remove() taskMgr.remove("Mover Nave") taskMgr.remove("Generar Enemigos") taskMgr.remove("Comprobar Impactos") taskMgr.remove("Actualizar Puntuacion") taskMgr.remove("Explosionar*") self.mostrarFinPartida() # Libera los recursos de la partida que ha terminado self.ship.eliminarObjetos() del self.ship del self.menuGraphics del self.menu self.marcadorNP.detachNode() self.marcadorNP.remove() self.barraEnergia.destroy() del self.marcador del self.barraEnergia #self.inicializarMenu() # Almacena la puntuacion del jugador def almacenarPuntuacion(self, valor): self.crearBDD() db = sqlite3.connect("datos.db") cursor = db.cursor() parametros = (valor, self.puntos) cursor.execute("insert into puntuaciones values (?, ?)", parametros) db.commit() cursor.close() self.entrada.destroy() self.mostrarTopPuntuacion() self.inicializarMenu() # Crea la Base de Datos si no existe ya def crearBDD(self): db = sqlite3.connect("datos.db") cursor = db.cursor() args = ("puntuaciones",) cursor.execute("select name from sqlite_master where name = ?", args) if len(cursor.fetchall()) == 0: cursor.execute("create table puntuaciones (nombre text, puntuacion numeric)") db.commit() cursor.close() # Muestra las 10 mejores puntuaciones def mostrarTopPuntuacion(self): # Extrae las 10 mejores puntuaciones de la base de datos db = sqlite3.connect("datos.db") cursor = db.cursor() cursor.execute("select nombre, puntuacion from puntuaciones order by puntuacion desc limit 10") puntuaciones = cursor.fetchall() cursor.close() resultado = "-- MEJORES PUNTUACIONES --\n-Jugador- -Puntuacion-\n\n" for nombre, puntuacion in puntuaciones: resultado += nombre + " " + str(puntuacion) + "\n" # Muestra las 10 mejores puntuaciones self.ranking = TextNode("Ranking") self.ranking.setText(resultado) self.ranking.setCardColor(0, 0, 0, 1) self.ranking.setCardDecal(True) self.ranking.setCardAsMargin(0.4, 0.4, 0.4, 0.4) self.rankingNP = aspect2d.attachNewNode(self.ranking) self.rankingNP.reparentTo(base.a2dTopLeft) self.rankingNP.setPos(1, 0, -1) self.rankingNP.setScale(0.07) # Muestra el mensaje de fin de partida def mostrarFinPartida(self): self.marcadorFinal = TextNode("Marcador Final") self.marcadorFinal.setText("Game Over!\nPuntuacion: " + str(self.ship.puntos) +"\n\n" + "Escribe tu nombre:") self.marcadorFinal.setCardColor(0, 0, 0, 1) self.marcadorFinal.setCardDecal(True) self.marcadorFinal.setCardAsMargin(0.4, 0.4, 0.4, 0.4) self.marcadorFinalNP = aspect2d.attachNewNode(self.marcadorFinal) self.marcadorFinalNP.setPos(-0.3, 0, 0.5) self.marcadorFinalNP.setScale(0.07) # Muestra un menu con las opciones durante el juego def mostrarMenuJuego(self): self.textoMenu = {} self.textoMenu["titulo"] = OnscreenText(text = "", pos = (0, 0.92), scale = 0.08, fg = (1, 1, 1, 1), bg = (0, 0, 1, 0.7)) self.textoMenu["descripcion"] = OnscreenText(text = "", pos = (0, 0.84), scale = 0.05, fg = (1, 1, 0, 1), bg = (0, 0, 0, 0.5)) self.textoMenu["opciones"] = OnscreenText(text = "", pos = (-1.3, 0), scale = 0.05, fg = (1, 1, 1, 1), bg = (1, 0.3, 0, 0.6), align=TextNode.ALeft, wordwrap = 15) self.textoMenu["opciones"].setText("** OPCIONES **\n" + "m = ocultar menu\n" + "q = salir") # Inicialmente el menu se deja oculto for linea in self.textoMenu.values(): linea.hide() # Muestra / Oculta el menu de juego def cambiarMenuJuego(self): for linea in self.textoMenu.values(): if linea.isHidden(): linea.show() else: linea.hide() # Sale del juego def salir(self): print("Saliendo . . .") sys.exit()
class GuiUnitInfo: def __init__(self, offset, parent, unit_type, default_hp, hp, default_ap, ap): self.offset = offset self.frame = DirectFrame( relief = DGG.FLAT , scale = 1 , frameSize = (-0.5, 0.5, 0, -0.5) , parent = parent ) self.frame.setBillboardPointEye() self.frame.setLightOff() self.frame.setBin("fixed", 40) self.frame.setDepthTest(False) self.frame.setDepthWrite(False) fixedWidthFont = loader.loadFont(GUI_FONT)#@UndefinedVariable #fixedWidthFont.setPixelsPerUnit(60) #fixedWidthFont.setRenderMode(fontt.RMSolid) if not fixedWidthFont.isValid(): print "pandaInteractiveConsole.py :: could not load the defined font %s" % str(self.font) fixedWidthFont = DGG.getDefaultFont() self.label = OnscreenText( parent = self.frame , text = "" , pos = (offset.getX(),offset.getZ()+0.1) , align=TextNode.ACenter , mayChange=True , scale=0.1 , fg = (1,0,0,1) #, shadow = (0, 0, 0, 1) #, frame = (200,0,0,1) ) self.label.setFont( fixedWidthFont ) #self.label.setLightOff() self.all_icons = {} self.visible_icons = {} self.addIcon("overwatch") self.addIcon("set_up") self.ap_bar = DirectWaitBar(parent = self.frame , text = "" , range = default_ap , value = ap , pos = (offset.getX()+0.08,0,offset.getZ()-0.27) , barColor = (0,0,1,1) , frameColor = (0,0,0.5,0.2) , scale = (0.3,0.5,0.3)) self.hp_bar = DirectWaitBar(parent = self.frame , text = "" , range = default_hp , value = hp , pos = (offset.getX()+0.08,0,offset.getZ()-0.2) , barColor = (0,1,0,1) , frameColor = (1,0,0,0.9) , scale = (0.3,0.5,0.3)) self.insignia = OnscreenImage(parent = self.frame ,image = "unit_" + unit_type + "_big_transparent_32.png" #,pos = (offset.getX(),0,offset.getZ()+0.14) , pos = (offset.getX() - 0.31,0,offset.getZ()-0.23) ,scale = 0.09) self.insignia.setTransparency(TransparencyAttrib.MAlpha) def addIcon(self, name): self.all_icons[name] = OnscreenImage(parent = self.frame ,image = name + "_icon.png" #,pos = offset + (0,0,-0.1) ,scale = 0.08) self.all_icons[name].setTransparency(TransparencyAttrib.MAlpha) self.all_icons[name].hide() def write(self, text): text = "" self.label.setText(text) def redraw(self): return def remove(self): self.frame.remove() def reparentTo(self, parent): self.frame.reparentTo(parent) def hide(self): self.label.hide() def show(self): self.label.show() def refreshBars(self, hp, ap): self.ap_bar['value'] = ap self.hp_bar['value'] = hp self.ap_bar.setValue() self.hp_bar.setValue() def refreshIcons(self): count = len(self.visible_icons) start_pos = (1 - count) * 0.25 / 2 for icon in self.all_icons: if icon in self.visible_icons: self.visible_icons[icon].setPos(self.offset + (start_pos, 0, -0.08)) self.visible_icons[icon].show() start_pos += 0.21 else: self.all_icons[icon].hide() def hideOverwatch(self): if "overwatch" in self.visible_icons: self.visible_icons.pop("overwatch") self.refreshIcons() def showOverwatch(self): self.visible_icons["overwatch"] = self.all_icons["overwatch"] self.refreshIcons() def hideSetUp(self): if "set_up" in self.visible_icons: self.visible_icons.pop("set_up") self.refreshIcons() def showSetUp(self): self.visible_icons["set_up"] = self.all_icons["set_up"] self.refreshIcons()