Ejemplo n.º 1
0
def stub_runtime():
    rt = runtime.Runtime(
        latest_parent_version=False,
        logger=logutil.getLogger(__name__),
        stage=False,
        branch=None,
    )
    rt.group_config = Model()
    return rt
Ejemplo n.º 2
0
def get_runtime(args):
    """ Create runtime by running compiler as per ARGS and initializing runtime
        with result of compilation. """
    comp = get_compiler(args)
    run = runtime.Runtime(comp.delta_rules)
    tracer = runtime.Tracer()
    tracer.trace('*')
    run.tracer = tracer
    run.database.tracer = tracer
    return run
Ejemplo n.º 3
0
    def runtime(self):
        res = runtime.Runtime()

        # add listeners to runtime object
        for e in self.events:
            e.runtime = res

            res.listeners.append([e.conditionMethod, e.statementMethod])

        return res
Ejemplo n.º 4
0
def main():
    print("Python AlgoRuntime")

    rt = runtime.Runtime()
    rt.execute()
Ejemplo n.º 5
0
class InfosWindow(runtime.Widget):
    """Displays information about the game"""
    _showing = False
    _rtm = runtime.Runtime()
    _win = None
    _returnCallBack = None
    _return_button = None
    closed = True

    def __init__(self):
        runtime.Widget.__init__(self)
        self._rtm.addRoutine(self.postCheck)

    def postCheck(self):
        """Runtime routine to tell when left"""
        if self.closed == True and self._rtm.running == True:
            self.closed = True
            self._rtm.quit()

    def setReturnCallBack(self, func):
        self._returnCallBack = func

    def handleReturn(self):
        #We have to make our RTM inst quit first, else it'll fail when recalling popup()
        self._rtm.quit()
        if self._returnCallBack:
            self._returnCallBack()

    def popup(self):
        """Shows th" win"""
        if self._rtm.running == False:
            self._rtm.clear()
            self._win = pg.display.set_mode((710, 350))
            self._rtm.setWindow(self._win)
            self._rtm.addRoutine(self.postCheck)
            self.closed = False
            self.loadElements()
            self._rtm.execute()

    def loadElements(self):
        """Loads UI elements"""
        l1 = runtime.Label()
        l1.text = "Comment jouer?"
        l1.font = pg.font.SysFont(None, 64)
        l1.color = (100, 100, 255)
        l1.rect.x = 10
        l1.rect.y = 5
        self._rtm.appendObject(l1)

        self._return_button = runtime.Button()
        self._return_button.text = "Retour"
        self._return_button.rect.x = self._rtm.target_win.get_width(
        ) - 5 - self._return_button.rect.width
        self._return_button.rect.y = self._rtm.target_win.get_height(
        ) - 5 - self._return_button.rect.height
        self._return_button.setCallBack(self.handleReturn)
        self._return_button.background_clicked = (0, 255, 8)
        self._return_button.background = (76, 175, 80)
        self._rtm.appendObject(self._return_button)

        #Create the label for each line
        l2 = runtime.Label()
        l3 = runtime.Label()
        l4 = runtime.Label()
        l5 = runtime.Label()
        l6 = runtime.Label()

        l2.text = "Ici, deux joueurs s'affrontent en 1v1."
        l3.text = "Chacun a un personnage avec différentes attaques. Cependant, il n'y en a seulement 4,"
        l4.text = "pour les appeler, vérifiez vos touches.La barre de vie, les sauts, kicks et autres"
        l5.text = "coups en tout genre seront vos seules options pour battre les doigts qui sont posés"
        l6.text = "sur le même clavier."

        listed = [l2, l3, l4, l5, l6]
        i = 0
        top = 68
        left = 15
        font = pg.font.SysFont(None, 24)
        while (i < len(listed)):
            listed[i].font = font
            listed[i].rect.y = i * 26 + top
            listed[i].rect.x = left
            self._rtm.appendObject(listed[i])
            i += 1
Ejemplo n.º 6
0
class MapChooser(runtime.Widget):
    """Lets the user choose the map he wants to play with. The replies with callback to load the map."""
    _showing = False
    _rtm = runtime.Runtime()
    _win = None
    _callBack = None
    _returnCallBack = None
    _fp = ""
    _hasError = False
    _button = None
    _label = None
    _return_button = None
    closed = True

    def __init__(self):
        runtime.Widget.__init__(self)
        self._rtm.setPaintCallBack(self.winPaint)
        self._rtm.addRoutine(self.postCheck)

    def postCheck(self):
        """Runtime routine to know when the win is closed."""
        if self.closed == True and self._rtm.running == True:
            self.closed = True
            self._rtm.quit()

    def reset(self):
        self._fp = ""
        self._hasError = False

    def popup(self):
        """Shows the map chooser win"""
        if self._rtm.running == False:
            self._rtm.clear()
            if self._win != None:
                del self._win
            self._win = pg.display.set_mode((800, 805))
            self._rtm.setWindow(self._win)
            self._rtm.addRoutine(self.postCheck)
            self.closed = False
            self.loadElements()
            self._rtm.execute()

    def setCallBack(self, func):
        self._callBack = func

    def onItemClick(self, fp):
        """Click callback to update last map selected."""
        #Store the map
        self._fp = fp
        if (self._fp != ""):
            self._button.background =(100, 100, 255)
            self._button.background_clicked = (175, 175, 255)

            #Show the chosen map
            f = open(os.path.splitext(fp)[0] + ".field", "r")
            self._fp = os.path.splitext(fp)[0] + ".py"
            self._label.text = "Map choisie: " + str(f.read()).split("\n")[0]

    def setReturnCallBack(self, func):
        self._returnCallBack = func

    def handleReturn(self):
        #We have to make our RTM inst quit first, else it'll fail when recalling popup()
        self._rtm.quit()
        if self._returnCallBack:
            self._returnCallBack()

    def handleNext(self):
        """Call callback when a map have been chosen."""
        if self._callBack != None:
            if self._fp != "":
                self._rtm.quit()
                self._callBack(self._fp)

    def reset(self):
        self._hasError = False
        self._fp = ""

    def winPaint(self,  win):
        pg.draw.rect(win, (200, 200, 200),(0, 0, win.get_rect().width, win.get_rect().height))
        if (self._hasError):
            font = pygame.font.SysFont(None, 24)
            color = (255, 0, 0)
            image = font.render("Vous n'avez toujours pas choisi de map!", True, color)
            text_width, text_height = font.size(self.text)
            win.blit(image, (rect.x + ((rect.width-text_width)/2), rect.heiht - text_height - 5))

    def loadElements(self):
        """Loads UI elements"""
        j1 = runtime.Label()
        j1.text = "Choisissez la map!"
        j1.font = pg.font.SysFont(None, 64)
        j1.rect.x = 20
        self._rtm.appendObject(j1)

        self._return_button = runtime.Button()
        self._return_button.text = "Retour"
        self._return_button.rect.x = self._rtm.target_win.get_width() - 5 - self._return_button.rect.width
        self._return_button.rect.y = self._rtm.target_win.get_height() - 5 - self._return_button.rect.height
        self._return_button.setCallBack(self.handleReturn)
        self._return_button.background_clicked = (0, 255, 8)
        self._return_button.background = (76, 175, 80)
        self._rtm.appendObject(self._return_button)

        #Put the button
        self._button = runtime.Button()
        self._button.text = "Suivant >"
        self._button.font = pg.font.SysFont(None, 32)
        self._button.rect.x = 595
        self._button.rect.y = 5
        self._button.setCallBack(self.handleNext)
        self._button.background = (255, 100, 100)
        self._button.background_clicked = (255, 180, 180)
        self._rtm.appendObject(self._button)

        #Put the title
        self._label = runtime.Label()
        self._label.text = "Choisissez une map"
        self._label.font = pg.font.SysFont(None, 40)
        self._label.color = (100, 100, 255)
        self._label.rect.x = 5
        self._label.rect.y = self._win.get_rect().height - self._label.rect.height
        self._rtm.appendObject(self._label)

        mapList = os.listdir("./maps")
        compatible = {}
        i = 0

        #Load data from dir
        while i<len(mapList):
            filePath = os.path.splitext(mapList[i])
            if filePath[1] == ".image":
                if os.path.exists("./maps/" + filePath[0] + ".field"):
                    try:
                        f = open("./maps/" + filePath[0] + ".field", "r")
                        data = str(f.read()).split("\n")
                        if len(data) >= 2:
                            compatible["./maps/" + filePath[0] + ".image"] = data[0]
                        f.close()
                    except:
                        ""
                else:
                    print("./maps/" + filePath[0] + ".field", "has incoherent data when coupled with files")
            i+=1
        keys = list(compatible.keys())
        values = list(compatible.values())

        #Generate buttons from loaded data
        left = 20
        top = 60
        spacing = 5
        row = 0
        column = 0
        i = 0
        items = []
        while i<len(keys):
            it = cbi.ClickableItem(values[i])
            it.setImage(keys[i])
            it.makePaintUpdate = True
            it.setCallBack(self.onItemClick)
            it.setX(column*150 + spacing*column + left)
            it.setY(row*170 + spacing*row + top)
            column += 1
            if column == 4:
                column = 0
                row += 1
            items.append(it)
            self._rtm.appendObject(it)
            i+=1
Ejemplo n.º 7
0
class Map:
    """Default map, used as base for any loaded map."""
    _showing = False
    _rtm = runtime.Runtime()
    _win = None
    _topBar = None
    _oldW = 0
    _j1 = None
    _j2 = None
    _pendingAttacks1 = []
    _pendingAttacks2 = []
    _inAttack1 = None
    _inAttack2 = None
    _fp = None
    _onExit = None
    _deathWas1 = None
    _hasDeath = False
    closed = True
    scaled = None
    _timerHint1 = pg.time.get_ticks()
    _timerHint2 = pg.time.get_ticks()

    def __init__(self,
                 image_fp="./maps/tlalok.image",
                 win_size=(1200, 800),
                 max_y=790,
                 min_y=80,
                 pop_border_padding=10,
                 bg=(255, 255, 255),
                 progress_bg=(200, 200, 200),
                 p_fg_a=(100, 100, 255),
                 p_fg_b=(150, 150, 255),
                 text_color=(0, 0, 0)):
        self._rtm.addRoutine(self.postCheck)
        self._fp = image_fp
        self._topBar = TopBar(bg, progress_bg, p_fg_a, p_fg_b, text_color)
        self.window_size = win_size
        self.max_y = max_y
        self.min_y = min_y
        self.pop_border_padding = pop_border_padding

    def setExitCB(self, func):
        self._onExit = func

    def handleDeath(self, player):
        self._deathWas1 = player
        self._rtm.clear()
        self.deathLoadElements()

    def deathLoadElements(self):
        label = runtime.Label()
        if self._deathWas1 == True:
            label.text = "Le joueur 1 a gagné!"
        else:
            label.text = "Le joueur 2 a gagné!"
        label.rect.x = 200
        label.rect.y = 100
        self._rtm.appendObject(label)
        button = runtime.Button()
        button.text = "Retour"
        button.rect.x = 200
        button.rect.y = 200
        button.setCallBack(self._onExit)
        self._rtm.appendObject(button)
        self._hasDeath = True

    def winPaint(self, win):
        """The painting of the window"""
        if self._win != None and self._hasDeath == False:
            if self.scaled != None:
                self._win.blit(
                    self.scaled,
                    pg.Rect((0, 0),
                            (self._win.get_width(), self._win.get_height())))
            #If no win was here went setting the fp, there's no scaled image, so we build it here.
            elif self._fp != None:
                self.scaled = pg.transform.scale(pg.image.load(
                    self._fp), (self._win.get_width(), self._win.get_height()))
                self._win.blit(
                    self.scaled,
                    pg.Rect((0, 0),
                            (self._win.get_width(), self._win.get_height())))
        elif self._win != None:
            pg.draw.rect(self._win, (255, 255, 255), self._win.get_rect())

    def setImage(self, fp):
        """Set the map's image by it's path (fp)"""
        self._fp = fp
        if self._win != None:
            self.scaled = pg.transform.scale(
                pg.image.load(fp),
                (self._win.get_width(), self._win.get_height()))
        else:
            self.scaled = None

    def checkMovingAttacks(self):
        """Check collisions of the moving attacks (e.g.: an arrow) of both players"""
        if self._hasDeath == True:
            return
#Keep track of the objects to remove. We must remove them from the attacks lists. We store their indexes here
        toRm2 = []
        toRm1 = []
        i1 = 0
        i2 = 0
        #Check each list
        if (self._j2 != None):
            for a in self._pendingAttacks1:
                #If you give a static attack into a moving one, the moving one gets destroyed.
                if (self._inAttack2 !=
                        None) and (self._inAttack2.rect.colliderect(a.rect)):
                    self._inAttack2 = None
                    a.destruction()
                #Else, maybe it just touched the player
                elif self._j1.rect.colliderect(a.rect):
                    self._j1.touched(a)
                    a.destruction()
                if a.destructionFinished == True:
                    a.remove()
                    self._rtm.removeObject(a.RuntimeOUID)
                    toRm1.append(i1)
                i1 += 1
        #Check for the other player
        if (self._j1 != None):
            for a in self._pendingAttacks2:
                if (self._inAttack1 !=
                        None) and (self._inAttack1.rect.colliderect(a.rect)):
                    self._inAttack1 = None
                    a.destruction()
                elif self._j2.rect.colliderect(a.rect):
                    self._j2.touched(a)
                    a.destruction()
                if a.destructionFinished == True:
                    a.remove()
                    self._rtm.removeObject(a.RuntimeOUID)
                    toRm2.append(i2)
                i2 += 1

        #Then remove the ones that we no more need.
        i = 0
        j = 0
        while i < len(toRm1):
            self._pendingAttacks1.pop(toRm1[i + j])
            j -= 1
            i += 1
        i = 0
        j = 0
        while i < len(toRm2):
            self._pendingAttacks2.pop(toRm2[i + j])
            j -= 1
            i += 1

    def checkStaticAttacks(self):
        """Check collisions of "Static" attack, such as a kick, for both players"""
        if self._hasDeath == True:
            return
        if (self._inAttack1 != None) and (self._j2.rect.colliderect(
                self._inAttack1.rect)) and (
                    (pg.time.get_ticks() - self._timerHint2) > 100):
            print("Rect:", self._inAttack1.rect, ", Collision:",
                  (self._j2.rect.colliderect(self._inAttack1.rect)))
            self._timerHint2 = pg.time.get_ticks()
            self._j2.touched(self._inAttack1)
            self._inAttack1 = None
        if (self._inAttack2 != None) and (self._j1.rect.colliderect(
                self._inAttack2.rect)) and (
                    (pg.time.get_ticks() - self._timerHint1) > 100):
            self._timerHint1 = pg.time.get_ticks()
            self._j1.touched(self._inAttack2)
            self._inAttack2 = None

    def checkPlayersPos(self):
        """Don't send the players out of the window! It checks and moves the players when needed to keep them in"""
        if self._hasDeath == True:
            return
        if (self._j1 != None):
            if (self._j1.rect.x + self._j1.rect.width) > (
                    self._win.get_width() - self.pop_border_padding):
                self._j1.rect.x = self._win.get_width(
                ) - self.pop_border_padding - self._j1.rect.width
            elif self._j1.rect.x < self.pop_border_padding:
                self._j1.rect.x = self.pop_border_padding
        if (self._j2 != None):
            if (self._j2.rect.x + self._j2.rect.width) > (
                    self._win.get_width() - self.pop_border_padding):
                self._j2.rect.x = self._win.get_width(
                ) - self.pop_border_padding - self._j2.rect.width
            elif self._j2.rect.x < self.pop_border_padding:
                self._j2.rect.x = self.pop_border_padding

    def checkOORAttacks(self):
        """Check for Out Of Range attacks, when it goes out of th window, and remove it"""
        if self._hasDeath == True:
            return
#Keep track of the objects to remove. We must remove them from the attacks lists. We store their indexes here
        toRm2 = []
        toRm1 = []
        i1 = 0
        i2 = 0
        for a in self._pendingAttacks1:
            if ((a.rect.x + a.rect.width) <= self.pop_border_padding) or (
                    a.rect.x >
                (self._win.get_width() - self.pop_border_padding)):
                val = self._rtm.countObjects()
                a.remove()
                self._rtm.removeObject(a.RuntimeOUID)
                toRm1.append(i1)
            i1 += 1
        for a in self._pendingAttacks2:
            if ((a.rect.x + a.rect.width) < self.pop_border_padding) or (
                    a.rect.x >
                (self._win.get_width() - self.pop_border_padding)):
                val = self._rtm.countObjects()
                a.remove()
                self._rtm.removeObject(a.RuntimeOUID)
                toRm2.append(i2)
            i2 += 1

        #Then remove the ones that we no more need.
        i = 0
        j = 0
        while i < len(toRm1):
            self._pendingAttacks1.pop(toRm1[i + j])
            j -= 1
            i += 1
        i = 0
        j = 0
        while i < len(toRm2):
            self._pendingAttacks2.pop(toRm2[i + j])
            j -= 1
            i += 1

    def postCheck(self):
        """To notify the other objects that have access to this instance that it closed the win."""
        if self.closed == True and self._rtm.running == True:
            self.closed = True
            self._rtm.quit()

    def handleMA1(self, attack):
        """Callback to add a moving attack from P1"""
        self._pendingAttacks2.append(self._rtm.prependObject(attack))

    def handleMA2(self, attack):
        """Callback to add a moving attack from P2"""
        self._pendingAttacks1.append(self._rtm.prependObject(attack))

    def handleSA1(self, r):
        """Callback to add a satic attack from P1"""
        self._inAttack2 = r

    def handleSA2(self, r):
        """Callback to add a satic attack from P2"""
        self._inAttack1 = r

    def setJ1(self, j):
        """Sets the first player"""
        self._j1 = j
        #Set the callbacks
        self._j1.MACallBack = self.handleMA1
        self._j1.SACallBack = self.handleSA1
        self._j1.LPCallBack = self._topBar.setLP1
        self._j1.setDeathCB(self.handleDeath)
        #Update UI's values
        self._topBar.setMLP1(self._j1.max_lp)
        self._topBar.setLP1(self._j1.max_lp)
        #Set player's values
        self._j1.isP1 = True
        self._j1.min_y = self.min_y
        self._j1.max_y = self.max_y
        self._j1.rect.x = self.pop_border_padding
        self._j1.rect.y = self.window_size[
            1] - self._j1.rect.y - self._j1.rect.height
        self.setupJ1Keys()
        self._j1 = self._rtm.appendObject(self._j1)
        self._topBar.setP1Name(self._j1.playerName)

    def setJ2(self, j):
        """Sets the second player"""
        self._j2 = j
        self._j2.MACallBack = self.handleMA2
        self._j2.SACallBack = self.handleSA2
        self._j2.LPCallBack = self._topBar.setLP2
        self._j2.setDeathCB(self.handleDeath)
        self._topBar.setMLP2(self._j2.max_lp)
        self._topBar.setLP2(self._j2.max_lp)
        self._j2.isP1 = False
        self._j2.min_y = self.min_y
        self._j2.max_y = self.max_y
        self._j2.rect.x = self.window_size[
            0] - self.pop_border_padding - self._j2.rect.width
        self._j2.rect.y = self.window_size[
            1] - self._j2.rect.y - self._j2.rect.height
        self.setupJ2Keys()
        self._j2 = self._rtm.appendObject(self._j2)
        self._topBar.setP2Name(self._j2.playerName)

    def setupJ1Keys(self):
        """Get keys that have been set in configuration for the first player."""
        self._j1.leftKey = keys.switcher["p1_left"]
        self._j1.rightKey = keys.switcher["p1_right"]
        self._j1.downKey = keys.switcher["p1_down"]
        self._j1.upKey = keys.switcher["p1_up"]
        self._j1.saKey = keys.switcher["p1_at1"]
        self._j1.maKey = keys.switcher["p1_at2"]

    def setupJ2Keys(self):
        """Get keys that have been set in configuration for the second player."""
        self._j2.leftKey = keys.switcher["p2_left"]
        self._j2.rightKey = keys.switcher["p2_right"]
        self._j2.downKey = keys.switcher["p2_down"]
        self._j2.upKey = keys.switcher["p2_up"]
        self._j2.saKey = keys.switcher["p2_at1"]
        self._j2.maKey = keys.switcher["p2_at2"]

    def popup(self, shouldLoad=True):
        """Setup the win and Runtime env."""
        if self._rtm.running == False:
            #Set the window
            self._win = pg.display.set_mode(self.window_size)
            self._rtm.setWindow(self._win)
            #Add our routines
            self._rtm.addRoutine(self.postCheck)
            self._rtm.addRoutine(self.checkStaticAttacks)
            self._rtm.addRoutine(self.checkMovingAttacks)
            self._rtm.addRoutine(self.checkPlayersPos)
            self._rtm.addMidRoutine(self.checkOORAttacks)
            self._rtm.setPaintCallBack(self.winPaint)
            #Set the needed values
            self._rtm.afterPropagationUpdate = True
            self.closed = False
            self.loadElements()
            self._rtm.execute()

    def clear(self):
        self._rtm.clear()

    def loadElements(self):
        self._topBar = self._rtm.appendObject(self._topBar)
Ejemplo n.º 8
0
def stub_runtime():
    return runtime.Runtime(
        latest_parent_version=False,
        logger=logutil.getLogger(__name__),
        stage=False,
    )
 def test_can_init(self):
     rt = runtime.Runtime()
     self.assertIsNotNone(rt)
Ejemplo n.º 10
0
class PlayerChooser(runtime.Widget):
    """Window to let the users choose their player."""
    _rtm = runtime.Runtime()
    _win = None
    _callBack = None
    _fp1 = ""
    _fp2 = ""
    closed = True
    _hasError = False
    _label1 = None
    _label2 = None
    _button = None
    _returnCallBack = None
    _return_button = None
    quitCallBack = None

    def __init__(self):
        runtime.Widget.__init__(self)
        self._rtm.setPaintCallBack(self.winPaint)
        self._rtm.addRoutine(self.postCheck)

    def postCheck(self):
        """Runtime routine to know when window is left."""
        if self.closed == True and self._rtm.running == True:
            self.closed = True
            self._rtm.quit()
            if (self.quitCallBack != None):
                self.quitCallBack()

    def reset(self):
        self._fp1 = ""
        self._fp2 = ""
        self._hasError = False

    def popup(self):
        """Shows the win"""
        if self._rtm.running == False:
            self._rtm.clear()
            self._win = pg.display.set_mode((900, 800))
            self._rtm.setWindow(self._win)
            self._rtm.addRoutine(self.postCheck)
            self.closed = False
            self.loadElements()
            self._rtm.execute()

    def setCallBack(self, func):
        self._callBack = func

    def setReturnCallBack(self, func):
        self._returnCallBack = func

    def handleReturn(self):
        #We have to make our RTM inst quit first, else it'll fail when recalling popup()
        self._rtm.quit()
        if self._returnCallBack:
            self._returnCallBack()

    def onItemClick1(self, fp):
        """Callback to store the players chosen."""
        #Show the chosen map
        f = open(os.path.splitext(fp)[0] + ".field", "r")
        splitted = str(f.read()).split("\n")
        self._label1.text = "Joueur 1: " + splitted[0]
        #Store the map
        self._fp1 = "./players/" + splitted[1] + "/" + splitted[1] + ".py"
        #Change button's color to say that user can click on it
        if self._fp2 != "" and self._fp1 != "":
            self._button.background = (100, 100, 255)
            self._button.background_clicked = (175, 175, 255)

    def onItemClick2(self, fp):
        """Callback to store the players chosen."""
        #Show the chosen map
        f = open(os.path.splitext(fp)[0] + ".field", "r")
        splitted = str(f.read()).split("\n")
        self._label2.text = "Joueur 2: " + splitted[0]
        #Store the map
        self._fp2 = "./players/" + splitted[1] + "/" + splitted[1] + ".py"
        #Change button's color to say that user can click on it
        if self._fp2 != "" and self._fp1 != "":
            self._button.background = (100, 100, 255)
            self._button.background_clicked = (175, 175, 255)

    def handleNext(self):
        """Calls callback when everyone chose their player."""
        if self._callBack != None:
            if (self._fp2 != "" and self._fp1 != ""):
                self._rtm.quit()
                self._callBack(self._fp1, self._fp2)
            else:
                self._hasError = True
                self.makePaintUpdate = True

    def winPaint(self, win):
        """Paints the window"""
        pg.draw.rect(win, (200, 200, 200),
                     (0, 0, win.get_rect().width, win.get_rect().height))

    def generateClickablesFrom(self, dicted, cb, left=20):
        keys = list(dicted.keys())
        values = list(dicted.values())
        top = 60
        spacing = 5
        row = 0
        column = 0
        i = 0
        while i < len(keys):
            it = cbi.ClickableItem(values[i])
            it.setImage(keys[i])
            it.makePaintUpdate = True
            it.setCallBack(cb)
            it.setX(column * 150 + spacing * column + left)
            it.setY(row * 170 + spacing * row + top)
            column += 1
            if column == 2:
                column = 0
                row += 1
            self._rtm.appendObject(it)
            i += 1

    def loadElements(self):
        """Loads and displays UI elements."""
        j1 = runtime.Label()
        j1.text = "Choisissez un joueur!"
        j1.font = pg.font.SysFont(None, 64)
        j1.rect.x = 20
        self._rtm.appendObject(j1)

        self._return_button = runtime.Button()
        self._return_button.text = "Retour"
        self._return_button.rect.x = self._rtm.target_win.get_width(
        ) - 5 - self._return_button.rect.width
        self._return_button.rect.y = self._rtm.target_win.get_height(
        ) - 5 - self._return_button.rect.height
        self._return_button.setCallBack(self.handleReturn)
        self._return_button.background_clicked = (0, 255, 8)
        self._return_button.background = (76, 175, 80)
        self._rtm.appendObject(self._return_button)

        #Put the button
        self._button = runtime.Button()
        self._button.text = "Suivant >"
        self._button.font = pg.font.SysFont(None, 32)
        self._button.rect.x = self._rtm.target_win.get_width(
        ) - 5 - self._button.rect.width
        self._button.rect.y = 5
        self._button.setCallBack(self.handleNext)
        self._button.background = (255, 100, 100)
        self._button.background_clicked = (255, 180, 180)
        self._rtm.appendObject(self._button)

        mapList = os.listdir("./players/profiles")
        compatible = {}
        i = 0

        while i < len(mapList):
            filePath = os.path.splitext(mapList[i])
            if filePath[1] == ".image":
                if os.path.exists("./players/profiles/" + filePath[0] +
                                  ".field"):
                    try:
                        f = open(
                            "./players/profiles/" + filePath[0] + ".field",
                            "r")
                        data = str(f.read()).split("\n")
                        if len(data) >= 2:
                            compatible["./players/profiles/" + filePath[0] +
                                       ".image"] = data[0]
                        f.close()
                    except:
                        ""
                else:
                    print("./players/profiles/" + filePath[0] + ".field",
                          "has incoherent data when coupled with files")
            i += 1

        #Load the clickables
        self.generateClickablesFrom(compatible, self.onItemClick1)
        self.generateClickablesFrom(compatible, self.onItemClick2, 400)

        #Show the labels to let users know which player have been chosen
        self._label1 = runtime.Label()
        self._label1.text = "Choisissez le J1"
        self._label1.font = pg.font.SysFont(None, 40)
        self._label1.color = (100, 100, 255)
        self._label1.rect.x = 5
        self._label1.rect.y = self._win.get_rect(
        ).height - self._label1.rect.height
        self._rtm.appendObject(self._label1)

        self._label2 = runtime.Label()
        self._label2.text = "Choisissez une J2"
        self._label2.font = pg.font.SysFont(None, 40)
        self._label2.color = (100, 100, 255)
        self._label2.rect.x = 400
        self._label2.rect.y = self._win.get_rect(
        ).height - self._label2.rect.height
        self._rtm.appendObject(self._label2)
Ejemplo n.º 11
0
import runtime as rt

runtime = rt.Runtime()
runtime.run()
Ejemplo n.º 12
0
class KeysSettings(runtime.Widget):
    """Displays and let the user choose which keys he wants to use while playing."""
    _showing = False
    _rtm = runtime.Runtime()
    _win = None
    _returnCallBack = None
    _return_button = None
    closed = True
    quitCallBack = None

    def __init__(self):
        runtime.Widget.__init__(self)
        self._rtm.addRoutine(self.postCheck)

    def setReturnCallBack(self, func):
        self._returnCallBack = func

    def postCheck(self):
        """Leave routine to handle callback"""
        if self.closed == True and self._rtm.running == True:
            self.closed = True
            self._rtm.quit()
            if self.quitCallBack != None:
                self.quitCallBack()

    def popup(self):
        """Shows the window"""
        if self._rtm.running == False:
            self._rtm.clear()
            self._win = pg.display.set_mode((600, 600))
            self._rtm.setWindow(self._win)
            self._rtm.addRoutine(self.postCheck)
            self.closed = False
            self.loadElements()
            self._rtm.execute()

    def loadElements(self):
        """Loads the UI elements and display 'em"""
        j1 = runtime.Label()
        j2 = runtime.Label()

        self._return_button = runtime.Button()
        self._return_button.text = "Retour"
        self._return_button.rect.x = self._rtm.target_win.get_width(
        ) - 5 - self._return_button.rect.width
        self._return_button.rect.y = self._rtm.target_win.get_height(
        ) - 5 - self._return_button.rect.height
        self._return_button.setCallBack(self._returnCallBack)
        self._return_button.background_clicked = (0, 255, 8)
        self._return_button.background = (76, 175, 80)
        self._rtm.appendObject(self._return_button)

        list1 = []
        list2 = []
        for i in range(0, 14):
            list1.append(KeyButton())
        for i in range(0, 7):
            list2.append(runtime.Label())

        #Put the titles
        j1.text = "Joueur 1"
        j2.text = "Joueur 2"
        j1.font = pg.font.SysFont(None, 64)
        j2.font = pg.font.SysFont(None, 64)
        j1.rect.x = 20
        j2.rect.x = 320

        #The sublabels
        list2[0].text = "Aller à gauche"
        list2[1].text = "Aller à droite"
        list2[2].text = "Sauter"
        list2[3].text = "Se baisser"
        list2[4].text = "Attaque 1"
        list2[5].text = "Attaque 2"
        list2[6].text = "Attaque 3"

        global switcher
        keys = list(switcher.values())

        self._rtm.appendObject(j1)
        self._rtm.appendObject(j2)

        #Add the buttons and the labels to know the used keys
        for i in range(0, 14):
            list1[i].rect.height = 40
            list1[i].setKey(keys[i])
            self._rtm.appendObject(list1[i])
        for i in range(0, 7):
            list2[i].rect.height = 20
            self._rtm.appendObject(list2[i])

        topBase = 60
        padding = 10
        for i in range(0, 7):
            list2[i].font = pg.font.SysFont(None, 20)
            list1[i].font = pg.font.SysFont(None, 40)
            list1[i + 7].font = pg.font.SysFont(None, 40)
            list2[i].rect.x = 20
            list2[i].rect.y = topBase + i * 60 + i * padding
            list1[i].rect.x = 20
            list1[i + 7].rect.x = 320
            y = list2[i].rect.y + 20
            list1[i].rect.y = y
            list1[i + 7].rect.y = y