分配全局之前引用的局部变量不起作用

时间:2019-11-14 18:51:49

标签: python function pygame

如何使此功能正常工作?有没有很好的全球替代品?

position=0
score=0
eggY=300
eggX=0
def egglg():
    while True:
        global eggX
        global eggY
        if eggX<330:
            eggX+=5
            eggY+=0.8
        elif eggX>=330:
            eggY=eggY+2
            if eggY>450:
                terminate()
            elif eggY<450 and eggY>350 and position ==1:
                score+=1
                return
#rest of my code, with chaning position to 1 when i press w

egglg()

它以某种方式返回0

1 个答案:

答案 0 :(得分:2)

由于要写入全局变量df2 <- as.data.frame(runif(10, min=0, max=100)) colnames(df2) <- "expression" rownames(df2) <- c("gene1", "gene2", "gene3", "gene4", "gene5", "gene6", "gene7", "gene8", "gene9", "gene10") df2$gene <- rownames(df2) > df2 expression gene gene1 47.63512 gene1 gene2 89.21983 gene2 gene3 86.43395 gene3 gene4 38.99895 gene4 gene5 77.73207 gene5 gene6 96.06180 gene6 gene7 43.46595 gene7 gene8 71.25147 gene8 gene9 39.99944 gene9 gene10 32.53522 gene10 ggplot(df2, aes(x = gene, y = expression, fill = gene))+ geom_dotplot(binaxis = "y", stackdir = "center", position = "dodge") eggXeggY,因此必须声明所有3个变量global

score

请注意,变量position=0 score=0 eggY=300 eggX=0 def egglg(): global eggX, eggY, score # [...] 仅被读取,因此无需声明它position