How can I make a quiz?

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
rukiruki
Newbie
Posts: 1
Joined: Mon Dec 22, 2014 7:35 pm
Contact:

How can I make a quiz?

#1 Post by rukiruki »

Hello everyone. I've just signed up to the website to finally ask a question.

I'm thinking of making a video game character quiz where you get to choose from a number of questions and the game comes up with a result for you based on your decisions. I wonder if people do this with Ren'py?
https://www.playbuzz.com/jonb10/which-norse-god-are-you

I need three things: the characters, the questions and a results table.

I have the characters.

Code: Select all

init:
    $ mage = 0
    $ hunter = 0
    $ guardian = 0
    $ assassin = 0
    $ warrior = 0
I have the questions.

Code: Select all

    menu:
        "Mage":
            $ mage += 1
        "Hunter":
            $ hunter += 1
        "Guardian":
            $ guardian += 1
        "Assassin":
            $ assassin += 1
        "Warrior":
            $ warrior += 1
What I can't seem to figure out is how to create a results table where the character with the highest number gets chosen. If I set lots of them at a time, it only selects the one closest to the top and disregards the rest of the characters. For example, if I had the most points in hunter but my mage has more points than guardians, it will jump to mage. How do I counter this?

Code: Select all

    if mage > hunter:
        jump mage
        
    if mage > guardian:
        jump mage
        
    if mage > assassin:
        jump mage
        
    if mage > warrior:
        jump mage
        
    if hunter > mage:
        jump hunter
My coding experience is extremely insufficient. If I had to try to write it without knowing what code it needed, it would probably look something like this:

Code: Select all

    if mage > hunter,guardian,assassin,warrior:
        jump mage
This doesn't seem to work though. I thank you in advance.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How can I make a quiz?

#2 Post by philat »


Breci
Newbie
Posts: 9
Joined: Thu Dec 18, 2014 4:07 pm
Organization: Edarion
IRC Nick: Breci
Skype: albatorb
Location: France
Contact:

Re: How can I make a quiz?

#3 Post by Breci »

In case someone need a solution I made a quick one.

So I used what is the easiest to expand in my personal opinion.

Why I am doing it like this?
I don't like having long "if" statement to check everything.
I personally like to have a code that can be changed or improved over time so I adapted the data structure for it. With it it's possible to add new scores during the game or even remove some without having to change the algorithm to find the maximum.

Data structure:
You can easily add more fields in the dictionary in your code or in the initialisation

Code: Select all

define classes = {'mage': 0,'hunter': 0,'guardian': 0,'assassin': 0, 'warrior': 0}
python functions:
Simply find the highest value and return the class name, not the value itself.

Code: Select all

init python:

    def highestScore(classes):
        max = 0
        res = ""
        for char in classes:
            if max < classes[char]:
                res = char
                max = classes[char]
        return res
How to change the value of a score :
Increase by 1:

Code: Select all

$ classes['mage'] += 1
Change value:

Code: Select all

$ classes['mage'] = 10
Getting and testing the result:

Code: Select all

    $ res = highestScore(classes)
    if res == 'mage':
        jump mage
    if res == 'hunter':
        jump hunter
    if res == 'guardian':
        jump guardian
    if res == 'assassin':
        jump assassin
    if res == 'warrior':
        jump warrior
    "[res] : empty" #To test if no max is found 
If you want to display a value:

Code: Select all

    $ nb = classes['mage']
    "mage : [nb]"

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How can I make a quiz?

#4 Post by philat »

Breci wrote:

Code: Select all

init python:

    def highestScore(classes):
        max = 0
        res = ""
        for char in classes:
            if max < classes[char]:
                res = char
                max = classes[char]
        return res
Have you thought about how to deal with ties? AFAIK, dictionaries are unordered, so if there were ties (i.e. both mage and hunter scored 3, for example) this would return the first key/value pair evaluated at random. Maybe you're fine with that, but it seems like something that should be pointed out.

Breci
Newbie
Posts: 9
Joined: Thu Dec 18, 2014 4:07 pm
Organization: Edarion
IRC Nick: Breci
Skype: albatorb
Location: France
Contact:

Re: How can I make a quiz?

#5 Post by Breci »

The only reference he provided is a basic online quizz. I don't think they are taking care about it.
Also, I'm based on his own code where he don't say that he try to handle it and don't have any line about it.

So yeah I assumed he does not care about it.

Post Reply

Who is online

Users browsing this forum: No registered users