首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >全局变量不是全局变量吗?

全局变量不是全局变量吗?
EN

Stack Overflow用户
提问于 2020-06-04 17:59:31
回答 2查看 133关注 0票数 1

这意味着这是一个tic tac脚趾游戏,尽管它还没有完成,下面的代码是我的整个程序,没有其他的导入或类似的东西,当我有top_left = top_left时,它意味着将顶部的变量top_left更改为1或2的值,当变量改变时,它的意思是在左上角或上中间的2的正方形上画一个X,因为某种原因,值会变化到一两个,但X或0没有绘制。

代码语言:javascript
复制
global top_left, top_middle, top_right
global middle_left, center, middle_right
global bottom_left, bottom_middle, bottom_right

#these are the variables used to check if someone has put their move their already
#0 = empty
#1 = Circle
#2  = X

top_left = 0
top_middle = 0
top_right = 0

middle_left = 0
center = 0
middle_right = 0

bottom_left = 0
bottom_middle = 0
bottom_right = 0


#code for changing turns
turn = 1
def turn_changer():
    global turn
    if turn == 1:
        turn = 2
    else:
        turn = 1

#board setup
def setup():

    size(600,600)

#this hurt my brain trying to fully understand
#lines dividing board
def draw():

    for y in range(3):
        for x in range(3):
            rect(200*x,200*y,200,200)
    #hope this is not what geomtry is like

    #top left ellipse
    if top_left == 1:
        ellipse(100,100,150,150)

    #top left X
    elif top_left == 2:
        line(0,0,200,200)
        line(200,0,0,200)

    #top middle ellipse
    if top_middle == 1:
        ellipse(300,100,150,150)

    #top middle  X
    elif top_middle == 2:
        line(200,0,400,200)
        line(400,0,200,200)

    #top right ellipse
    if top_right == 1:
        ellipse(500,100,150,150)

    #top right X
    elif top_right == 2:
        line(400,0,600,200)
        line(600,0,400,200)

    #middle left ellipse
    if middle_left == 1:
        ellipse(100,300,150,150)

    #middle left X
    elif middle_left == 2:
        line(0,200,200,400)
        line(200,200,0,400)

    #middle ellipse
    if center == 1:
        ellipse(300,300,150,150)

    #middle X
    elif center == 2:
        line(200,200,400,400)
        line(400,200,200,400)

    #middle right ellipse
    if middle_right == 1:
        ellipse(500,300,150,150)

    #middle right X
    elif middle_right == 2:
        line(400,200,600,400)
        line(600,200,400,400)

    #bottom left ellipse
    if bottom_left == 1:
        ellipse(100,500,150,150)

    #bottom left  X
    elif bottom_left == 2:
        line(0,400,200,600)
        line(200,400,0,600)

    #bottom middle ellipse
    if bottom_middle == 1:
        ellipse(300,500,150,150)

    #bottom middle X
    elif bottom_middle == 2:
        line(200,400,400,600)
        line(400,400,200,600)

    #bottom right ellipse
    if bottom_right == 1:
        ellipse (500,500,150,150)

    #bottom right X
    elif bottom_right == 2:
        line(400,400,600,600)
        line(600,400,400,600)

#dectects the quardnates where the mouse clicked and prints them
def mousePressed():
    println( (mouseX, mouseY) )

    #top left square hitbox
    if (mouseX > 0 and mouseX < 200) and (mouseY > 0 and mouseY < 200):
        top_left = turn
        turn_changer()
        print("top left")
        print(top_left)

    #top middle square hitbox 
    elif (mouseX > 200 and mouseX < 400) and (mouseY > 0 and mouseY < 200):     
        top_middle = turn
        turn_changer()

        print(top_middle)
        print("top middle")

    #top right square hitbox  
    elif (mouseX > 400 and mouseX < 600) and (mouseY > 0 and mouseY < 200):  
        turn_changer()
        print("top right")

    #middle left square hitbox
    elif (mouseX > 0  and mouseX < 200) and (mouseY > 200 and mouseY < 400):  
        turn_changer()
        print("middle left")

    #center square hitbox
    elif (mouseX > 200 and mouseX < 400) and (mouseY > 200 and mouseY < 400):  
        turn_changer()
        print("middle")  

    #middle right square hitbox
    elif (mouseX > 400 and mouseX < 600) and (mouseY > 200 and mouseY < 400):  
        turn_changer()
        print("middle right") 

    #bottom left square hitbox
    elif (mouseX > 0 and mouseX < 200) and (mouseY > 400 and mouseY < 600):  
        turn_changer()
        print("bottom left")

    #bottom middle square hitbox
    elif (mouseX > 200 and mouseX < 400) and (mouseY > 400 and mouseY < 600):  
        turn_changer()
        print("bottom middle")

    #bottom right square hitbox
    elif (mouseX > 400 and mouseX < 600) and (mouseY > 400 and mouseY < 600):  
        turn_changer()
        print("bottom right")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-04 18:09:29

global statement是一个包含整个当前代码块的声明。这意味着所列出的标识符将被解释为全局值。

这意味着,您必须将global语句放在要写入全局命名空间中的变量的函数中。

将全局声明移动到mousePressed

代码语言:javascript
复制
#dectects the quardnates where the mouse clicked and prints them
def mousePressed():
    global top_left, top_middle, top_right
    global middle_left, center, middle_right
    global bottom_left, bottom_middle, bottom_right

    println( (mouseX, mouseY) )

    #top left square hitbox
    if (mouseX > 0 and mouseX < 200) and (mouseY > 0 and mouseY < 200):
        top_left = turn
        turn_changer()
        print("top left")
        print(top_left)

    #top middle square hitbox 
    elif (mouseX > 200 and mouseX < 400) and (mouseY > 0 and mouseY < 200):     
        top_middle = turn
        turn_changer()

        print(top_middle)
        print("top middle")

    # [...]
票数 1
EN

Stack Overflow用户

发布于 2020-06-04 18:08:17

您似乎已经定义了几个函数,但没有调用它们中的任何一个。您的程序如何知道在运行它时使用哪一个?

您需要调用一个或多个函数。尝试在你的程序底部添加一个调用,你应该会“工作”。因为我会怀疑你会有错误。例如,如果要调用函数mousePressed(),则会出现抱怨未定义println的错误。实际上,println不是python函数。类似地,如果您不导入rect()ellipse()line(),您的程序就不会知道它们。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62201225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档