【问题标题】:How to receive updated variables from other functions?如何从其他函数接收更新的变量?
【发布时间】:2020-02-12 22:25:47
【问题描述】:

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

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

main():

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

fireBullet():

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

redrawGameWindow():

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 代表敌人。

感谢任何可以帮助阐明我的问题的人,祝大家度过愉快的一天

【问题讨论】:

  • 你能简化fireBullet函数吗?似乎有很多重复的代码。
  • 我想我可以尝试通过将所有重复代码放在它自己的函数中来简化,谢谢你的建议!

标签: python pygame return increment


【解决方案1】:

事实上,如果您对每个使用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)

【讨论】:

  • 确实有效,谢谢!但是,现在我遇到了另一个问题。由于某种原因,分数不会不断更新。比如,如果敌人在短时间内被多次射击,它有时会保持自己的分数,而其他时候会更新
  • 重复记分牌结果:Enemy Got Hit! 1 D Enemy Got Hit! 1 D Enemy Got Hit! 1 D Enemy Got Hit! 1 D Enemy Got Hit! 1 D Enemy Got Hit! 2 D Enemy Got Hit! 3 D Enemy Got Hit! 4 D Enemy Got Hit! 5 D Enemy Got Hit! 6 D Enemy Got Hit! 7 D Enemy Got Hit! 7 D Enemy Got Hit! 7 D Enemy Got Hit! 7 D Enemy Got Hit! 8 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D Enemy Got Hit! 9 D
  • 数字旁边的字母D、U、L和R代表敌人被击中的方向
  • @HaqeemWan 首先仔细检查您是否正确处理了所有出现的score。我不熟悉pygame,但这些问题可能会有所帮助:这里有异步函数吗?有没有可以使用的同步机制? pygame.event 是独一无二的吗?
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2018-07-02
  • 2018-03-07
  • 2017-10-15
  • 1970-01-01
  • 2018-10-16
  • 2022-01-22
  • 2013-09-13
相关资源
最近更新 更多