如何从其他函数接收更新的变量?
How to receive updated variables from other functions?
所以在我的游戏中,我试图建立一个计分系统,每次击中敌人时,你的分数都会上升 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
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 函数无关) .另外,board 代表玩家,而 DiArrow 代表敌人。
感谢任何能帮助阐明我的这件事的人,祝大家度过愉快的一天
事实上,如果您对使用 score
的每个函数都这样做,那么您从函数中尝试 return score
应该会起作用,但您还必须保存 [=15] =]ed 每次调用每个使用它的函数的值,以便在函数调用之间传播更新的值。修改您的代码以满足以下格式,您应该会实现所需的行为。
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)
所以在我的游戏中,我试图建立一个计分系统,每次击中敌人时,你的分数都会上升 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
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 函数无关) .另外,board 代表玩家,而 DiArrow 代表敌人。
感谢任何能帮助阐明我的这件事的人,祝大家度过愉快的一天
事实上,如果您对使用 score
的每个函数都这样做,那么您从函数中尝试 return score
应该会起作用,但您还必须保存 [=15] =]ed 每次调用每个使用它的函数的值,以便在函数调用之间传播更新的值。修改您的代码以满足以下格式,您应该会实现所需的行为。
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)