如何一次显示一个函数并在python中输入返回值?

时间:2018-07-01 02:05:28

标签: python pygame python-3.6

我总共有5个问题,我希望将它们随机调用,但是我的问题是我无法一次在屏幕上显示一个问题,一直等到用户选择答案。我现在拥有的代码只是切换问题,而不是只关注一个问题。

在我发布的代码下面,只有我遇到的主要问题是要测试的2个问题,因此在代码中,随机选择的问题只有2个。我正在使用的python版本是3.6,任何帮助将不胜感激。 谢谢。

  

游戏画面

def game_screen():

    player_score = 25
    actual_score = 0
    wrong_answer = 0
    timer = pygame.time.get_ticks()
    seconds = 0
    start = True

    question_list = [question1, question2]

    while start :
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        seconds = (pygame.time.get_ticks()- timer)/1000

        main_font = pygame.font.Font("ArialRounded.TTF", 22)
        sub_font = pygame.font.Font("ArialRounded.TTF", 22)
        timer_font = sub_font.render(str(seconds), True, SEABLUE)
        question_font = main_font.render("Question:", True, SEABLUE)

        star_img = pygame.image.load("starscore.png")
        menu_screen_img = pygame.image.load("quizzappbackgroundscreen.png")
        blureffect_img = pygame.image.load("blureffect.png")
        onoff_button_img = pygame.image.load("onoffbutton.png")
        knobone_img = pygame.image.load("knob_a.png")
        knobtwo_img = pygame.image.load("knob_a.png")

        knobrect_a = knobone_img.get_rect(center=(97.5,647.5))
        knobrect_b = knobtwo_img.get_rect(center=(514.5,647.5))
        mpos = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if knobrect_a.collidepoint(mpos):
            knobone_img = pygame.image.load("knob_b.png")
            if click[0] == 1:
                knobone_img = pygame.image.load("rotatedknob_a.png")
                click_sound.set_volume(0.3)
                click_sound.play()

        if knobrect_b.collidepoint(mpos):
            knobtwo_img = pygame.image.load("knob_b.png")
            if click[0] == 1:
                knobtwo_img = pygame.image.load("rotatedknob_a.png")
                click_sound.set_volume(0.3)
                click_sound.play()

        screen.blit(menu_screen_img, [0,0])
        screen.blit(star_img, [50,47])
        screen.blit(timer_font, [485,55])
        screen.blit(question_font, [50,95])


        actual_score = random.choice(question_list)(player_score)
        #actual_score = question1(player_score)
        #player_score += actual_score

        if player_score > 25:
            player_score = 25

        draw_score(player_score)
        screen.blit(blureffect_img, [0,0])
        screen.blit(onoff_button_img, [25,726])
        screen.blit(knobone_img, [50,599])
        screen.blit(knobtwo_img, [465,599])

        pygame.display.update()

1 个答案:

答案 0 :(得分:0)

我猜您的特定问题是由于您未能连贯或正确地跟踪游戏状态。我不得不猜测,因为您不会提供一个展示您所描述的问题的示例,而且如果不生成大量填充资源并猜测其他未定义函数的实现,就无法运行您的代码。

下面是一个最少显示询问多个问题的示例,按一个键开始询问,然后单击鼠标左键或鼠标右键回答。

它不需要执行任何其他资源,并且,如果您注释掉所指示的行,将表现出与您描述的问题相同的问题:每帧循环问题(无需等待答案)。

import pygame
import random

pygame.init()
pygame.font.init()

clock = pygame.time.Clock()
screen = pygame.display.set_mode([320,240])
sys_font = pygame.font.SysFont(pygame.font.get_default_font(), 18)

pygame.display.set_caption("Quiz")
nums_left = 0  # initialise the  counters
nums_right = 0  
done = False

questions = ["Who?", "What?", "Where?", "When?", "Why?"]

# states: 0: init, 10: select a question, 20: wait for answer, 30: Answered
state = 0
while not done:
    # Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if state == 0:
                state = 10
        elif event.type == pygame.MOUSEBUTTONUP:
            if state == 20:
                if event.button == 1:  # left
                    nums_left += 1
                    state = 30
                elif event.button == 2: # middle
                    pass
                elif event.button == 3: # right
                    nums_right += 1
                    state = 30
            elif state == 30:
                state = 10 # ask a new question

    # Clear Background
    screen.fill(pygame.color.Color("white"))
    #  Manage state
    if state == 0:
        question_txt = sys_font.render("Press any key to start!", True, pygame.color.Color("violet"))
    elif state == 10:
        # choose a random question
        question = random.choice(questions)
        question_txt = sys_font.render(question, True, pygame.color.Color("seagreen"))
        # comment out the line below to replicate buggy behaviour
        state = 20  
    elif state == 20:
        # we're just waiting here, perhaps indicate the mouse should be clicked
        pass
    elif state == 30:
        question_txt = sys_font.render("Click for a new question", True, pygame.color.Color("turquoise"))
    else:
        raise NotImplementedError  # shouldn't get here

    # always draw the score
    score_txt = sys_font.render(f"Left: {nums_left} Right: {nums_right}", True, pygame.color.Color("olivedrab"))
    # update the screen
    screen.blit(question_txt, (20, 20))
    screen.blit(score_txt, (20, 220))
    # Frame Change
    pygame.display.update()
    clock.tick(60)
pygame.quit()

要充分利用Stack Overflow,请阅读并了解How to Ask。这将帮助您创建不会被低分忽略的问题。