首页 > 解决方案 > 如何从其他函数接收更新的变量?

问题描述

所以在我的游戏中,我试图建立一个评分系统,每次击中敌人时,你的分数都会增加 1。但是,分数似乎没有更新,我不知道为什么以及如何要解决这个问题。如果可能的话,我不想使用全局变量,因为我的讲师鼓励我们反对它。

这是所有相关代码(我已经标记了提到分数变量的位置):

主要的():

def main():
    font = pygame.font.SysFont("28 Days Later", 35, True, False) # (Font name, Font size, Bold?, Italic?)
    board = player(225, 225, 64, 64, 0)
    DiArrow = enemy(100, 410, 48, 48, 410)
    score = 0 # <------------- SCORE
    bullets = []
    E_bullets = []
    while True:
        moveFunctions(board, DiArrow, bullets, E_bullets, score, font) # <------------- SCORE

移动功能:

def moveFunctions(board, DiArrow, bullets, E_bullets, score, font): # <------------- SCORE
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(15) # Frame rate per second (FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                stopGame()

        fireBullet(board, bullets, DiArrow, score) # <------------- SCORE
        E_fireBullet(board, E_bullets, DiArrow)

        arrowKeys = pygame.key.get_pressed()

        bulletDirection(arrowKeys, board, bullets)
        E_bulletDirection(DiArrow, E_bullets, board)
        playerControls(arrowKeys, board)
        enemyControls(DiArrow, board)
        redrawGameWindow(board, DiArrow, bullets, E_bullets, font, score) # <------------- SCORE

火子弹():

def fireBullet(board, bullets, DiArrow, score): # <------------- SCORE
    if board.lastDirection == "left":
        for bullet in bullets:
            if bullet.y - bullet.radius < DiArrow.hitbox[1] + DiArrow.hitbox[3] and bullet.y + bullet.radius > \
                    DiArrow.hitbox[1]:
                if bullet.x + bullet.radius > DiArrow.hitbox[0] and bullet.x - bullet.radius < DiArrow.hitbox[0] + \
                        DiArrow.hitbox[2]:
                    DiArrow.getHit()
                    score += 1 # <------------- SCORE
                    bullets.pop(bullets.index(bullet))
            if bullet.x < 500 and bullet.x > 50:
                bullet.x += bullet.vel
            else:
                bullets.pop(bullets.index(bullet))
    elif board.lastDirection == "right":
        for bullet in bullets:
            if bullet.y - bullet.radius < DiArrow.hitbox[1] + DiArrow.hitbox[3] and bullet.y + bullet.radius > \
                    DiArrow.hitbox[1]:
                if bullet.x + bullet.radius > DiArrow.hitbox[0] and bullet.x - bullet.radius < DiArrow.hitbox[0] + \
                        DiArrow.hitbox[2]:
                    DiArrow.getHit()
                    score += 1 # <------------- SCORE
                    bullets.pop(bullets.index(bullet))
            if bullet.x < 450 and bullet.x > 0:
                bullet.x += bullet.vel
            else:
                bullets.pop(bullets.index(bullet))
    elif board.lastDirection == "up":
        for bullet in bullets:
            if bullet.y - bullet.radius < DiArrow.hitbox[1] + DiArrow.hitbox[3] and bullet.y + bullet.radius > \
                    DiArrow.hitbox[1]:
                if bullet.x + bullet.radius > DiArrow.hitbox[0] and bullet.x - bullet.radius < DiArrow.hitbox[0] + \
                        DiArrow.hitbox[2]:
                    DiArrow.getHit()
                    score += 1 # <------------- SCORE
                    bullets.pop(bullets.index(bullet))
            if 500 > bullet.y > 50:
                bullet.y += bullet.vel
            else:
                bullets.pop(bullets.index(bullet))
    else:
        for bullet in bullets:
            if bullet.y - bullet.radius < DiArrow.hitbox[1] + DiArrow.hitbox[3] and bullet.y + \
                bullet.radius > DiArrow.hitbox[1]:
                if bullet.x + bullet.radius > DiArrow.hitbox[0] and bullet.x - bullet.radius < \
                    DiArrow.hitbox[0] + DiArrow.hitbox[2]:
                    DiArrow.getHit()
                    score += 1 # <------------- SCORE
                    bullets.pop(bullets.index(bullet))
            if 450 > bullet.y > 0:
                bullet.y += bullet.vel
            else:
                bullets.pop(bullets.index(bullet))
    # return score <------------------ I tried using return here but to no avail

重绘游戏窗口():

def redrawGameWindow(board, DiArrow, bullets, E_bullets, font, score): # <------------- SCORE
    window.blit(bg, (0,0))
    text = font.render("Score: " + str(score), 1, (255, 255, 255)) # <------------- SCORE
    window.blit(text, (380, 515))
    board.drawCharacter(window)
    DiArrow.drawEnemy(window)
    for bullet in bullets:
        bullet.draw(window)
    for bullet in E_bullets:
        bullet.draw(window)
    pygame.display.update()

我应该提到,除了分数之外,一切都在正常工作,并且我正在使用 Pygame 模块(尽管这里的问题与任何 Pygame 函数无关)。此外,棋盘代表玩家,而 DiArrow 代表敌人。

感谢任何可以帮助阐明我的这件事的人,并祝大家度过愉快的一天

标签: pythonpygamereturnincrement

解决方案


实际上,score如果您对每个使用 的函数都执行此操作,则从函数返回的尝试应该有效score,但是您还必须保存每次调用使用它的每个函数的返回值,以便在函数调用之间传播更新的值。修改您的代码以满足以下格式,您应该实现所需的行为。

def functionThatUsesScore(score):
    score += 1
    return score


def functionThatDoesNotUseScore():
    pass


def anotherFunctionThatUsesScore(score):
    score += 2
    return score


def main():
    score = 0
    while True:
        score = functionThatUsesScore(score)
        functionThatDoesNotUseScore()
        score = anotherFunctionThatUsesScore(score)

推荐阅读