[Ren'Py] Тест с 4 вариантами ответов и рандомным порядком вопросов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init python: | |
# Просто вспомогательный класс для хранения информации о вопросе, можно обойтись и картежом, но это было бы не сильно удобно | |
class Question: | |
def __init__(self, text, correct, *variants): | |
if len(variants) != 4: | |
raise Exception("Должно быть 4 варианта ответа") | |
self.text = text | |
self.correct = correct - 1 | |
self.variants = variants | |
define questions = ( | |
Question(_('Why you are gae?'), 2, _("I'm not gay!"), _('Who says im gay?'), _('Fuck off!'), _("I'm a lesbian.")), | |
Question(_('Сколько длилась Столетняя война?'), 4, _('100'), _('100'), _('100'), _('116')), | |
Question(_('Когда началась Вторая мировая война?'), 2, _('1941'), _('1939'), _('ШТО?'), _('1945')) | |
) # Список вопросов. Сначала идёт текст вопроса, далее номер правильно ответа(начиная с 1), а затем варианты ответа | |
define variant_prefixes = (_('А'), _('Б'), _('В'), _('Г')) # Префиксы для вариантов ответа | |
define test_hp = 2 # начальное количество жизней | |
screen question_screen: | |
frame: | |
padding (8, 8, 8, 8) | |
xalign .5 | |
yalign .5 | |
xsize 800 | |
vbox: | |
spacing 40 | |
text "%s. %s" % (question_index + 1, __(question.text)) | |
grid 2 2: | |
xfill True | |
# экран должен возращать индекс выбранного ответа | |
for i, variant in enumerate(question.variants): | |
textbutton "%s. %s" % (__(variant_prefixes[i]), __(variant)) action Return(i) | |
image gray = Solid('#333') | |
label start: | |
scene gray | |
"Время пройти тест!" | |
jump test | |
label test: | |
$ question_count = len(questions) | |
$ questions_order = list(range(0, question_count)) # создаём список в котором хранятся индексы(номера) вопросов | |
$ renpy.random.shuffle(questions_order) # перемешиваем индексы вопросов | |
$ question_index = 0 | |
$ score = 0 | |
$ hp = test_hp | |
jump test_loop | |
label test_loop: | |
# если вопросы закончились, то прыгаем в лейбл test_complete | |
if question_index >= question_count: | |
jump test_complete | |
$ question = questions[questions_order[question_index]] # получаем текущий вопрос по его индексу | |
call screen question_screen # вызываем экран, в котором будет отображён текст вопроса и варианты ответа | |
# экран возвращает индекс ответа(хранится в переменной _return), который выбрал игрок | |
if _return == question.correct: | |
"Верно!" | |
$ score += 1 | |
else: | |
"Неверно!" | |
$ hp -= 1 | |
$ question_index += 1 | |
# если жизни закончились, то прыгаем в test_failed | |
if hp == 0: | |
jump test_failed | |
jump test_loop | |
label test_failed: | |
"У вас закончились жизни. Количество набранных очков сброшено до нуля." | |
# сбрасываем счёткик жизней и очков | |
$ score = 0 | |
$ hp = test_hp | |
jump test_loop | |
label test_complete: | |
"Тест пройден. Количество набранных очков: [score]." | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment