[SOLVED]sort list

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

[SOLVED]sort list

#1 Post by senisanti »

Hi everyone.

I want to create a ranking of players according to their points.
The one who has the most points on top of the ranking.

I use then the sort list initialization.

But it doesn't work at all.

Can you tell me what I am doing wrong in my code?

Code: Select all

init python:
    class SortList(Action):
            def __init__(self, ranking):
                self.list = ranking
            def __call__(self):
                self.list.sort()
                return True

screen ranking():
    add "computer"
    vbox xalign 0.99 yalign 0.01:
        spacing 20
        imagebutton auto "icon/buttonclose_%s.png" action Hide("ranking")
    text "Mendoza [mendopoint]pts" align (0.60, 0.40)
    text "Sinero [sinepoint]" align (0.30, 0.08)
    text "Gibbs [gibpoint]" align (0.30, 0.12)
    text "Alonzo [alonpoint]" align (0.30, 0.16)
    text "Gonçalva [goncapoint]" align (0.30, 0.20)
    text "Garrido [garripoint]" align (0.30, 0.24)
    text "Enciso [encipoint]" align (0.30, 0.28)
    text "Saima Lozano [lozapoint]" align (0.30, 0.32)
    
    
Last edited by senisanti on Sun May 22, 2022 8:04 am, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: sort list

#2 Post by Alex »

senisanti wrote: Sat May 21, 2022 5:34 am ...But it doesn't work at all. ...
In this code sample you doesn't use your class.

You can sort list right inside the screen

Code: Select all

screen sort_scr():
    hbox:
        align(0.5, 0.1)
        spacing 20
        vbox:
            spacing 10
            text "Original list"
            for res in a:
                $ name, score = res
                text "{} - {} pts".format(name, score)
                
        vbox:
            spacing 10
            text "List sorted by points"
            for res in sorted(a, key=lambda pts: pts[1], reverse=True):
                $ name, score = res
                text "{} - {} pts".format(name, score)
                
        vbox:
            spacing 10
            text "List sorted by points and then by names"
            for res in sorted(sorted(a, key=lambda n: n[0], reverse=False), key=lambda pts: pts[1], reverse=True):
                $ name, score = res
                text "{} - {} pts".format(name, score)

# The game starts here.
label start:
    $ a = [('B', 3), ('D', 5), ('B', 4), ("C", 4), ('A', 5)]
    show screen sort_scr
    "?!"
https://wiki.python.org/moin/HowTo/Sorting/

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#3 Post by senisanti »

I'm sorry I don't understand anything, I'll need more explanation if you want help me.

I've already read the link you gave me and a lot of other renpy documentation on sort lists and with all that I tried for two days to create my code and I didn't do better than the one I gave as an example.

Obviously I don't understand anything.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: sort list

#4 Post by Alex »

senisanti wrote: Sat May 21, 2022 7:45 am I'm sorry I don't understand anything, I'll need more explanation if you want help me.

I've already read the link you gave me and a lot of other renpy documentation on sort lists and with all that I tried for two days to create my code and I didn't do better than the one I gave as an example.

Obviously I don't understand anything.
Mmm, in your code sample you've created class and made a screen. But you didn't use your class anywhere. And you unable to sort a screen.

In 'ranking' screen you have some text lines that are positioned - and there's no option to change their positions.

If you need to show sorted results, you need a list of result - then you could sort this list and iterate through it to position text dynamically. This is the code sample in my previous post.

I'm unable to tell you about classes and all, so if you need some rather complex system that required classes, you'll have to ask someone else.

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#5 Post by senisanti »

There is that i do

Code: Select all

screen sort_scr():
    hbox:
        align(0.5, 0.1)
        spacing 20
        imagebutton auto "icon/buttonclose_%s.png" action Hide("sort_scr"), Jump("aftertest")
        vbox:
            spacing 10
            text "List sorted by points and then by names"
            for res in sorted(sorted(a, key=lambda n: n[0], reverse=False), key=lambda pts: pts[1], reverse=True):
                $ name, score = res
                text "{} - {} pts".format(name, score)


label start:
    scene black
    $ a = [('Mendoza', 3), ('Sinero', 5), ('Gibbs', 4), ("Alonzo", 10), ('Goncalva', 8), ('Garrido', 22), ('Enciso', 34), ('Lozano', 1)]
    call screen sort_scr

label aftertest:
    scene black
    "blabla"
    "fdsdfd"
    call screen sort_scr

It works very well, already thank you very much for this code but I do not understand where the values are from.
If I want to change the score of the players, I have to add a line like this?
$ a = [('Mendoza', 33), ('Sinero', 45), ('Gibbs', 42), ("Alonzo", 17), ('Goncalva', 84), ('Garrido', 28), ('Enciso', 74), ('Lozano', 101)]

removed the lists by point and original lists, what are they for?

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#6 Post by senisanti »

Oops, didn't know you were still connected.
I made my second post while you were answering the first one.

I don't want something more complex, I'm already having trouble understanding this one.
I need help to understand what you gave me.

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#7 Post by senisanti »

I also have another important question about the code.

The player does not appear in the list but I have to add it.
But the player will put a score that depends on his choices in the game.
How can I add the score of the player which is random.
I tried like this, but it doesn't work.

Code: Select all

default playerscore = 0

label start:
    $ playerscore += 48
    $ a = [('Mendoza', 3), ('Sinero', 5), ('Gibbs', 4), ('Alonzo', 10), ('Goncalva', 8), ('Garrido', 22), ('Enciso', 34), ('Lozano', 1), (Name, [playerscore])]


User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: sort list

#8 Post by Alex »

senisanti wrote: Sat May 21, 2022 8:05 am ...If I want to change the score of the players, I have to add a line like this?
$ a = [('Mendoza', 33), ('Sinero', 45), ('Gibbs', 42), ("Alonzo", 17), ('Goncalva', 84), ('Garrido', 28), ('Enciso', 74), ('Lozano', 101)]...
Well, yes - you need to operate the list of results somehow.
To make things easier, you can create a function that will add values to the list and sort this list.
senisanti wrote: Sat May 21, 2022 8:05 am ... removed the lists by point and original lists, what are they for?
They was meant to show how looks the original list and the result of sorting it.

Another sample with comments.

Code: Select all

default a = [] # list to store results

init python:
    def add_result(res):
        """
        Function that adds new items to the list 'a' and sorts list 'a'.
        It takes 'res' - the list [name, score].
        """
        
        # function will use global variable 'a'
        global a
        
        # add new item at the end of list 'a'
        a.append(res)
        # and sort the list with the new item
        a = sorted(sorted(a, key=lambda n: n[0], reverse=False), key=lambda pts: pts[1], reverse=True)
        
    def add_and_update_result(res):
        """
        Function that adds new items to the list 'a' if there's no item
        with this name, or updates the score for the item with this name
        and sorts list 'a'.
        It takes 'res' - the list [name, score].
        """
        
        # function will use global variable 'a'
        global a
        
        # local variable to track if 'a' already has this name
        new_name = True
        # check all items
        for player_res in a:
            # if new item has the same name as current item in 'a'
            if res[0] == player_res[0]:
                # it's not a new name
                new_name = False
                # update the score for this name if new score is greater
                if res[1] > player_res[1]:
                    player_res[1] = res[1]
        # if we got new name
        if new_name:
            # add it to the list 'a'
            a.append(res)
        # and sort the list with the new data
        a = sorted(sorted(a, key=lambda n: n[0], reverse=False), key=lambda pts: pts[1], reverse=True)

screen sorted_scr():
    vbox:
        align(0.5, 0.1)
        text "Results:"
        for res in a[:10]: # will show first 10 items in 'a'
            $ name, pts = res
            text "{} - {} pts".format(name, pts)


# The game starts here.
label start:
    $ Name = '{color=#c00}Player{/color}'
    $ a = []
    show screen sorted_scr
    "Test #1 will show a list that stores all results for all players (click to continue)."
    # use function to add results to list 'a'
    # note to put [name, score] in square brackets
    $ add_result(['Mendoza', 3])
    $ renpy.pause()
    $ add_result(['Sinero', 5])
    $ renpy.pause()
    $ add_result(['Gibbs', 4])
    $ renpy.pause()
    $ add_result(['Alonzo', 10])
    $ renpy.pause()
    $ add_result(['Goncalva', 8])
    $ renpy.pause()
    $ add_result(['Garrido', 22])
    $ renpy.pause()
    $ add_result(['Enciso', 34])
    $ renpy.pause()
    $ add_result(['Lozano', 1])
    $ renpy.pause()
    
    $ playerscore = 7
    $ add_result([Name, playerscore])
    $ renpy.pause()
    $ playerscore = 30
    $ add_result([Name, playerscore])
    "Since screen shows 10 entries of a list,"
    $ playerscore = 777
    $ add_result([Name, playerscore])
    "the other results won't be shown."
    
    $ a = []
    "Test #2 will show a list that stores and updates results for all players."
    # use another function to add results to list 'a'
    $ add_and_update_result(['Mendoza', 3])
    $ renpy.pause()
    $ add_and_update_result(['Sinero', 5])
    $ renpy.pause()
    $ add_and_update_result(['Gibbs', 4])
    $ renpy.pause()
    $ add_and_update_result(['Alonzo', 10])
    $ renpy.pause()
    $ add_and_update_result(['Goncalva', 8])
    $ renpy.pause()
    $ add_and_update_result(['Garrido', 22])
    $ renpy.pause()
    $ add_and_update_result(['Enciso', 34])
    $ renpy.pause()
    $ add_and_update_result(['Lozano', 1])
    $ renpy.pause()
    
    $ playerscore = 7
    $ add_and_update_result([Name, playerscore])
    "[Name] got result."
    $ playerscore = 30
    $ add_and_update_result([Name, playerscore])
    "[Name] got better result."
    $ playerscore = 777
    $ add_and_update_result([Name, playerscore])
    "[Name] got high score!"

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#9 Post by senisanti »

Thanks, it's great what you've done, I love the renpy.pause thing, the results appear in turn, it's great.

I'm going to use the two methods you suggest for my game, the second method with the label for the end of each competition so that the player can see the results as if he was live.
and the screen I will put it in the computer to allow me to make the total of the championship.

It's great and the explanation to tell me what each thing is used for is very nice.


But I'm going to bother you some more.
For the player part

Code: Select all

$ playerscore = 7
    add_and_update_result([Name, playerscore])
    "[Name] got result."
    $ playerscore = 30
    $ add_and_update_result([Name, playerscore])
    "[Name] got better result."
    $ playerscore = 777
    $ add_and_update_result([Name, playerscore])
    "[Name] got high score!"
With this code I can put the name of the player and in red that's great too.
But I would like the result also to be randomly configurable.

playerscore = 30
or
$ playerscore = [mypoints] (it doesn't work but I would like this kind of thing, the player can score points in the championship but it depends on the choices he makes during the competition, I can't know how many points he will score).

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: sort list

#10 Post by Alex »

senisanti wrote: Sun May 22, 2022 2:02 am ...But I would like the result also to be randomly configurable.

playerscore = 30
or
$ playerscore = [mypoints] (it doesn't work but I would like this kind of thing, the player can score points in the championship but it depends on the choices he makes during the competition, I can't know how many points he will score).
Well, you don't need extra variables: if you track player's score in 'mypoints' than just use it

Code: Select all

$ add_and_update_result([Name, mypoints])
Square brackets are used:
- for lists (my_list = ['a', 'b', 'c']),
- for indexes and keys when you taking data from lists and dictionaries (res = my_list[1], res = my_dict['key_name']),
- to show value of a variable in text ("Hello, [Name]!") - https://www.renpy.org/doc/html/text.htm ... ating-data

So, in game it will look like

Code: Select all

default playerscore = 0 # var to keep score
label start:
    $ Name = renpy.input("What's your name?")
    "..."
    menu menu_1:
        "Correct choice":
            $ playerscore += 3
        "Best choice":
            $ playerscore += 5
        "Random choice":
            $ playerscore += ranpy.random.randint(0, 2)
        "Wrong choice":
            pass
    "... ..."
    menu menu_2:
        "Correct choice":
            $ playerscore += 3
        "Best choice":
            $ playerscore += 5
        "Random choice":
            $ playerscore += ranpy.random.randint(0, 2)
        "Wrong choice":
            pass
    # add the result - then say about it
    $ add_and_update_result([Name, playerscore])
    "Done"
    "[Name], you got [playerscore] pts."
https://www.renpy.org/doc/html/other.html#renpy-random

User avatar
senisanti
Regular
Posts: 94
Joined: Tue May 07, 2019 4:37 am
Projects: Allball, Smash scrap metal, Santi...
Location: Rennes
Contact:

Re: sort list

#11 Post by senisanti »

Sorry, I didn't understand that I had the right to put several with different names.

So now I'll even put one for each AI player.
It will allow me to put them in random, it's even better.

Thanks a lot Alex.
I was just asking for a small thing, you gave much more than I asked for, even better than I could have imagined, it's great.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], konimyun