谁想成为百万富翁

2024-09-29 22:34:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用pygame编程“谁想成为百万富翁”。 现在我正在努力建立一个50:50的小丑。你知道吗

我已经设置了下面显示的代码。 这将绘制4个矩形并输入问题的文本。你知道吗

import pygame

pygame.init ()
font = pygame.font.SysFont ('Arial', 22)
window = pygame.display.set_mode ((1680, 945))
pygame.display.set_caption ("Who will become a millionaire?")

def draw_button (x, y, answer, width, height, bgcolor):
    pygame.draw.rect (window, bgcolor, (x, y, width, height))
    text = font.render (answer, False, (0, 0, 0))
    window.blit (text, (x + 10, y + 10))

draw_button(100, 600, question["answers"][0], 450, 70, (0,0,255))
draw_button(650, 600, question["answers"][1], 450, 70, (0,0,255))
draw_button(100, 750, question["answers"][2], 450, 70, (0,0,255))
draw_button(650, 750, question["answers"][3], 450, 70, (0,0,255))

现在我想做的是,如果用户选择按KP1来使用50:50小丑,我想用一个空矩形覆盖3个错误答案中的2个。 从3个选项中随机选择2个并覆盖它们的最佳方法是什么?你知道吗

def wait ():
    running = true
    while running:
        for ev in pygame.event.get ():

            if ev.type == pygame.QUIT:
                return len (monies)
            elif ev.type == pygame.KEYDOWN:

                if ev.key == pygame.K_KP1: # 50: 50
                    draw_joker_chosen (150.50)

                    if current_question ["rightQ"] == 0:
                        fifty_fifty_a ()
                        delete_joker (150.50)

                    elif current_question ["rightQ"] == 1:
                        fifty_fifty_b ()
                        delete_joker (150.50)

                    elif current_question ["rightQ"] == 2:
                        fifty_fifty_c ()
                        delete_joker (150.50)

                    elif current_question ["rightQ"] == 3:
                        fifty_fifty_d ()
                        delete_joker (150.50)

我希望fifty_fifty函数从3个列表中随机选取2个错误答案,并覆盖它们,例如,如果答案a是正确的,我想随机调用这3个答案中的两个。你知道吗

b = pygame.draw.rect(window, (0,0,255), (650, 600, 450,70))
c = pygame.draw.rect(window, (0,0,255), (100, 750, 450,70))
d = pygame.draw.rect(window, (0,0,255), (650, 750, 450,70))
wrong = [b,c,d]
...

我能给它们分配变量并把它们放到一个列表中,然后从列表中随机选取2个并只执行它们吗?你知道吗

我试过一些东西,但没成功。你知道吗


Tags: 答案rectbuttoncurrentwindowdeletepygameanswers
1条回答
网友
1楼 · 发布于 2024-09-29 22:34:09

你想要什么还不完全清楚。我认为下面的代码应该会给你一些启发。你知道吗

import random


def randomly_discard_two_wrong_answers(answers, correct_answer_index):
    false_answers = answers[:correct_answer_index] + answers[correct_answer_index + 1:]
    discarded_answers = random.sample(false_answers, 2)
    return [answer if answer not in discarded_answers else None for answer in answers]


if __name__ == '__main__':
    answers = ['A', 'B', 'C', 'D']
    for i in range(10):
        new_answers = randomly_discard_two_wrong_answers(answers, 2)
        print(new_answers)

结果:

[None, None, 'C', 'D']
[None, 'B', 'C', None]
[None, None, 'C', 'D']
['A', None, 'C', None]
[None, None, 'C', 'D']
['A', None, 'C', None]
[None, 'B', 'C', None]
[None, None, 'C', 'D']
[None, None, 'C', 'D']
[None, None, 'C', 'D']

相关问题 更多 >

    热门问题