已设置全局变量,然后在Python Canvas中重置

问题描述 投票:0回答:1

下午好,

我正在为心理学实验建立(应该是)一个相对简单的问答。我认为我正在使用Pythons画布进行绘画,但是碰到了一些砖墙和经典的更新方案。这是代码:

# Replace with 60000 for 1 minute a question
screen_timeout = 10000
start_time     = clock.time()

# Create a canvas, mouse & keyboard object
canvas         = canvas()
mouse          = mouse()
kb             = keyboard(timeout=0)
question       = '1) Which two chemicals are discussed?'
answer         = ''
show_circle    = False

global circle_clicked
circle_clicked = False

def draw_question_and_answer(c, a):
    c.text('%s<br />(Just start typing; press enter to submit)<br /><br />%s' % (question, a))

def draw_mouse(c, (x, y)):
    c.fixdot(x, y)

def draw_circle(c):
    c['circle'] = Circle(0, 0, 50, fill=True, color='red')

def paint(c, a, s, (x, y)):
    c.clear()
    # show_circle_every(s, 2500)
    # NOTE Drawing order matters here
    if s:
        draw_question_and_answer(c, a)
        draw_circle(c)
        draw_mouse(c, (x, y))
        if (x, y) in c['circle']:
            circle_clicked = True   
    else: 
        draw_question_and_answer(c, a)
    c.show()

def game_loop(c, m, a, s):

    while True:
        if clock.time() - start_time >= screen_timeout:
            break   

        # if clock.time() - start_time >= 2500 and s == False:
        #   s = True

        response, timestamp_kb = kb.get_key()
        (x, y), timestamp_m    = m.get_pos()

        # TODO Extrapolate to function
        if s == False:
            if response == 'return':
                var.gq1 = a
                log.write_vars()
                break

            if response != None and response != 'right shift' and response != 'left shift':
                if response == 'space':
                    a += ' '
                elif response == 'backspace':
                    a = a[:-1]
                else: 
                    a += response
        paint(c, a, s, (x, y))

        # If the user enters the circle it should disappear
        print circle_clicked
        if clock.time() - start_time >= 2500 and circle_clicked == False:
            s = True

game_loop(canvas, mouse, answer, show_circle)

我想在这里做的是每2.5秒显示一个红色圆圈,并将该圆圈保留在那里,直到用户鼠标进入圆圈边界为止。在这些行中:

if clock.time() - start_time >= 2500 and circle_clicked == False:
    s = True

我将s变量(显示)设置为True,以显示有效的圆圈。在这一行:

if (x, y) in c['circle']:
    circle_clicked = True

如果用户进入圈子,则将我设置的圈子单击为true。但是,如果我打印这些值,我可以看到circle_clicked从True变为back False-怎么了?循环是否获得自己的circle_clicked版本?如果可以,怎么办?

我在这里做错什么,因为我认为这是一个非常简单的问题?来自纯Java脚本的背景也使我尝试学习Python做到这一点的事情复杂化。

谢谢

python game-loop
1个回答
0
投票

将您的paint函数更改为此

def paint(c, a, s, (x, y)):
    global circle_clicked
    c.clear()
    # show_circle_every(s, 2500)
    # NOTE Drawing order matters here
    if s:
        draw_question_and_answer(c, a)
        draw_circle(c)
        draw_mouse(c, (x, y))
        if (x, y) in c['circle']:
            circle_clicked = True   
    else: 
        draw_question_and_answer(c, a)
    c.show()
© www.soinside.com 2019 - 2024. All rights reserved.